rajeshkumar created the topic: Questions to test your linux shell Script
www.scmgalaxy.com/index.php?option=com_k…34&id=427&Itemid=442
www.scmgalaxy.com/index.php?option=com_k…34&id=426&Itemid=442
www.scmgalaxy.com/index.php?option=com_k…34&id=425&Itemid=442
www.scmgalaxy.com/index.php?option=com_k…34&id=424&Itemid=442
www.scmgalaxy.com/index.php?option=com_k…34&id=423&Itemid=442
www.scmgalaxy.com/index.php?option=com_k…34&id=422&Itemid=442
www.scmgalaxy.com/index.php?option=com_k…34&id=419&Itemid=442
www.scmgalaxy.com/index.php?option=com_k…34&id=421&Itemid=442
www.scmgalaxy.com/index.php?option=com_k…34&id=420&Itemid=442
www.scmgalaxy.com/index.php?option=com_k…34&id=418&Itemid=442
www.scmgalaxy.com/index.php?option=com_k…34&id=431&Itemid=442
www.scmgalaxy.com/index.php?option=com_k…34&id=430&Itemid=442
www.scmgalaxy.com/index.php?option=com_k…34&id=428&Itemid=442
Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn
sribhavani_u replied the topic: Questions to test your linux shell Script
#assignment question1
#You have a number of C programs that certain comment lines at the beginning of each program. The lines begin with /* followed by the first line of comment, butthe terminator line has */ as the only characters in the line. Remove these comments from all files.
for file in *.c
do
echo “file name is $file”
if [ -e $file ]
then
`sed ‘s/[/**/]//g’ $file > newfile`
mv newfile $file
fi
done
=======================================================================
#assignment question1
#You have a number of C programs that certain comment lines at the beginning of each program. The lines begin with /* followed by the first line of comment, butthe terminator line has */ as the only characters in the line. Remove these comments from all files.
for file in test123.c
do
echo “file name is $file”
nooflines=`wc -l $file | awk ‘ { print $1 } ‘`
cat $file | grep -n “^/\*” | awk -F”:” ‘ { print $1 } ‘ | while read strtlineno
do
totlines=`wc -l $file | awk ‘ { print $1 } ‘`
let taillines=$nooflines-$strtlineno
endlineno=`tail -$taillines $file | grep -n “^\*/” | awk -F”:” ‘ { print $1 } ‘ | head -1`
let endlineno=$strtlineno+$endlineno
let difflines=$nooflines-$totlines
let strtlineno=$strtlineno-$difflines
let endlineno=$endlineno-$difflines
echo “$strtlineno $endlineno”
endlineno=`echo $endlineno\d`
sed -i $strtlineno,$endlineno $file
done
done
====================================================================
# assingment question2
#Write a script that compares two direcotries bar1 and bar2 (supplied as arguments) and copies or overwrites to bar1 from bar2 every file that is (i) not present in bar1 or (ii) newer than its namespace in bar1. (HINT: Use the find command)
BAR1=/root/test/bar1/
BAR2=/root/test/bar2/
BAR1_MOD=/root/test/bar1/
cd $BAR2
find . -type f | while read filename
do
newfile=false
modified=false
if [ ! -e “$BAR1$filename” ]; then
newfile=true
echo “ADD $filename”
elif ! cmp $filename $BAR1$filename &>/dev/null; then
modified=true
echo “MOD $filename”
fi
if $newfile || $modified; then
#massage the filepath to not include leading ./
filepath=$BAR1_MOD$(echo $filename | cut -c3-)
#create folder for it if it doesnt exist
destfolder=$(echo $filepath | sed -e ‘s/\/[^\/]*$/\//’)
mkdir -p $destfolder
#copy new/modified file to the upgrade folder
cp $filename $filepath
fi
done
====================================================================
#assingment question 3
#Add the statement #inlclude
egrep -l “printf|fprintf” *.c > new
for filename in `cat new`
do
if [ `grep -c “#include
sed ‘1i\#include
`cat new1>$filename`
rm new1
fi
done
rm new
====================================================================
#assingment question4
#!/bin/bash
#Find out the pathname of the Korn shell on your machine and then change the interpreter line in all shell script in the current directory that show a differentpathname for ksh
dest=`which ksh`
grep -l “^#!.*ksh” *.shh | while read filename
do
cat $filename | grep “^#!.*ksh” | while read src
do
sed -i “s|$src|$dest|g” $filename
done
done
=======================================================================
# assingment question 5
#!/bin/bash
#Write a script that displays a special listing showing the (i) permission (ii) size (iii) filename (iv) last modification time (v) last access time of filenames supplied as arguments. Provide suitable headers using the printf command.
if [ $# -eq 0 ]
then
echo “pass the file name”
exit
fi
divider==================================================
divider=$divider$divider$divider$divider
header=”\n %-15s %15s %15s %40s %40s\n”
format=” %-15s %15d %15s %40s %40s \n”
width=75
printf “$header” “PERMISSION” “SIZE” “FILENAME” “LASTMODTIME” “LASTACCESSTIME”
printf “%$width.${width}s\n” “$divider”
while [ $# -ne 0 ]
do
abc=`stat –format=%A” “%s” “%n” “\”%y\”” “\”%x\” $1 `
shift
printf “$format” \
$abc
done
======================================================================
# assingment question 6
#You are moving files to handled which accepts only 8+3 type filesname. Produce a list of those files in your current directory that fail in this test
#find . -type f ! -name “????????.???”
find . -type f -printf “%f\n” | while read filename
do
bname=`echo $filename | awk -F”.” ‘ { print $1}’ | wc -m`
ename=`echo $filename | awk -F”.” ‘ { print $2}’ | wc -m`
if [ $bname != 9 ] || [ $ename != 4 ]; then
echo “$filename”
fi
done
=======================================================================
#assingment question7
#Expand the scope of the script in 16.12 (Test Your Understanding) to perform search recursively.
======================================================================
#assingment question 8
#Display a process in the system every 30 seconds five times using a(i) while loop (ii) for loop. What is the unusual feature of the for loop?
#using while loop
#i=5
#while [ $i -gt 0 ]
#do
#echo “============”
#ps
#echo “===========”
#sleep 30
#i=$i-1
#done
#using for loop
for i in 1 2 3 4 5
do
echo “*******”
ps
echo “*******”
sleep 30
done
#for loop can not check for condition
=====================================================================
#assignment question 9
#Write a script that behaves both in interactive and noninteractive mode. When no arguments are supplied, it picks up each C program from the current directory and lists the first 10 lines. it then prompts for deletion of the file. if the user supplies arguments with the script, then it works on those files only.
args=$#
if [ $args -eq 0 ]
then
for i in $(find . -name “*.shh”)
do
echo “$(sed -n ‘1,10p’ $i)”
rm -i $i
done
else
for i in $*
do
if [ ! -f $i ]
then
echo “File $i does not exist”
else
echo “$(sed -n ‘1,10p’ $i)”
rm -i $i
fi
done
fi
=====================================================================
# assingment question 10
#Write a script that lists files by modification time when called with lm and by access time when called with la. By default, the script should show the listing of all files in the current directory.
# Listing files by modification time and access time
s=$1
if test “$s” == “lm”
then
ls -lt
elif test “$s” == “la”
then
ls -lu
else
ls -l
fi
====================================================================
# assingment question 11
#Assume that you have a number of files, downloaded from the internet, in the /home/kumar/download directory. The table of contents (TOC) is available in the file TOC_Download.txt in the form filename:description. The script should check each file in the download directory that does not have a description in the TOC file and prompt the user for the description. The TOC should be updated to maintain the list in sorted condition. The script must be immune to signals.
#!/bin/ksh
trap ” INT TERM QUIT
TOC=/root/test/download/contents.txt
find /root/test/download -type f | grep -v $TOC | awk -F/ ‘{ print $NF }’ | sort -u | while read fname
do
if [ `cat $TOC | grep -c “$fname”` == 1 ]; then
echo “file description exists for $fname”
else
echo “update description for $fname: ”
read desc > $TOC
sort -o /tmp/assing11.txt $TOC
cat /tmp/assing11.txt > $TOC
rm -f /tmp/assing11.txt
fi
done
===================================================================
#assingment question 12
#Devise a script that creates a lock file which prevents more than one user from running it. The lock file must be removed before script termination or if the user presses the interrrupt key.
setEnv()
{
LOCK=/tmp/mylock
}
checkLock()
{
if [ -f $LOCK ]; then
echo “Someone is running the script, Hence exiting…”
exit 1;
fi
}
createLock()
{
echo “Creating lock $LOCK”
touch $LOCK
}
doWork()
{
echo “Working”
sleep 10
}
removeLock()
{
echo “Removing lock $LOCK”
rm -f $LOCK
}
capkill()
{
removeLock
exit 1
}
########
# MAIN #
########
trap capkill INT TERM QUIT
setEnv
checkLock
createLock
doWork
removeLock
===================================================================
# assingment qestion 13
#Write a shell script that uses find to look for a file and echo a suitable message if the file is not found. you must not store the find output in a file.
echo “give the name of the file to be searched”
read file
if [ ` find . -type f -iname $file` ];
then
echo “$file exists”
else
echo “File \””$file”\” does not exist.”
fi
chaitu137 replied the topic: Questions to test your linux shell Script
Solution to Question 01
#!/bin/sh
sed -i ‘/\*/{D}’ *.c
- Medical Laboratory equipment manufacturing company - December 20, 2024
- Discover the Heart of India with Mera Apna Bihar - December 20, 2024
- Experience the Pulse of Bangalore with Bangalore Orbit - December 20, 2024