You are on page 1of 7

The following Shell Scripts are the basic level scripts to get hands on linux commands.

1. Simple echo command to display what we give as arguments

#!/bin/csh

echo $0 $1 $2

2. Set of commands to display its output at once in the terminal.


#!/bin/csh
ls
file
ls | wc –l
3. Script to provide input through argument.
#!/bin/csh
set filename = $1
ls $filename
file $filename
ls -1 $filename | wc –l

4. Script to fetch today’s date through array


#!/bin/csh
echo It is `date`
set today = `date`
echo As you can see, today is $today[1] and the year is $today[6]

5. Example of foreach command

#!/bin/csh
foreach y ($*)
cal $y
end

6. Another example of foreach using basic commands


#!/bin/csh
foreach filename ($*)
echo --------------------
ls -l $filename
wc -l $filename
file $filename
end
echo --------------------

7. Example of ed editor
#!/bin/csh
set file = $1
ed $file <<%
1,$s/^/ /p
w work/icc_output_mod.txt
q
%

#!/bin/csh
ed -s $1 <<%
\$-9,\$p
q
%

#!/bin/csh
# prints first line of a file
ed -s $1 <<%
1,3p
q
%

ed -s $1 <<%
1,\$s/^/
/
w
q
%
ed -s $1 <<\%
1,$s/^/
/
w
q
\%

ed -s $1 <<'LastLine'
1,$s/^/ /
w
q
'LastLine'

#!/bin/csh
# prints first line of a file

ed -s $1 <<%
\$-9,\$p
q
%
8. Replacing old by new string using command line argument.
#!/bin/csh
# replaces all occurrences of a string by another string in a file
# It takes three arguments: old-string new-string filename
ed -s $3 <<LastLine
g/$1/s//$2/gp
w
q
LastLine
9. Renaming and copying examples
#!/bin/csh
if ( ! -e $2 ) then
mv $1 $2
endif

#!/bin/csh
if ( -e $2 ) then
echo "mv has not been done because $2 already exists"
else
mv $1 $2
endif

#!/bin/csh
if ( (! -f $1) || -e $2 ) then
echo "mv not done because $1 is not a file or $2 already exists"
else
mv $1 $2
endif

#!/bin/csh
if ( $#argv !== 2 ) then
echo Usage: $0 existingname nameofcopy
exit 1
endif

if ( -f $2 ) then
echo -n "Is it OK to overwrite $2? (type y or n): "
set reply = $<
if ( $reply != y ) then
echo $0 has not done the copying
exit 2
endif
endif
cp $1 $2

10. Renaming commands scripts with $argv concept


#!/bin/csh

if ( $#argv != 2 ) then
echo "Usage: $0 currentname newname"
exit 1
endif

if ( -f $1 ) then
set fromfile = isafile
else
set fromfile = isnotafile
echo "mv has not been done because $1 is not a file"
exit 1
endif

if ( -e $2 ) then
echo "mv has not been done because $2 already exists"
else if ( $fromfile == isafile ) then
mv $1 $2
endif

11. Scripts with concept of fetching .ext, filename and path accessing, modifying filename at
once.
#!/bin/csh
foreach f ($*)
set file = $f
set filer = $f:r
set ext = $f:e
set newfile = ${filer}_mod.$ext
cp $file $newfile
end

12. Few more script on copying.


#!/bin/csh
foreach f ($*)
set dir = $f:h
set nam = $f:t
echo "copying $f to ${dir}/old${nam}"
cp $f ${dir}/old${nam}
end

#!/bin/csh
foreach f ($*)
set file = $f
set filer = $f:r
set ext = $f:e
set newfile = ${filer}_mod.$ext
cp $file $newfile
end

#!/bin/csh

set myname = $0
set mynametail = $myname:t

if ( $#argv <= 2 ) then


echo Usage: $mynametail oldstring newstring filename ...
exit 1
endif
set oldstring = $1
set newstring = $2
shift
shift
foreach filename ($*)
echo ${mynametail}: about to process $filename
ed -s $filename <<LastLine
g/$oldstring/s//$newstring/gp
w
q
LastLine
end

You might also like