Shell Scripts
Shell Scripts
clear
read filename
if test -e $filename
then
else
fi
Ans.
who ; ls
man cat
Ans.
clear
echo “5. Explain two functionality of “mv” command with example” echo “enter your choice”
read ch
case $ch in
ls .[a-z]* ;;
rm -R dirname
cp -i file1 file2 ;;
rm -i file1 ;;
mv oldfilename newfilename ;;
esac
Ans.
clear
echo “1. Create a file called text and store name,age and address in it.”
echo “2. Display the contents of the file text on the screen.”
echo “3. Delete the directories mydir and newdir at one shot.”
echo “5. Change the permissions for the file newtext to 666.”
read ch
case $ch in
1)echo “Create a file called text and store name,age and address in ”
read fn
cat $fn ;;
sort -n filename ;;
esac
if [ -e $fn ]
then
else
fi
who | grep ^s
for i in *
do
if [ ! -s $i ]
then
rm $i
echo ” $i removed ”
fi
done
OR
for i in *
do
if [ -x $i ] then
countx=`expr $countx + 1`
echo $i
fi
d=`date +”%H”`
if [ $d -lt 12 ]
then
then
else
fi
for i in *
do
if [ -d $i ]
then
countd=`expr $countd + 1`
fi
if [ -f $i ]
then
countf=`expr $countf + 1`
fi
done
If it is not an ordinary file then script should display the message: “ File does not exist or is not
ordinary, cannot display. “
Ans.
if [ -f $1 ]
then
cat $1
else
fi
If it is not a directory file then script should display the message: “File is not a directory file"
Ans:
read fn
if [ -d $fn ]
then
ls $fn
else
fi
If the file does not exists and is not executable then the script should display the message: “File does not exist or is not
”
Ans.
echo “enter the filename”
read fn
if [ -e $fn -a -x $fn ]
then
fi
for i in *
do
if [ -d $i ]
then
echo $i
fi
done
OR
for i in *
do
if [ -d $i ]
then
countd=`expr $countd + 1`
fi
if [ -f $i ]
then
countf=`expr $countf + 1`
fi
done
read fn1
read fn2
if [ -f $fn1 -a -f $fn2 ]
then
else
fi
Ans.
cat $1
cat $1 | more
cp $1 $2
c=`echo $?`
if [ $c -eq 0 ]
then
else
fi
Ans:
clear
echo “Menu”
echo “2. Display “Good Morning/Good Afternoon/Good Evening” according to the current login time.”
echo “3. User name, Users home directory.” echo “4. Terminal name, Terminal type.”
echo “6. No. of users who are currently logged in; List of users who are currently logged in.”
read ch
case $ch in
cal ;;
2) d=`date +”%H”`
if [ $d -lt 12 ]
then
then
else
fi
esac
Ans.
echo “4. Copy the file to specific location” echo “enter your choice”
read ch
case $ch in
read str1
read str2
read ofn
read nfn
mv $ofn $nfn
read fn
rm $fn
read fn
cp $fn \usr\home\dir\$fn
esac
Write a shell script to change the
suffix of all your *.txt files to .dat.
Ans.
suffix=”.dat”
for i in `ls *.txt`
do
name=`basename $i .txt`
mv $i $name$suffix
done
if [ $# ]
then
ls $1
else
ls $HOME
fi
To execute do as below
suffix=111
for f in `ls *`
do
echo filename $f
extension=${f##*.}
echo $extension
name=`basename $f .$extension`
if test -z $extension
then
mv $f $f$suffix
else
mv $f $name$suffix”.”$extension
fi
done
read fn1
read fn2
cmp $fn1 $fn2
c=`echo $?`
if [ $c -eq 0 ]
then
rm $fn2
else
fi
Once both exist copy all the files from source directory to destination directory.
Ans.
if [ $# ]
then
if [ -d $1 ]
then
if [ -d $2 ]
then
cp -R $1 $2 else
mkdir $2
fi
else
echo “source directory does not exist”
fi
else
fi
• Date
• Users logged in
Read the proper choice. Execute corresponding command. Check for invalid choice.
Ans.
echo “1.List home directory”
echo “2.Date”
read ch
case $ch in
esac
OR
ls .[a-z]*
read fn1
read fn2
read datafile
d=`date +%u`
if [ $d –eq 7]
then
else
fi
read w
for i in $@
do
grep $w $i
done
read fn
if [ -r $fn ]
then
fi
if [ -x $fn ]
then
fi
if [ -w $fn ]
then
fi
OR
ii. Display the calendar of the months from m1 to m2 by ‘CAL m1-m2’ command file.
Ans.
m1=$1
y1=$2
wat=$3
m2=$4
y2=$5
then
then
do
read a
then
exit
fi
m1=`expr $m1 + 1`
if [ $m1 -gt 12 ]
then
m1=1
y1=`expr $y1 + 1`
done
else
fi
fi
Ans.
case $ch in
sort $fn;;
sort -r $fn;;
4) file $fn;;
esac
if [ $c -gt 0 ]
then
else
fi
read ch
read fn
case $ch in
1)sort $fn ;;
2)sort -r $fn ;;
3)rev $fn
5) file $fn
*) echo “Invalid choice”
esac
cat etc/passwd | wc -l
OR
x=`ls -l | wc -l`
i=2
echo “Filename\t\t\tSize\tDate\tProtection\tOwner\n”
while [ $i -le $x ]
do
fn=`echo $s | cut -d ” ” -f 9`
si=`echo $s | cut -d ” ” -f 5`
d1=`echo $s | cut -d ” ” -f 6`
d2=`echo $s | cut -d ” ” -f 7`
p=`echo $s | cut -d ” ” -f 1`
o=`echo $s | cut -d ” ” -f 3`
done
OR
echo “`ls -l $fn | cut -d ‘ ‘ -f3` ` ls -l $fn | cut -d ‘ ‘ -f5` `ls -l $fn | cut -d ‘ ‘ -f6,7`
`ls -l $fn | cut -d ‘ ‘ -f1` `ls -l $fn | cut -d ‘ ‘ -f4` “
Ans.
read fn
read un
if [ $c -gt 0 ]
then
else
fi
ls | grep ^$1
# run the shell script as: sh filename.sh a # it will list files starting with a
Sort file according to the second field and save it to file ‘names’.
Ans.
read fn
echo “1. Display the contents of the file sorted by marks in descending order”
echo “2. Display the names of students in alphabetical order ignoring the case.”
echo “4. Sort file according to the second field and save it to file ‘names’.”
echo “5. Display the list of students who scored between 70 and 80”
read ch
case $ch in
3)sort $fn ;;
esac
The table below lists some of the most used file management commands File/directory permissions and groups
Command Utility
chmod <specification> filename Change the file permissions. Specifications = u user, g group, o other, + add
permission, – remove, r read, w write,x execute.
Change the permissions of a directory recursively. To change permission of a directory and everything within that
directory, use this command.
chmod go=+r myfile Add read permission for the owner and the group. chmod a +rwx myfile Allow all users to read,
write or execute myfile. chmod go -r myfile Remove read permission from the group and others. chown owner1 filename
Change ownership of a file to user owner1.
chgrp grp_owner filename Change primary group ownership of file filename to group grp_owner.
Change primary group ownership of directory dir-name to group grp_owner recursively. To change group ownership of a
directory and everything within that directory, use this command.
Type the following code into your terminal, then press Enter :
Hello World
Linux has a command for almost any tasks and most of them are intuitive and easily interpreted. Getting Help in Linux
Command Usability
man <section> <name> Read the manual page of <name>, related to the given section. man -k <editor> Output all the
software whose man pages contain <editor> keyword. man -K <keyword> Outputs all man pages containing <keyword>
within them.
apropos <editor> Output all the applications whose one line description matches the word ditor. When not able to recall
the name of the application, use this command.
help In Bash shell, this will display the list of all available bash commands.
help <name> In Bash shell, this will display the info about the <name> bash command. info <name> View all the
information about <name>.
dpkg -L packageName Will list out the files installed and path details for a given package on Debian.
dpkg -l | grep -i <edit> Return all .deb installed packages with <edit> irrespective of cases. less /var/lib/dpkg/available
Return descriptions of all available packages.
<command-name> –help Display usage information about the <tool-name>. Sometimes command -h also works, but not
for all commands.
User identification and who is who in Linux world Command Usability hostname Display hostname of the system.
hostname -f Displays Fully Qualified Domain Name (FQDN) of the system. passwd Change password of current user.
whoami Username of the users logged in at the terminal. who List of all the users currently logged in as a user.
w Display current system status, time, duration, list of users currently logged in on system and other user information.
last root When was the last time root logged in as user. lastb Shows all bad login attempts into the system.
top List all processes sorted by their current system resource usage. Displays a continually updated display of
processes (By default 3 seconds). Use q key to exit top.
ps -u root List all of the processes and commands root is running ps aux List all the processes by all users on the
current system
A common and task of someone using the Linux Command Line (shell) is to search for files/directories with a certain
name or containing certain text. There are 2 commands you should familiarise yourself with in order to accomplish this:
This will print out the full path/filename to all files under /var/www that end in .css. Example output:
/var/www/html/text-cursor.css
/var/www/html/style.css
man find
This will print all lines containing the pattern font in the specified file. Example output: font-weight: bold;
GoalKicker.com – Linux® Notes for Professionals 7 This doesn’t work as you’d hoped. You get:
Notice that when grep is matching multiple files, it prefixes the matched lines with the filenames. You can use the –
man grep
Files and directories (another name for folders) are at the heart of Linux, so being able to create, view, move, and
delete them from the command line is very important and quite powerful. These file manipulation commands allow you
to perform the same tasks that a graphical file explorer would perform.
touch myFile
cat myFirstFile
View the content of a file with pager (one screenful at a time): less myFirstFile
head myFirstFile
ls
mkdir myFirstDirectory
Create multi path directory: (creates two directories, src and myFirstDirectory) mkdir -p src/myFirstDirectory
Move the file into the directory:
rm myFirstFile
cd ..
rmdir myFirstDirectory
Delete a non-empty directory (i.e. contains files and/or other directories): rm -rf myFirstDirectory
Make note that when deleting directories, that you delete ./ not / that will wipe your whole filesystem.
The ls command has several options that can be used together to show more information. Details/Rights
The l option shows the file permissions, size, and last modified date. So if the root directory contained a dir called test
and a file someFile the command:
The permissions are in format of drwxrwxrwx. The first character represents the file type d if it’s a directory -otherwise.
The next three rwx are the permissions the user has over the file, the next three are the permissions the group has over
the file, and the last three are the permissions everyone else has over the file.
The r of rwx stands for if a file can be read, the w represents if the file can be modified, and the x stands for if the file
can be executed. If any permission isn’t granted a – will be in place of r, w, or x.
So from above user can read and modify someFile.txt but the group has only read-only rights.
To change rights you can use the chmod ### fileName command if you have sudo rights. r is represented by a value of
4, w is represented by 2, and x is represented by a 1. So if only you want to be able to modify the contents to the test
directory
Used in conjunction with the l option the h option shows file sizes that are human readable.
Running
total 4166
Might list
To view the size of the current directory use the s option (the h option can also be used to make the size more
readable).
user@linux-computer:~$ ls -s Outputs