Shell script (1/2)
In Linux single command do one thing and do it well
Complex command is a combination of single commands
You can make a new complex command by placing many single
commands in a file
The file itself is called a shell script
03/03/09 Meeting title Rev. 1.00 00000-A
Confidential ©2008. Renesas Technology Corp., All rights reserved.
Shell script (2/2)
Shell script can be called as a single command
The name of a command is the name of the script file
$ script_file_name option argument
03/03/09 Meeting title Rev. 1.00 00000-A
Confidential ©2008. Renesas Technology Corp., All rights reserved.
Shell script
To make your script file executable you need to change mode of
your file
$ chmod +x script_file_name
03/03/09 Meeting title Rev. 1.00 00000-A
Confidential ©2008. Renesas Technology Corp., All rights reserved.
Which shell
To know which is the current shell
$ echo $SHELL
/bin/csh
03/03/09 Meeting title Rev. 1.00 00000-A
Confidential ©2008. Renesas Technology Corp., All rights reserved.
Shell script
To specify what shell you want your script to be executed, you need
to add
#!location_of_ shell
to the beginning script file To know the location of a shell
$ which shell_name
$ which sh
/bin/sh
03/03/09 Meeting title Rev. 1.00 00000-A
Confidential ©2008. Renesas Technology Corp., All rights reserved.
Bourne Shell
Bourne shell script should begin with
#!/bin/sh
03/03/09 Meeting title Rev. 1.00 00000-A
Confidential ©2008. Renesas Technology Corp., All rights reserved.
Output text to screen
To output text to the screen
echo text_to_output
#/bin/sh
echo “Hello 9G”
$sh my_script
Hello 9G
Please refer to “echo.sh”
03/03/09 Meeting title Rev. 1.00 00000-A
Confidential ©2008. Renesas Technology Corp., All rights reserved.
Variable (1/4)
A named item that can hold a value
To assign value for variable
VARIABLE_NAME=VALUE
To refer to variable value
$VARIABLE_NAME
Please refer to “variable.sh”
03/03/09 Meeting title Rev. 1.00 00000-A
Confidential ©2008. Renesas Technology Corp., All rights reserved.
Variable (2/4)
Consider this example
MYVAR=”Hello 9”
echo “$MYVARGeneration”
The program will output nothing because it does not understand
what is $MYVARGeneration
Please refer to “variable.sh”
03/03/09 Meeting title Rev. 1.00 00000-A
Confidential ©2008. Renesas Technology Corp., All rights reserved.
Variable (3/4)
If you want to specify a string follow a variable you must specified the
end of a variable
The end of a variable can be specified by using {}
MYVAR=”Hello 9”
echo “${MYVAR}Generation”
Please refer to “variable.sh”
03/03/09 Meeting title Rev. 1.00 00000-A
Confidential ©2008. Renesas Technology Corp., All rights reserved.
Variable (4/4)
To read the value from user input
read VARIABLE_NAME
Please refer to “read.sh” for example
Please refer to “read.sh”
03/03/09 Meeting title Rev. 1.00 00000-A
Confidential ©2008. Renesas Technology Corp., All rights reserved.
for loop (1/5)
General Syntax
for variable in list_of items
do
command 1
command 2
.....
last command
done
03/03/09 Meeting title Rev. 1.00 00000-A
Confidential ©2008. Renesas Technology Corp., All rights reserved.
for loop (2/5)
This code will list all files in the current directory
for filename in *
do
echo “$filename”
done
Please refer to “for_3.sh”
03/03/09 Meeting title Rev. 1.00 00000-A
Confidential ©2008. Renesas Technology Corp., All rights reserved.
for loop (3/5)
This code will print value from 1 to 10
for i in 1 2 3 4 5 6 7 8 9 10
do
echo $i
done
Please refer to “for_1.sh”
03/03/09 Meeting title Rev. 1.00 00000-A
Confidential ©2008. Renesas Technology Corp., All rights reserved.
for loop (4/5)
max=10
for ((i=1; i <= max; i++))
do
echo $i
done
Please refer to “for_2.sh”
03/03/09 Meeting title Rev. 1.00 00000-A
Confidential ©2008. Renesas Technology Corp., All rights reserved.
for loop (5/5)
Nested Loop
for i in 1 2 3 4
do
for j in 1 2 3 4
do
echo “Row $i Column $j”
done
done
Please refer to “nested_for.sh”
03/03/09 Meeting title Rev. 1.00 00000-A
Confidential ©2008. Renesas Technology Corp., All rights reserved.
If
Syntax
if ( test condition)
then
command 1
command 2
.....
fi
03/03/09 Meeting title Rev. 1.00 00000-A
Confidential ©2008. Renesas Technology Corp., All rights reserved.
If statement (1/4)
Condition of comparing number
03/03/09 Meeting title Rev. 1.00 00000-A
Confidential ©2008. Renesas Technology Corp., All rights reserved.
while statement
Syntax
while ( test condition)
do
commands .....
done
Please refer to “while_1.sh”
03/03/09 Meeting title Rev. 1.00 00000-A
Confidential ©2008. Renesas Technology Corp., All rights reserved.
case statement
Syntax
case word in
value1)
command ..
;;
value2)
command...
;;
*)
command ...
esac
Please refer to “case.sh”
03/03/09 Meeting title Rev. 1.00 00000-A
Confidential ©2008. Renesas Technology Corp., All rights reserved.