Start by looping over all entry listings in the current directory . Use a for OR
ID: 3728059 • Letter: S
Question
Start by looping over
all
entry listings in the current directory
. Use a
for
OR
for ... in
loop structure. Then
for each entry
, do the following:
1. Print out whether the entry name starts with a lower-case letter, upper-case
letter or a number or something else.
HINT: Use a
case
statement. Because the pattern matching for letters is case
insensitive, use the groups
[[:lower:]]
and
[[:upper:]]
rather than
[a-z]
and
[A-Z]
respectively. See slides for week 9.
2. Check the following (use a series of nested
if..then..else
statements):
A. If the entry is a directory, print this fact out and go to next entry.
B. If the entry is a file, check to see if it is readable.
a) If the file is not readable print this fact out and go to the next entry.
b) Else, check to see If the file is executable
If it is not executable, display the contents of the file and go to the entry.
Else, If it is executable, check to see if the file is the same name as
the script you are running.
HINT: Compare $entry to see if it is running your script.
You can do this by check the name of the currently
running program (the $0 variable) to the entry name:
if [ "$entry" = $0 ]
if so, skip executing the script
if not, then execute the file.
So, basically, display any readable but non-executable file and execute any
readable and executable file (unless it is the script being currently executed. This
is to prevent getting into a loop you can't get out of!).
Notice that this script takes
no arguments
at all. You should just run it from the
command line by just using the name of the script:
bash assg6.sh
and nothing else. This is how I will be testing your script.
It also shouldn't need the user to type in any input at the keyboard.
So don't use
read
command either.
Put in enough echo statements to make the listing of information more easier to
follow.
Explanation / Answer
#!/bin/bash
dir = $(pwd)
for file in $dir
do
$ [[ "$file" == "[[:lower:]*]" ]] && echo "File name starts with lowercase letter"
$ [[ "$file" == "[[:upper:]*]" ]] && echo "File name starts with uppercase letter"
$ [[ "$file" == "[[0-9]*]" ]] && echo "File name starts with a number" || echo "File name starts with some character other than alphanumeric"
if [[-d "$file"]]
then
echo "${file} is a directory"
conitnue
fi
if [[-d "$file"]]
then
if [[ -r $file ] ]
then
if [[ -x $file ] ]
then
if [ "$file" = $0 ]
then
echo "${file} is not executable"
continue
else
./$file
fi
else
cat $file
continue
fi
else
echo "${file} is not readable"
continue
fi
fi
done
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.