If/Else Tests

Tip for working with longer scripts: Use variables on the shell command line to experiment with how shell scripts work. For example, the following skeleton script can be used to test out your tasks on commandline by setting the variable VAR:

if [ -n "$VAR" ]; then
    echo "non-empty"
fi
     

On the command line, you can set VAR to different values and recall the if statement to re-execute the test command. Remember to export your variable using export. Alternatively, the script itself may be modified using your text editor of choice

Part One

1. Output cow found if the file whose path is the variable file contains the string cow. You can find this as an example script here

if grep -q cow "$file"; then
  echo cow found
fi
       

2. Output there are subdirectories if the current directory has subdirectories.

if ls -F | grep -q '/$'; then
  echo there are subdirectories
fi
       

3. If you used an if statement to do #2, redo it using &&. If you used &&, redo it using an if statement.

ls -F | grep -q '/$' && echo there are subdirectories
       

4. Remove the $file if it is empty (be careful how you use this!)

[ ! -s "$file" ] && rm -i "$file" OR
if [ ! -s "$file" ]; then
    rm -i "$file"
fi
       

5. Output the word writeable if the current directory is writeable by you. Test this command when connected to several directories such as your home directory and the system directory /bin

if [ -w . ]; then
    echo writable
fi
       

6. Output yes if there is a file named foo in the current directory.

if [ -f foo ]; then
    echo yes
fi
       

7. You want to create a file named $$tmp in the current directory. output ok if there is nothing in the way and if you can create the file.

if [ ! -e $$tmp ] && touch $$tmp; then
    rm -f $$tmp; echo ok
fi
       

8. Output yes if the content of $path 'looks like' an absolute path.

if [ "$(echo $path | cut -c1)" = / ]; then
    echo yes
fi OR 
if [[ "$path" =~ ^/ ]]; then
    echo yes
fi OR
if [[ "$path" = /* ]]; then
    echo yes
fi
       

9. Output upper if the contents of the variable string is entirely upper-case. (like GREG)

10. Output symlink to x, broken symlink or not symlink depending on whether $sym is a symbolic link. x is the type of object the link points to and should be file, directory or other. Note: this is a pretty long nested if-statement. You may want to put it in a file.

if [ -h "$sym" ]; then
    if [ -e "$sym" ]; then
        if [ -f "$sym" ]; then
            echo "symlink to file"
        elif [ -d "$sym" ]; then
            echo "symlink to directory"
        else 
            echo "symlink to other"
        fi
    else
        echo "broken symlink"
    fi
else
    echo "not symlink"
fi
       

11. Assuming there are 30 days in each month, output the number of days remaining in this month.

# note: the date command displays single digit days with a prefix '0', causing interpretation as an 
# octal number. To force it to be interpreted as decimal, prefix it with 10#
day=$(date | tr -s ' ' | cut -d' ' -f3)
mon=$(date | cut -d' ' -f2)
echo "$((30 - 10#$day)) left"
       

12. You want to copy the file $file1 to the directory that $file2 is in. Write a command to do it.

cp $file1 $(dirname $file2)
       

13. Create a simple shell script that takes one commandline argument and outputs a message to indicate the permissions of the argument's value. It should output a message that indicates whether the argument does not exist, or, if it does, whether it is readable, writable and/or executable.

There are a lot of ways to write this, but here's there's one solution in the file
samples/args/iftest/perms.bash
       

More Exercises