Thursday, April 7, 2022

Linux Shell Script Practices

 1. Find unix word in a file and if unix word is found then change the directory to /home/vagrant if not found then print that it is not a unix file.

var1=`grep -i unix seachinfile | wc -l`
func1() {
        cd /home/vagrant
}
if [ $var1 -ge 1 ]
then
        echo "Changing the directory to vagrant dir"
        func1
else
       echo "it is not a unix file"
fi

2. Check that .txt files which are present in a directory if it has then delete those files otherwise print the message No TXT file is found.

var1=`find  /home/vagrant/test *.txt | grep .txt | wc -l`
if [ $var1 -ge 1 ]
then
        rm /home/vagrant/test/*.txt
else
    echo "No file found"
fi


3. Check the current working directory if it is not /home/vagrant then change it to /home/vagrant.

4. Create a function in shell script to print "------------" and call this function multiple times in your script

eg

-----------------------

this is first line

-----------------------

0 comments:

Post a Comment