L03 - C Shell Scripting - Part 1 1. What Is A Shell?: Shell Command-Line Interpreter
L03 - C Shell Scripting - Part 1 1. What Is A Shell?: Shell Command-Line Interpreter
L03 - C Shell Scripting - Part 1 1. What Is A Shell?: Shell Command-Line Interpreter
L03-1
After creating this file, type the following on the command line: >> chmod +x example.csh
Geophysical Computing
L03-2
This will set the permissions for your new file example.csh such that you are allowed to execute it. You only need to do this once for a new file and not after every time you edit it. Now you can execute the commands in this text file by typing: >> ./example.csh A couple notes on the above script. Line 1: #!/bin/csh - this basically just says that I want to use the C Shell to interpret these commands. Every C Shell script must start out with this as the top-most line. Line 2: # the simplest - you can add comments, and should frequently, to your scripts if you start the line out with the # symbol Filename: example.csh unlike on a windows machine Linux machines do not require you to have a file extension in most cases. However, it usually makes sense for people to adopt some kind of nomenclature so that you quickly know what kind of file you are dealing with. Hence, I usually use .csh to let me know that I have a C Shell script. OK, now that we have that out of the way, type up the following script and see what it does #!/bin/csh # Script to print user information who currently login , # current date & time clear echo "Hello $USER" echo "Today is \c ";date echo "Number of user login : \c" ; who | wc l echo "Calendar" cal
Note that some versions of C-Shell require you to use echo e so that the \c will not print to the screen.
3. C Shell Variables
There are two types of variables: (1) System variables that are created and maintained by the Linux system itself. We saw one example of these in the example script above: $USER. Another example would be if you wanted to print out your home directory then you could type: >> echo $HOME (2) User defined variables that are created and maintained by the User.
Geophysical Computing
L03-3
Setting variables in a C Shell script is done in two ways: (a) String variables. String variables are just treated as a bunch of text characters. i.e., you cannot do math with them. String variables are created with the set command as shown below. #!/bin/csh set x = 1 set y = 10.5 set myvar = super echo $x $y $myvar echo $x + $y
(b) Numeric variables. The C Shell can only handle integer valued numeric variables. Setting variable names is done with the @ symbol. Below is a simple example. #!/bin/csh @ x = 1 @ x = $x + 10 echo $x What happens if you try: set x = $x + 10 in the above script? (c) Arrays of String Variables. You can also use a single variable name to store an array of strings. #!/bin/csh set days = (mon tues wed thurs fri) echo $days echo $days[3] echo $days[3-5] As a special note: variables are case sensitive. For example, the three following combinations of the letters n and o are all considered to be a different variable by the C Shell. This is important to remember as it is not the case with other programming languages (e.g., in Fortran all three of these variable names would be considered to be the same variable). set no = 10 set No = 11 set nO = 12
Geophysical Computing
L03-4
Quotes Name
Meaning
"Double Quotes" - Anything enclosed in double quotes removes the meaning of the characters (except \ and $). For example, if we set arg = blah, then echo $arg would result in blah being printed to the screen. 'Single quotes' Text enclosed inside single quotes remains unchanged (including $variables). For example, echo $arg would result in $arg being printed to the screen. That is, no variable substitution would take place. `Back quote` - To execute a command. For example, `pwd` would execute the print working directory command.
" ' `
To see the effect of the single or double quote add the following to the above script:
echo $minX
Geophysical Computing
L03-5
echo $minX
The back quote is really useful. This allows us to set a shell variable to the output from a Unix command:
#!/bin/csh set mydir = `pwd` # set variable to current working directory # what does this do?
As a final note on displaying shell variables it is often useful to concatenate shell variables: #!/bin/csh set year set month set day = 2010 = 12 = 30
set output1 = ${year}_${month}_${day} set output2 = ${year}${month}${day} echo $output1 echo $output2 mv inputfile ${output1}.txt Note that we use the { } brackets in this example. This is because if I just type $year_ then the shell would look for a variable called year_.
Geophysical Computing
L03-6
#!/bin/csh set ifile = $argv[1] echo Now lets perform some kind of action on file: $ifile
If I named this C Shell script: action.csh and we want to perform the action on the file foo.txt then we need to type: >> action.csh foo.txt on the command line to make this work. This is really useful when we want to make generalized scripts that dont require editing the variable names every time we want them to run.
Geophysical Computing
L03-7
If the special variable, noclobber is set, any redirection operation that will overwrite an existing file will generate an error message and the redirection will fail. In order to force an overwrite of an existing file using redirection, append an exclamation point (!) after the redirection command. For example for the command: date >! datefile The file datefile will be overwritten regardless of its existence. The output of one command can be sent to the input of another command. This is called piping. The commands which are to be piped together are separated by the pipe character. For example: ls -l | sort -k 5n This command takes the output of the ls -l command and puts the output of it into the sort command.
7. Homework
1) Write a C Shell script that will allow you to set the name of an input postscript file and desired output name of a jpg file, and then use ImageMagicks convert command to convert a postscript file into a jpeg image. E.g., At the very least I should enter, either by the command line or by interactive input the name of an input .ps file, and desired name of output .jpg file and the script will automatically create the .jpg file.
2) Write a C Shell script that will add the current date to the end of a filename. E.g., if today is Dec 25, 2010, then the shell script should change the filename to: filename.20101225 The script should read the filename from the command line. Hence, if we named this script adddate then execution of this command should look like: >> addate filename
Geophysical Computing
L03-8
3) Write a C Shell script that will remove dates added with the script written in Problem #2. Note: this script should also work when there is a dot in the filename. E.g., the code should work for any filename of the form foo.20101225 foo.foo.20101225 foo.foo.foo.20101225 foo.foo.foo.*.20101225 Output file names for the examples above should be: foo foo.foo foo.foo.foo etc.
4) Write a script that will replace spaces in file names with underscores. E.g., if the input file is named: My File.txt , then the output file should be named My_File.txt.