Shell Assign

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Linux Shell - Scripts

1.

Write a shell script to check the number of files and directories in current directory.
echo Enter a Dir Name:
read nm
If [-d $nm]; then
echo The no of files in the directory $nm : $(find $nm type f | wc l)
echo The no of Directoy in $nm : $(find $nm type d | wc l)
else
echo Enter dir name!
exit1
fi

Output :

2. Write a shell script to check whether the database is in OPEN state?


ps ef | grep pmon
echo Enter an instance
read db
export ORACLE_SID=$db
if [ $? ]then
echo Connected to an instance
else
echo Connection failed
fi

exit
3. Write a shell script to check whether the given name is palindrome.
Echo Enter a String :
Read nm
If [ $nm == $rev nm]; then
Echo Given name is a Palindrome !
Else
Echo Not A Palindrome!!
Exit 1
Fi

4. write a shell script that accepts name from user and creates a directory by the path name, then creates a text
file in that directory and stores in it.
echo Enter a Dir.Name to create
read nm
mkdir -p $nm
echo Dir $nm created!
echo Enter a file name to create
read fnm
cd $nm
touch $fnm
echo File $fnm created in $nm !!
exit
Output:

5. How do you refer to the arguments passed to a shell script?


By passing $1,$2 with script name
Output:

6. Whats the conditional statement in shell scripting?


If,then and elseif statements are used in shell script to evaluate the conditions.
7. How do you do number comparison in shell scripts?
Numbers can be compared as follows,
echo #1
read a
echo#2
read b
if (a -gt n)then
echo #1 is greater than #2
else
echo #2 is greater than #1
fi
exit

gt greater than can be replaced by th following,


lt less than
ge greter than or equal to
le - less than or equal to
ee equal to
ne not equal to
8. How do you test for file properties in shell scripts?
-s to check whether the file empty.
-f to check whether it is a file.
-d to check whether it is a directory.
-w r x to check read,write and execute permissions.
9. How do you find out the number of arguments passed to the shell script?
By using $#
10. How do you read keyboard input in shell scripts?
By using read $nm
11. How do you define a function in a shell script?
echo Enter #
read nm
echo Enter 2#
read nmn
n1()
{
echo Fucn 1 called!!
}
n2()
{
echo Fucn 2 called!!
}

if [ $nm == $nmn ] ; then


n1
else
n2
fi
exit
Output:

12. Write a small shell script that adds an extension ".new" to all the files in a directory.?
echo Path
read p
cd $p
for i in *
do
mv $i ${i}.nnn
if [ $? eq 0 ] then
echo Renamed successfully!!
ls -l {$i}
fi
done

Output:

13.In shell scripting How to indentify that the previous command was run successfully?
By using $?

You might also like