Unix Examples
Unix Examples
/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
#!/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
fi
-----------------------------------------------------
# sh name.sh babjee reddy
sh name.sh
sh neme.sh one
===================================
#!/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
================================
#!/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
-------------------------------------------------
#!/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
--------
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
===========================
#!/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
# 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[@]}"