Professional Documents
Culture Documents
#!/bin/csh
echo $0 $1 $2
#!/bin/csh
foreach y ($*)
cal $y
end
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
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
#!/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