Concepts we covered
- Aliases: these are shortcuts you can define to call often-used
commands.
- For example, let's assume you want to create an alias for
printing "sausages"
- Enter the following on the comand line: alias ilike="echo sausages"
- This means that everytime you enter "ilike" the following
command is carried out instead: "echo
sausages". Try it!
- User permissions: We played around with the chmod command to define who can access a file or directory. For example:
- chmod a+x filename would
give everyone ("a" means all) executable permissions
- chmod u-x filename would remove executable permissions from the currrently logged in user ("u" means user).
- Here is a tutorial on chmod: http://catcode.com/teachmod/
Read it up to "chmod shortcuts".
- Redirecting text output:
- To create a new text file containing the output of a command do the following:
- Example: to put the output from "ls" into a file called "directory.txt" enter this: "ls > directory.txt". Try it!
- This will overwrite the contents of the text file each time. To attach a text to the end of a file without overwriting it, use ">>" instead.
- Example: to put the output from running the fortune command into "fortunes.txt" three times in a row, enter this: "fortune >> fortunes.txt". Do it three times, then check the contents of "fortune.txt" with the nano or cat commands.
- Special arguments to scripts:
- $1, $2, $3 etc. are special variables when used in a script.
They are used to access arguments you pass to the script. For example:
- A script called fruits.sh is run with 2 arguments: ./fruits.sh apples oranges
- Then $1=apples and $2=oranges and can be accessed from within the script.
- Here is a nice summary (which you need to read to be able to do your homework): http://osr507doc.sco.com/en/OSUserG/_Passing_to_shell_script.html
- The "IF" statement
- We created a very simple script that gave an output dependent
on what was passed as an argument to the script.
- Take a look at numbers.sh in /home/wrede/unix/unix103. Copy it
to your homedirectory and play around with it. Especially important is
the "exit" command in this script.
- Here is a good tutorial: http://www.linuxtutorialblog.com/post/tutorial-conditions-in-bash-scripting-if-statements
- Read the tutorial up to (but not including) "2. Double Bracket
Syntax"
Homework
- Try out the following aliases by entering them on the command line:
alias
la=’/bin/ls -lah –color=auto’
alias ll=’ls
-lh’
alias ls=’ls
–color=auto’
alias ..=’cd
..’
alias …=’cd
../..’
- Make these aliases permanent by adding them to your bashrc
- Create a directory called homework103 in your home directory. Use chmod to prevent others from accessing it and stealing your homework! Yes, this happens. All the time.
- In your new homework directory create a script called "cowmaker.sh" that does the following:
- It requires two arguments. If it does not get two arguments the script should exit early with an error message.
- The first argument specifies a filename. The script writes its output into this file.
- The second argument provides a text string which is put into a cow and then written to a file whose name is given by the first argument. This text must be given in inverted commas, by the way, so that the script recognises it as one argument.
- Example:
- Running cowmaker.sh outputfile.txt "I love unix almost as much as the baby jesus"
- would put the text "I love unix almost as much as the baby jesus" into a text file called outputfile.txt
- In your new homework directory create a script called "switch.sh" that does the following:
- The script is given 2 filenames and renames them so that their
names are switched. In other words: switch.sh
file1 file2 would rename file1 to file2 and vice versa.
- HINT: this is not as easy as it sounds. You will have to work
with a temporary file. The steps are
- rename file1 to temp
- rename file2 to file1
- rename temp to file2
- The script requires two arguments. If it does not get two
arguments the script should exit early with an error message. Also, if
both arguments are the same, the script should exit with a different
error message.
- You script cowmaker is dangerous because it can self destruct: if you provided "cowmaker.sh" as the filename then the script would overwrite itself with its own output.
- Make an improved version of the script called cowmaker2.sh. If
you provide this script with a filename that already exists, it should
exit with an error message.
- You can do this using if [ -a filename ] (read the tutorial given above for more info).