In: Computer Science
Lab 3-5:
Activity 5-5: Using Condition Statements 10 minutes
Objective: Create a script with if, then, and else statements.
Description: In this activity, you create a shell script that tests whether a file the user specifies exists and uses output and error redirection to do some simple command validation.
NOTE:
If necessary, start a virtual machine in the server mode, and sign-on. This script will determine if a user has sudo permission and then as such can execute the find command and traverse the hierarchy without Permission denied errors.
Make a subdirectory in your home directory and call it scripts (for roots scripts).
Exit insert mode and switch to extended mode. Save your changes and exit the vimeditor.
Give the owner execute permission to run the script. Run the script searching for a valid file, like bash.
What command did you use to run the script?
Answer:____________________________
Explain how you would test the script to make sure it was working.
Answer:___________________________________________
(a)
bash script.sh
(b) An if statement is a kind of switch; if something is true, then you'll do one thing, and if it's false, you'll do something different. This if-then instruction is exactly the kind of binary decision making computers are best at; all you have to do is define for the computer what needs to be true or false and what to do as a result.
The easiest way you test for True or False is the test utility. You don't call it directly, you use its syntax. Try this in a terminal:
$ if [ 1 == 1 ]; then echo "yes,
true, affirmative"; fi
yes, true, affirmative
$ if [ 1 == 123 ]; then echo "yes, true, affirmative";
fi
That's how test works. You have all manner of shorthand to choose from, and the one you'll use is the -z option, which detects if the length of a string of characters is zero (0). The idea translates in your despace script as:
#!/bin/sh
if [ -z "$1" ]; then
echo "Provide a \"file name\", using quotes to nullify the
space."
exit 1
fi
mv "$1" `ls "$1" | tr -d ' '`
The if statement is broken into separate lines for readability, but the concept remains: if the data inside the $1 variable is empty (zero characters are present), then print an error statement.
Try it:
$ ./despace
Provide a "file name", using quotes to nullify the space.
$
Notice the statement exit 1. This is a way for POSIX applications to send an alert to the system that it has encountered an error. This capability is important for yourself and for other people who may want to use despace in scripts that depend on despace succeeding in order for everything else to happen correctly.
The final improvement is to add something to protect the user from overwriting files accidentally. Ideally, you'd pass this option through to the script so that it's optional, but for the sake of simplicity, you'll hard code it. The -i option tells mv to ask for permission before overwriting a file that already exists:
#!/bin/sh
if [ -z "$1" ]; then
echo "Provide a \"file name\", using quotes to nullify the
space."
exit 1
fi
mv -i "$1" `ls "$1" | tr -d ' '`