KEMBAR78
Linux Shell Scripts and Shell Commands✌️ | PPTX
Shell Commands
and
Shell Scripts
Presented By:
Nazmul Hyder
ID- 011 131 085
Shell Command
Identity and Manual
Command Description
$uame Linux will tell you his name to you
$whoami If you want to know your name
$man uname For each command manual
Continue..
Editors
Command Description
$ kwrite File write
$gedit File Edit
$vi
Continue..
File Listing
Command Description
$ls -a Do not hide entries starting with.
$ls -h Print size in human readable format
$ls -l Use a long listing format
$ls -s Sort by file size
Continue..
Directory Operations
Command Description
$pwd If u want to see your current directory
$cd “directory name” To change the directory
$mkdir “directory name” Create a new directory
$rmdir “directory name” Remove an empty directory
$rmdir -r “directory name” Remove directory with file
Continue..
File Operations
Command Description
$cp “file” “copyfile.txt” Copy file
$rm “file.txt” To remove file
$mv “file.txt “ “directory name” Create a new directory
$file -i “file name” To know the file type
$file -v “file name” Print the version of the file
$cat “file1” “file2” To concat files and print on the
standard output
Continue..
File Viewing
Command Description
$echo “some text” You can a line of text in the shell
$more file.txt It uses for the Spacebar and the B key for
the forward and backward navigation
$less “file.txt” Less allows forward and backward
movement using arrow keys
$head filename To see the first 10 contents of the file
$tail filename To see the last 10 contents of the file
Continue..
File Viewing
Command Description
$filename -n To print the line number of the file
$sort file1 file2 To write the sorted concatenation of all
the file and standard output
$wc filename Print bytes,words,total lines
$wc -l or -w You can see specific
Continue..
Piping and Grep
Command Description
$head -10 a.txt | tail -5 To see 5-10 number lines of a file
$grep “a” file.txt Print lines which contains “a”
$grep -i For case insensitive search
$grep -c Count total matches
$grep -n Display the line number of matches
Continue..
Grep
Command Description
$grep -E [^F] filename Anything except capital F
$grep -E [$a] filename Anything that ends with a
$grep -E [a-zA-Z] filename Anything that contains small a to z and
capital A to Z
Shell Script
Advantage of shell script :
● It take input from user , file and output them on screen.
● Useful to create own commands.
● Save time.
Continue..
Write and Execute :
● We can use any editor to write the shell script.
● The extension is .sh
● Two thing you can need to do before execute file
○ Chmod +x script_name “set execute permission for your script”
○ ./script_name “execute script”
Continue..
Shell Script Examples
Arithmetic if-else Case
#!/bin/bash
expr 1 + 1
expr 1 ‐1
expr 1 * 1
expr 1 / 1
var=`expr 1 + 1`
x=1
x=`expr $x + 1`
#!/bin/bash
echo "Enter first number "
read num1
echo "Enter second number"
read num2
if [ $num1 ‐gt $num2 ] ; then
echo
"$num1 is greater than $num2"
elif [ $num1 ‐lt $num2 ] ; then
echo "$num1 is less than $num2"
else
echo "$num1 and $num2 are equal"
fi
#!/bin/sh
echo “Is it morning? Please answer
yes or no”
read timeofday
case “$timeofday” in
yes) echo “Good Morning”;;
no ) echo “Good Afternoon”;;
y ) echo “Good Morning”;;
n ) echo “Good Afternoon”;;
* ) echo “Sorry, answer not
recognized”;;
esac
Thank You

Linux Shell Scripts and Shell Commands✌️

  • 1.
    Shell Commands and Shell Scripts PresentedBy: Nazmul Hyder ID- 011 131 085
  • 2.
    Shell Command Identity andManual Command Description $uame Linux will tell you his name to you $whoami If you want to know your name $man uname For each command manual
  • 3.
    Continue.. Editors Command Description $ kwriteFile write $gedit File Edit $vi
  • 4.
    Continue.. File Listing Command Description $ls-a Do not hide entries starting with. $ls -h Print size in human readable format $ls -l Use a long listing format $ls -s Sort by file size
  • 5.
    Continue.. Directory Operations Command Description $pwdIf u want to see your current directory $cd “directory name” To change the directory $mkdir “directory name” Create a new directory $rmdir “directory name” Remove an empty directory $rmdir -r “directory name” Remove directory with file
  • 6.
    Continue.. File Operations Command Description $cp“file” “copyfile.txt” Copy file $rm “file.txt” To remove file $mv “file.txt “ “directory name” Create a new directory $file -i “file name” To know the file type $file -v “file name” Print the version of the file $cat “file1” “file2” To concat files and print on the standard output
  • 7.
    Continue.. File Viewing Command Description $echo“some text” You can a line of text in the shell $more file.txt It uses for the Spacebar and the B key for the forward and backward navigation $less “file.txt” Less allows forward and backward movement using arrow keys $head filename To see the first 10 contents of the file $tail filename To see the last 10 contents of the file
  • 8.
    Continue.. File Viewing Command Description $filename-n To print the line number of the file $sort file1 file2 To write the sorted concatenation of all the file and standard output $wc filename Print bytes,words,total lines $wc -l or -w You can see specific
  • 9.
    Continue.. Piping and Grep CommandDescription $head -10 a.txt | tail -5 To see 5-10 number lines of a file $grep “a” file.txt Print lines which contains “a” $grep -i For case insensitive search $grep -c Count total matches $grep -n Display the line number of matches
  • 10.
    Continue.. Grep Command Description $grep -E[^F] filename Anything except capital F $grep -E [$a] filename Anything that ends with a $grep -E [a-zA-Z] filename Anything that contains small a to z and capital A to Z
  • 11.
    Shell Script Advantage ofshell script : ● It take input from user , file and output them on screen. ● Useful to create own commands. ● Save time.
  • 12.
    Continue.. Write and Execute: ● We can use any editor to write the shell script. ● The extension is .sh ● Two thing you can need to do before execute file ○ Chmod +x script_name “set execute permission for your script” ○ ./script_name “execute script”
  • 13.
    Continue.. Shell Script Examples Arithmeticif-else Case #!/bin/bash expr 1 + 1 expr 1 ‐1 expr 1 * 1 expr 1 / 1 var=`expr 1 + 1` x=1 x=`expr $x + 1` #!/bin/bash echo "Enter first number " read num1 echo "Enter second number" read num2 if [ $num1 ‐gt $num2 ] ; then echo "$num1 is greater than $num2" elif [ $num1 ‐lt $num2 ] ; then echo "$num1 is less than $num2" else echo "$num1 and $num2 are equal" fi #!/bin/sh echo “Is it morning? Please answer yes or no” read timeofday case “$timeofday” in yes) echo “Good Morning”;; no ) echo “Good Afternoon”;; y ) echo “Good Morning”;; n ) echo “Good Afternoon”;; * ) echo “Sorry, answer not recognized”;; esac
  • 14.