You are on page 1of 3

Date - 08.01.

22

Shell scipring examples===============


[root@localhost Desktop]# vim s1.sh
#!/bin/sh

echo "What is your name?"


read Person
echo "Hello, $Person"

To run any script file=================


[root@localhost Desktop]# sh s1.sh [s1.sh is the script file]
Sahil
Hello, Sahil

Another programming===============
[root@localhost Desktop]# vim s2.sh
#!/bin/bash

clear
((sum=25+35))

echo $sum
:wq!

[root@localhost Desktop]# sh s2.sh


60

Loop programming=================
[root@localhost Desktop]# vim s3.sh
#!/bin/bash
valid=true
count=1
while [ $valid ]
do
echo $count
if [ $count -eq 5 ];
then
break
fi
((count++))
done
:wq!

For Loop======================
[root@localhost Desktop]# vim s4.sh
#!/bin/bash/
for (( counter=10; counter>0; counter-- ))
do
echo -n "$counter "
done
printf "\n"
:wq!

If statement==============
[root@localhost Desktop]#
#!/bin/bash/
n=10
if [ $n -lt 10 ];
then
echo "It is a one digit number"
else
echo "It is a two digit number"
fi
:wq!

[root@localhost Desktop]# vim s6.sh


#!/bin/bash

echo "Enter username"


read username
echo "Enter password"
read password

if [[ ( $username == "admin" && $password == "secret" ) ]]; then


echo "valid user"
else
echo "invalid user"
fi
:wq!
[root@localhost Desktop]# sh s6.sh
Enter username
admin
Enter password
secret
valid user

Another if statement============
[root@localhost Desktop]#
#!/bin/bash

echo "Enter any number"


read n

if [[ ( $n -eq 15 || $n -eq 45 ) ]]
then
echo "You won the game"
else
echo "You won the game"
fi
:wq!
[root@localhost Desktop]# sh s7.sh
Enter any number
15
You won the game

Another one==========
[root@localhost Desktop]# vim s8.sh
#!/bin/bash

echo "Enter your lucky number"


read n

if [ $n -eq 101 ];
then
echo "You got 1st prize"
elif [ $n -eq 510 ];
then
echo "You got 2nd prize"
elif [ $n -eq 999 ];
then
echo "You got 3rd prize

else
echo "sorry, try for the next time"
fi
:wq!
[root@localhost Desktop]# sh s8.sh
Enter your lucky number
101
You got first prize

For execute a poweroff or reboot==============


[root@localhost Desktop]# whereis reboot
reboot: /usr/sbin/reboot - copy /usr/sbin/reboot and paste it on script to run the
command
[root@localhost Desktop]# vim s9.sh
#!/bin/bash

/usr/sbin/reboot
:wq!

You might also like