Executing a script
A. Writing it directly in bash interpreter
B. First write the script then execute
A.
1. Go to directory where the script should live
2. Create a file with .sh extension
3. Write script in that file using an editor (e.g. vi)
Indicate the file is a bash script with #!/bin/bash
4. Make script executable >chmod 755 [script name].sh
5. Run script > ./[script name].sh
B.
Shell script with variable in script
#! /bin/bash
MY_SHELL="eat"
echo "I am ${MY_SHELL}ing my keyboard."Output
I am eating my keyboard
Shell script with output of a command as variable
!# /bin/bash
SERVER_NAME = $(hostname) »_space;this is the command
echo “You use ${hostname}”
Output
You use Linux
Executing Python scripts via the interpreter
File Operators
String Operators
-z STRING True if string is empty.
-n STRING True if string is not empty.
STRING1 = STRING2
True if the strings are equal.
STRING1 != STRING2
True if the strings are not equal
Arithmetic operators
arg1 –eq arg2 True if arg1 is equal to arg2.
arg1 –ne arg2 True if arg1 is not equal to arg2.
arg1 –lt arg2 True if arg1 is less than arg2.
arg1 –le arg2 True if arg1 is less than or equal to arg2.
arg1 –gt arg2 True if arg1 is greater than arg2.
arg1 –ge arg2 True if arg1 is greater than or equal to arg2
IF Statement Syntax
if [ condition-is-true ]
then
command 1
command 2
command N
fiIF Statement script - Example
#!/bin/bash
MY_SHELL="bash"
if [ "$MY_SHELL" = "bash" ]
then
echo "You seem to like the bash shell."
fiOutput:
You seem to like the bash shell.
IF/THEN/ELSE Syntax
if [ condition-is-true ]
then
command N
else
command N
fiIF/THEN/ELSE Script Example
#!/bin/bash
MY_SHELL="csh"
if [ "$MY_SHELL" = "bash" ]
then
echo "You seem to like the bash shell."
else
echo "You don't seem to like the bash
shell."
fiIF / ELIF / ELSE Syntax
if [ condition-is-true ]
then
command N
elif [ condition-is-true ]
then
command N
else
command N
fiIF / ELIF / ELSE Example
!/bin/bash
MY_SHELL=”csh”
if [ “$MY_SHELL” = “bash” ]
then
echo “You seem to like the bash shell.”
elif [ “$MY_SHELL” = “csh” ]
then
echo “You seem to like the csh shell.”
else
echo “You don’t seem to like the bash or csh shells.”
fi
FOR Loop Syntax
for VARIABLE_NAME in ITEM_1 ITEM_N
do
command 1
command 2
command N
doneFOR Loop Script Example
#!/bin/bash
for COLOR in red green blue
do
echo "COLOR: $COLOR"
done
Output:
COLOR: red
COLOR: green
COLOR: blue
\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
#!/bin/bash
COLORS="red green blue"
for COLOR in $COLORS
do
echo "COLOR: $COLOR"
doneFOR Loop Script Example 2
#!/bin/bash
PICTURES=$(ls *jpg)
DATE=$(date +%F)
for PICTURE in $PICTURES
do
echo "Renaming ${PICTURE} to ${DATE}
-${PICTURE}"
mv ${PICTURE} ${DATE}-${PICTURE}
done
$ ls bear.jpg man.jpg pig.jpg rename-pics.sh $ ./rename-pics.sh Renaming bear.jpg to 2015-03-06-bear.jpg Renaming man.jpg to 2015-03-06-man.jpg Renaming pig.jpg to 2015-03-06-pig.jpg $ ls 2015-03-06-bear.jpg 2015-03-06-man.jpg 2015-03-06-pig.jpg rename-pics.sh
Positional Parameters Syntax
$ script.sh parameter1 parameter2 parameter3
$0 : “script.sh”
$1 : “parameter1”
$2 : “parameter2”
$3 : “parameter3”
Positional Parameters script Example
#!/bin/bash
echo "Executing script: $0"
echo "Archiving user: $1"
# Lock the account
passwd –l $1
# Create an archive of the home directory.
tar cf /archives/${1}.tar.gz /home/${1}$ ./archive_user.sh elvis
Executing script: ./archive_user.sh
Archiving user: elvis
passwd: password expiry information changed.
tar: Removing leading `/’ from member names
Accepting User Input (STDIN) Syntax
The read command accepts STDIN.
read -p “PROMPT” VARIABLE
Accepting User Input (STDIN) Example
!/bin/bash
read –p “Enter a user name: “ USER
echo “Archiving user: $USER”
# Lock the account passwd –l $USER
Create an archive of the home directory.
tar cf /archives/${USER}.tar.gz /home/${USER}
____________________
$ ./archive_user.sh
Enter a user name: mitch
Archiving user: mitch
passwd: password expiry information changed.
tar: Removing leading `/’ from member names