Its like a container.
interface between users and kernal / OS.
CLI is a shell
Find your shell:
echo $0: gives the output which shell you are using. cat /etc/shells: gives the output of available shells.
your shell? /etc/passwd
.................................
Shell Scripting:
What is shell Script?
Shell Script is the executable file containing multiple shell commands that are executed sequentially. The file can contain:
1. shell
2. comments
3. commands
4. statements
shell script should have executable permissions.
shell script has to be called absolute path.
if it is called from the current location then, ./script.sh
.....................................
Shell Script-Basic Scripts:
Question: Write the script to print "hello world" on the terminal. Answer:
content of script1.sh should be:
#!/bin/bash
echo Hello World
chmod a+x script1.sh: give the executible permission to all for this script.
/script1.sh: run the script.
....,......................
Question: Defining Tasks
#!/bin/bash
# Define small tasks
whoami
echo #echo command will give the space in the output.
pwd
echo
hostname
echo
Is-ltr
echo
............................
Question: Defining variables
#!/bin/bash
# Example of defining variables
a=Imran
b=Afzal
c='Linux class'
echo "My first name is $a"
echo "My surname is $b"
echo 'My class is $c'
......................................
Question: Read Input
#!/bin/bash
# Read user input
echo "What is your first name?"
read a
echo
echo "What is your last name?"
read b
echo
echo Hello $a $b
...........................
Question: Scripts to run commands within.
#!/bin/bash
#Script to run commands within
clear
echo "Hello `whoami`"
echo
echo "Today is `date`"
echo
echo "Number of user login: `who | wc -1`"
echo
..................
Question : rename the filename.
#!/bin/bash
#This script will rename a file
echo Enter the file name to be renamed
read oldfilename
echo Enter the new file name
read newfilename
mv $oldfilename $newfilename
echo The file has been renamed as $newfilename
.......,....................
If-then Scripts:
Question: Check the variable
#!/bin/bash
count=100
if [ $count -eq 100 ]
then
echo Count is 100
else
echo Count is not 100
fi
***********************************
Question: Check if a file error.txt exist
#!/bin/bash
clear
if [ -f /home/iafzal/error.txt ]
then
echo "File exist"
else
echo "File does not exist "
fi
†********************************
Question:Check if a variable value is met
#!/bin/bash
a=`date | awk '{print $1}'`
if [ "$a" == Mon]
then
echo Today is $a
else
echo Today is not Monday
fi
************************************
Question: Check the response and then output
#!/bin/bash
clear
echo
echo "What is your name?"
echo
read a
echo
echo Hello $a sir
echo
echo "Do you like working in IT? (y/n)"
read Like
echo
if [ "$Like" == y ]
then
echo You are cool
elif [ "$Like" == n]
then
echo You should try IT. it's a good field
echo
fi
******************************
For loop Scripts:
Simple for loop output
#!/bin/bash
for i in 1 2 3 4 5
do
echo "Welcome $i times"
done
***********************************
Simple for loop output
#!/bin/bash
for i in eat run jump play
do
echo See Imran $i
done
*************************************
Question:
for loop to create 5 files named 1-5
#!/bin/bash
for i in (1..5)
do
touch $i
done
************************************
Question:
for loop to delete 5 files named 1-5
#!/bin/bash
for i in (1..5)
do
rm $i
done
**************************************
Specify days in for loop
#!/bin/bash
i=1
for day in Mon Tue Wed Thu Fri
do
echo "Weekday $((i++)): $day"
done
*******************************†******
List all users one by one from /etc/passwd file
#!/bin/bash
i=1
for username in `awk -F: '{print $1)' /etc/passwd
do
echo "Username $((i++)): $username"
done
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Comments
Post a Comment