Chapter 15: Scripts and scheduling
Exercise: Write and run your first scripts
Write a greeting script, run it every way that works, then pass it arguments and make it fail on purpose to see the exit status.
Write and run your first scripts
- Write a script with a shebang and make it executable.
- Run it with ./, bash and through PATH.
- Use arguments, quoting and exit statuses.
Start the exercise
On workstation, start the exercise. It removes the scripts of an earlier run from student's home on servera.
[student@workstation ~]$ lab start sa-first-scriptsLater tasks assume that you are logged in to the server they name: ssh student@servera from workstation, then sudo -i for a root shell.
Write hello.sh
On servera as student, create ~/scripts/hello.sh (use vim) with the content below. Try to run it before doing anything else.
#!/bin/bash
# Greet the user named in the first argument (default: the current user)
name=${1:-$USER}
echo "Hello, $name! You ran $0 with $# argument(s)."
echo "Today is $(date +%A) and this host is $(hostname -s)."[student@workstation ~]$ ssh student@servera
[student@servera ~]$ mkdir -p ~/scripts ~/bin
[student@servera ~]$ cd ~/scripts
[student@servera scripts]$ vim hello.sh
[student@servera scripts]$ ./hello.sh
-bash: ./hello.sh: Permission denied
[student@servera scripts]$ echo $?
126The file exists, but it is not executable yet: exit status 126.
Make it executable and run it
Make it executable and run it with no arguments, then with one argument, then with an argument that contains a space.
[student@servera scripts]$ chmod +x hello.sh
[student@servera scripts]$ ./hello.sh
Hello, student! You ran ./hello.sh with 0 argument(s).
Today is Saturday and this host is servera.
[student@servera scripts]$ ./hello.sh Maria
Hello, Maria! You ran ./hello.sh with 1 argument(s).
Today is Saturday and this host is servera.
[student@servera scripts]$ ./hello.sh Maria "two words"
Hello, Maria! You ran ./hello.sh with 2 argument(s).
Today is Saturday and this host is servera.The quotes group two words into one argument, so the script counts 2 arguments, not 3.
Run it through bash and through PATH
Remove the execute bit again and run the script with bash hello.sh. Restore the bit, copy the script to ~/bin, and run it from another directory by name.
[student@servera scripts]$ chmod -x hello.sh
[student@servera scripts]$ bash hello.sh x
Hello, x! You ran hello.sh with 1 argument(s).
Today is Saturday and this host is servera.
[student@servera scripts]$ chmod +x hello.sh
[student@servera scripts]$ cp hello.sh ~/bin/
[student@servera scripts]$ cd /tmp && hello.sh Ana
Hello, Ana! You ran /home/student/bin/hello.sh with 1 argument(s).
Today is Saturday and this host is servera.
[student@servera tmp]$ cd ~/scripts~/bin is already in your PATH (your ~/.bashrc adds it), so the name works from any directory.
Handle arguments properly
Write args.sh that prints its own name, the first two arguments, the count, and then each argument on its own line in square brackets using a loop over "$@". Run it with one "two words" three.
[student@servera scripts]$ cat args.sh
#!/bin/bash
echo "script: $0"
echo "first: $1 second: $2"
echo "count: $#"
echo "all: $@"
for a in "$@"; do echo " arg: [$a]"; done
[student@servera scripts]$ chmod +x args.sh
[student@servera scripts]$ ./args.sh one "two words" three
script: ./args.sh
first: one second: two words
count: 3
all: one two words three
arg: [one]
arg: [two words]
arg: [three]Change "$@" to $@ (without quotes) and run it again: two words is now split into two arguments.
Exit statuses
Show the exit status of true, of false, and of ls /nope. Then use && and || to print "found" or "not found" for /etc/hostname.
[student@servera scripts]$ true; echo $?
0
[student@servera scripts]$ false; echo $?
1
[student@servera scripts]$ ls /nope 2> /dev/null; echo $?
2
[student@servera scripts]$ ls /etc/hostname && echo found || echo "not found"
/etc/hostname
found
[student@servera scripts]$ ls /etc/nothing 2> /dev/null && echo found || echo "not found"
not foundGrade and finish
Check your work with lab grade sa-first-scripts, then put the exercise away with lab finish sa-first-scripts.