Basic Shell Scripting
Basic Shell Scripting
Zach Byerly
HPC User Services
LSU HPC & LONI
[email protected]
*: not by default
http://www.cis.rit.edu/class/simg211/unixintro/Shell.html
▪ The majority of script programs are “quick and dirty”, where the main
goal is to get the program written quickly
▪ May not be as efficient as programs written in C and Fortran
o Test whether you are using an interactive shell using $- (prints The current
set of options in your current shell.)
[fchen14@mike1 shelltut]$ echo $-
himBH
[fchen14@mike1 shelltut]$ cat checkshell.sh
#!/bin/bash
# read value # you can still interact with the script
echo $-
[fchen14@mike1 shelltut]$ ./checkshell.sh
hB
Subshell
o Definition:
o A subshell is a child process launched by a shell (or shell script).
o Just as your commands are interpreted at the command-line prompt,
similarly does a script batch-process a list of commands.
o Each shell script running is, in effect, a subprocess (child process) of
the parent shell.
o Two typical examples of starting subshell:
o Running a shell script launches a new process, a subshell.
o Type “bash” from an interactive shell
Outline
LD_LIBRARY_PATH colon-separated set of directories where libraries should be searched for first
HOME indicate where a user's home directory is located in the file system.
TERM specifies the type of computer terminal or terminal emulator being used
Operation Operator
Addition +
Subtraction -
Multiplication *
Division /
Exponentiation ** (bash only)
Modulo %
Operation bash
File exists if [ -e test ]
File is a regular file if [ -f test]
File is a directory if [ -d /home ]
File is not zero size if [ -s test ]
File has read permission if [ -r test ]
File has write permission if [ -w test ]
File has execute permission if [ -x test ]
Operation bash
Equal to if [ 1 –eq 2 ]
Not equal to if [ $a –ne $b ]
Greater than if [ $a –gt $b ]
Greater than or equal to if [ 1 –ge $b ]
Less than if [ $a –lt 2 ]
Less than or equal to if [ $a –le $b ]
Operation bash
Equal to if [ $a == $b ]
Not equal to if [ $a != $b ]
Zero length or null if [ -z $a ]
Non zero length if [ -n $a ]
Operation Example
! (NOT) if [ ! –e test ]
&& (AND) if [ -f test] && [ -s test ]
if [[ -f test && -s test ]]
if ( -e test && ! –z test )
Example 2
touch test.txt
if [ -e test.txt ]; then
echo “file exist”
elif [ ! -s test.txt ]; then
echo “file empty”;
fi
What happens after
echo “hello world” >> test.txt
Example 2:
for file in `ls /home/$USER`
do
cat $file
done
read counter
while [ $counter -ge 0 ]
do let counter--
echo $counter
done
a=3;b=4
echo "a= $a, b= $b"
result="nothing"
echo "result before calling the function is: " $result
func_add $a $b # note this is arguments to the function
echo "result by passing function arguments is: " $result
func_add $1 $2 # note this is command line arguments
echo "result by passing command line arguments is: "
$result
• grep
• sed
• awk
• grep OR
grep ‘Man\|Sales’ employee.txt
-> 100 Thomas Manager Sales $5,000
300 Raj Sysadmin Technology $7,000
500 Randy Manager Sales $6,000
• grep AND
grep –i ‘sys.*Tech’ employee.txt
-> 100300 Raj Sysadmin Technology $7,000
#!/bin/bash
# My First Script
#!/bin/bash
# My First Script
echo "Hello World!"
#!/bin/bash
echo "Hello World!"
• Alternate form
sed ’s/bash/tcsh/g; s/First/Second/g’ hello.sh
#!/bin/tcsh
# My Second Script
echo "Hello World!"
#!/bin/tcsh
# My First Script
echo "Hello World!"