0% found this document useful (0 votes)
86 views8 pages

Unix Examples

The document contains several shell script examples demonstrating various shell scripting concepts like: 1) Printing text using echo; 2) Comments; 3) User input using read; 4) Arithmetic operations; 5) Conditional statements like if-else; 6) Loops like for and while; 7) Functions; 8) Arrays.

Uploaded by

babjeereddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views8 pages

Unix Examples

The document contains several shell script examples demonstrating various shell scripting concepts like: 1) Printing text using echo; 2) Comments; 3) User input using read; 4) Arithmetic operations; 5) Conditional statements like if-else; 6) Loops like for and while; 7) Functions; 8) Arrays.

Uploaded by

babjeereddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

#!

/bin/sh
echo helow world

=======================================
#!/bin/sh
# this is comment
echo Hellow world # this is comment too

=========================================
#!/bin/sh
echo "Enter Your Name :"
read name
echo "hi $name"

---------------------------------------
#!/bin/sh

Echo Enter first no:


read num1
echo Enter second no:
read num2

echo $num1 + $num2 = `expr $num1 + $num2`


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

#!/bin/sh
echo "Hello World"
echo "Hello * World"
echo Hello * World
echo Hello World
echo "Hello" World
echo Hello$'\t'World
echo Hello$'\n'World
-----------------------------------------
#!/bin/sh
MY_MESSAGE="Hello World"
MY_SHORT_MESSAGE=hi
MY_NUMBER=1
MY_PI=3.142
echo $MY_MESSAGE
echo $MY_SHORT_MESSAGE
echo $MY_NUMBER
echo $MY_PI
----------------------------------------------
#!/bin/sh
echo "What is your name?"
read PERSON
echo "Hello, $PERSON"
======================
#!/bin/sh

if [ $# -lt 2 ]
then
echo minumum 2 arg needed
else

echo "My first name is $1"


echo "My surname is $2"
echo " file name is $0"
echo "Total number of arguments is $#"

fi
-----------------------------------------------------
# sh name.sh babjee reddy

sh name.sh

sh neme.sh one

sh name.sh one two three four five six seven

===================================
#!/bin/sh
echo "File Name: $0"
echo "First Parameter : $1"
echo "Second Parameter : $2"
echo "Third Parameter: $3"
echo "Quoted Values: $@"
echo "Quoted Values: $*"
echo "Total Number of Parameters : $#"
echo "exit status of the command : $?"
----------------------------------------------------
#sh filename.sh Jay vibhava

sh filename.sh one two three

sh filename.sh one two three four five six

================================
#!/bin/sh
for i in 1 2 3 4 5
do
echo $i
done
--------------------------------
#!/bin/sh
for i in {1..10}
do
echo $i
done
----------------------------

#!/bin/sh
for i in {1..10..2}
do
echo $i
done

--------------------------------
#!/bin/sh
for TOKEN in $*
do
echo $TOKEN
done
# sh name.sh hi ram welcome to unix world

---------------------------------------
#!/bin/sh

a=0

while [ $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done

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

#!/bin/sh
while [ "$*" != "" ]
do
echo "value is $1"
shift #shift parameter to left
done
---------------------------------

-------------------------------
#!/bin/sh

a=0
while [ "$a" -lt 10 ] # this is loop1
do
b="$a"
while [ "$b" -ge 0 ] # this is loop2
do
echo -n "$b "
b=`expr $b - 1`
done
echo
a=`expr $a + 1`
done

-------------------------------
========================
#!/bin/sh
echo " enter two numbers"
read n1
read n2
if [ $n1 -eq $n2 ]
then
echo " numbers are same "
else
echo " numbers are not equal"
fi
================================
echo enter num1:
read num1;
echo enter num2:
read num2
echo chose operator
echo "1) '+'"
echo "2) '-'"
echo "3) '*'"
echo "4) '/'"
echo select choice
read choice

if [ $choice -eq 1 ]
then
result=`expr $num1 + $num2`
elif
[ $choice -eq 2 ]
then

result=`expr $num1 - $num2`


elif
[ $choice -eq 3 ]
then

result=`expr $num1 \* $num2`


else
result=`expr $num1 / $num2`
fi

echo result is $result

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

#!/bin/sh
if [ -d $1 ]
then
ls -l $1

elif [ -f $1 ]
then
cat $1
else
echo "no file of folder found "
fi
---------------------------------
sh file.sh demo
sh file.sh fruit1
sh file.sh abcd

--------

echo "enter the folder name to create"


read fname

if [ ! -d $fname ]
then
mkdir $fname
else
echo "$fname already exit"
fi

=================================================
#!/bin/sh
if [ -d $1 ]
then
ls $1
else
cat $1
fi

sh sixth.sh test
sh sixth.sh file4.txt
==========================================
#!/bin/sh

if [ "$1" ]
then
echo "Found an argument to this script"
if [ $1 = "fred" ]
then
echo "The argument was fred!"
else
echo "The argument was not fred!"
fi
else
echo "This script needs one argument"
fi

sh xyz.sh fred
sh xyz.sh abcd
sh xyz
==========================================
!/bin/sh

echo "Enter username"


read username
echo "Enter password"
read password
if [ $username == "admin" && $password == "secret" ]
then
echo "valid user"
else
echo "invalid user"
fi
=================================================================
#!/bin/sh
echo "Enter any number"
read n
if [ [ ( $n -eq 15 || $n -eq 45 ) ] ]
then
echo "You won the game"
else
echo "You lost the game"
fi

===========================
#!/bin/sh

case "$1" in
fred)
echo "Hi fred"
;;
joe)
echo "hi joe?"
;;
harry)
echo "hai harry!"
;;
*)
echo "Who are you?"
;;
esac
==================================

