Csci 132 Spring 13 Assignment 2 Due: Tuesday, March 5 (by 11:59PM) A. Readings Read and understand this part before starting working on the exercises of part B. Variables In a Bash script a variable can be dened simply by choosing its name and assigning it a value: MYVAR=5 Be careful, there should be no white space around the =. The following is incorrect: MYVAR = 5 # Wrong. Will give error message You can get the value that a variable holds by prepending the symbol to its name: Y=5 echo Y # Will print: 5 echo Y # Will print: Y You can perform arithmetic operations variables, numbers or both, using the (( operation )) X=5 X=(( X + 5 )) echo X # Will print: 10 Y=10 Z=(( X + Y )) echo Z # Will print: 20 if statements An if statement executes a set of commands if a condition is true, or another set of commands if the condition is false. Syntax: if [ condition ]; then command(s) # Will be executed only if condition is true command(s) # Will be executed only if condition is false *** Notice the white spaces in the line: if [ condition ]; then **** # Compares the rst argument with the number 10 and prints # a relevant message. if [ 1 -lt 10 ]; then echo 1 is less than 10 echo 1 is greater than 10
If you save the above script as comparewithten, then it can be run as: comparewithten 5 5 is less than 10 comparewithten 25 25 is greater than 10 while loop A while loop executes a set of commands repeatedly, for as long as its condition is true. Syntax: while [ condition ] do command(s) done x=1 while [ x -le 5 ] do echo Hello World x=(( x + 1 )) done The above will print Hello World 4 times. (Why?) File Conditions There are some useful conditions that we can include in our Bash scripts to check various things regarding les. Option Description -f le Checks if le exists -x le Checks if le is executable (and exists) -d dir Checks if dir is an existing directory # Checks if the le somele.dat exists FILE=somele.dat if [ -f FILE ]; then echo File FILE exists echo File FILE does not exist We can search for a given word in a le, using the grep lter in the command line: grep -Fxq word le Check grep s man page to see what the Fxq options do.
We can use the grep lter in our scripts to check if a given string exists in a le or not: word=hello #Variable that holds the string we want to search for le=myle #Variable that holds the le s name if grep -Fxq "word" le then echo Found! echo Not Found Notice the different syntax for the if statement in this case; there are no [ ] and the then keyword is in a new line. What makes this work is that the grep command will return 0 if the word is found and 1 if not. In Bash 0 is interpreted as true and 1 as false. So, if grep nds the word in the le it will return 0, which for Bash means that the condition of the if statement is true (and therefore print Found! ). If the word is not found, grep will return 1 which means that the if s condition is false, so it will print Not Found. B. Exercises 1. Write a script named range which will print all the integers between a lower and maximum value that the user should provide as arguments. For example: range 5 13 5 6 7 8 9 10 11 12 13 range 22 17 22 21 20 19 18 17
Your script should take exactly two arguments. In a different case, it should inform the user about the correct usage and quit. range 6 Usage: range <start> <end> Notice that if the rst argument is less than the second, the numbers are printed in ascending order. In the opposite case, they are printed in descending order. 2. Write a script called add_names that will get a name as argument and add this name in le callesd names_list.txt This le should contain names; one name per line. The script should check that the name does not exist in the le before adding it. If the name exists, the script should print a relevant message and quit. It should also check that only one argument is provided and in any other case print a message and quit. Example Assuming that we have a le that contains: Tom Mary Bob Alice Then after running: add_names John our le should now contain: Tom Mary Bob Alice John While: add_names Bob Name already exists And: add_names Nick Angelo I can only add one name at a time
Submission Create your scripts in your favorite editor in Diogenes. Save them in your home folder with the names: yourusername_assignment2_1 yourusername_assignment2_2 Change the permissions of the scripts to 600 Copy them to: /home/students/cs132