Professional Documents
Culture Documents
3 Shell Scripting
3 Shell Scripting
3 Shell Scripting
Computing:
Shell scripting
Amit Amritkar
How to edit a file
• Multiple options for command line editors (no GUI
needed) like emacs, nano, vim etc.
$ vim myfirstfile
Learning Curves of Editors
Vim editor - Modes
• 3 Modes:
- l …move right
- k …move up
- j …move down
Vim editor - Editing (command mode)
- i … insert after the cursor.
- d …delete the characters from the cursor position up the position given by the next
command (for example d$ deletes all character from the current cursor position up to
the last column of the line).
- c …change the character from the cursor position up to the position indicated by the
next command.
- x …delete the character under the cursor.
- X …delete the character before the cursor (Backspace).
- y …copy the characters from the current cursor position up to the position indicated
by the next command.
- p …paste previous deleted or yanked (copied) text after the current cursor position.
- P …paste previous deleted or yanked (copied) text before the current cursor position.
- r …replace the current character with the newly typed one.
- s …substitute the text from the current cursor position up to the position given by the
next command with the newly typed one.
- . …repeat the last insertion or editing command (x, d, p…).
- Doubling d, c or y operates on the whole line, for example yy copies the whole line.
Vim editor - More
• /string or ?string Search for the string (forward or backward)
• Don’t be afraid to try the various commands, you can undo almost
anything using u in the command mode, and redo using Ctrl-r.
• “Hello”
• “Hello World!”
• Options include 3 permission levels (r, w, e) for 3 groups (u, g, o), while
keeping/overwriting the existing file permissions.
chmod u+r file Adds read permission for owner.
chmod ug+w file Adds write permission for owner and group.
chmod o-x file Removes execute permission for other.
chmod (a)+r file Adds read permission for everyone.
• “-rwxr-xr-x” = 755
• “-rw-rw-r--” = 664
• “-r-x--------” = 500
• Example:
# chmod 755 dictionary.txt
# chmod -R 755 ../intro2linux/
Shell Scripts
• Shell script is a file containing a series of commands (including shell scripts!)
• shell reads this file and carries out the commands as if they were entered on
the shell prompt
cd $HOME
tar -xf intro2linux.tgz
tar -cvzf example.tar.gz intro2linux
mkdir dustbin
mv example.tar.gz ./dustbin
cd dustbin
tar -xvf example.tar.gz
mv intro2linux newdir
ls newdir > contents.txt
cd $HOME
exit 0
#!/bin/bash
# Naked variables
echo
# Assignment
a=879
echo "The value of \"a\" is $a."
exit 0
Bash Variables … are untyped
• Unlike many other programming languages, Bash does not segregate its variables
by "type."
#!/bin/bash
# int-or-string.sh
a=2334 # Integer.
let "a += 1"
echo "a = $a " # a = 2335
echo # Integer, still.
$ touch junk
$ ./rem
The return code from rm was 0
$ ./rem
rm: junk: No such file or directory
The return code from rm was 1
Flow Control - if…
• The if command makes a decision based on
the exit status of a command.
if [ -f ~/.bash_profile ]; then
echo "You have a .bash_profile. Things are fine."
else
echo "Yikes! You have no .bash_profile!"
fi
Flow Control - for loops
Syntax
for index in [argument list]
do
command(s) ...
done
for i in 1 2 3 4 5 6 7 8 9 10
do
echo -n This is iteration $i
echo -n " and the time is "
date +%T
sleep 1
done
command substitution brace expansion
# loop1.sh # loop1.bash
for i in $(seq 1 10) for i in {1..10}
do do
... ...
done done
Flow Control - for loops
• Another syntax in C/C++ Style:
#! /bin/bash
# loop2.sh
• Syntax:
• Local variables can be declared within the function body with the local keyword and can be used
only inside that function. You can have local variables with the same name in different functions.
#!/bin/bash
var1='A'
var2='B'
my_function () {
local var1='C'
var2='D'
echo "Inside function: var1: $var1, var2: $var2"
}
my_function
for fyle in *; do
bad=0
for word in $@; do
grep $word $fyle > /dev/null 2> /dev/null
if [ $? -ne 0 ]; then
bad=1
break
fi
done
if [ $bad -eq 0 ]; then echo $fyle; fi
done
exit 0
Shell Scripting Programming Flow
More Unix Commands (useful in shell scripts)
• Free Ebooks
Advanced Bash-Scripting Guide
http://tldp.org/LDP/abs/html/