#!/bin/bash
# copy a file, creating a backup if the target file exists
if [ $# -lt 2 ]
then
echo "Usage: $0 fromfile tofile"
exit 1
fi
if [ -f $2 ]
then mv $2 $2.bak
fi
cp $1 $2
===================================
#!/bin/sh
case "$1" in
fred)
echo "Hi fred. Nice to see you"
;;
joe)
echo "Oh! Its you, is it, joe?"
;;
harry)
echo "Clear off!"
;;
*)
echo "Who are you?"
;;
esac

===================================
#!/bin/sh

echo "Welcome to the DOS interpreter"


echo "Type Ctrl-C to exit"

# Infinite loop
while [ "forever" ]
do
# Show DOS prompt; \c stops a new line from being issued
echo "DOS> \c"
# Read in user's command
read command arg1 arg2
# Do a UNIX command corresponding to the DOS command
case $command in
cd)
cd $arg1
;;
dir)
ls
;;
type)
cat $arg1
;;
del)
rm $arg1
;;
ren)
mv $arg1 $arg2
;;
copy)
cp $arg1 $arg2
;;
*)
echo "DOS does not recognise the command $command"
;;
esac
done
=======================================

======================

#!/bin/sh
#defining function
hello()
{
echo "Hello World"
}
#calling function

hello

---------------------------------------
#!/bin/sh
a=$1
print_msg(){
echo "Hello $a"
}

print_msg
------------------------------------------
#!/bin/sh
a=20
b=30
add(){
c=`expr $a + $b`
echo $c
}

add

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

#!/bin/sh

NAME[0]="Zara"
NAME[1]="Qadir"
NAME[2]="Mahnaz"
NAME[3]="Ayan"
NAME[4]="Daisy"
echo "First Index: ${NAME[0]}"
echo "Second Index: ${NAME[1]}"

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

#!/bin/sh

NAME[0]="Zara"
NAME[1]="Qadir"
NAME[2]="Mahnaz"
NAME[3]="Ayan"
NAME[4]="Daisy"
echo "First Method: ${NAME[*]}"
echo "Second Method: ${NAME[@]}"

You might also like