1. Analog Linnux Login Shell
#/bin/bash
Echo-n "Login:"
Read name
Echo-n "Password:"
Read passwd
if [$name = "CHT"-a $passwd = "abc"];then
echo "The host and password is right!"
else echo "Input is error!"
Fi
2. Compare two number sizes
#/bin/bash
echo "Please enter the number"
Read a
Read B
if test $a-eq $b
Then echo "The No.2"
Elif Test $a-gt $b
Then echo "The > no.2"
else echo "The < No.2"
Fi
3. Find out if the file exists under the/root/directory
#/bin/bash
echo "Enter a file name:"
Read a
If TEST-E/root/$a
Then echo "The file is exist!"
else echo "The file is not exist!"
Fi
Use of 4.for loops
#/bin/bash
Clear
For NUM in 1 2 3 4 5 6 7 8 9 10
Do
echo "$num"
Done
5. Command line Input
#/bin/bash
echo "Please enter a User:"
Read a
b=$ (WhoAmI)
if test $a = $b
Then echo "The user is running."
else echo "The user is not running."
Fi
6. Delete the file size 0 in the current directory
#/bin/bash
for filename in ' ls '
Do
If test-d $filename
Then b=0
Else
a=$ (ls-l $filename | awk ' {print $} ')
if test $a-eq 0
Then RM $filename
Fi
Fi
Done
7. If there is a file under/export/um_lpp_source, change its file system size to 3G
#/bin/bash
While Line= ' Ls/export/um_lpp_source '
Do
if test $line = ""
Then echo "NULL"
Sleep 1
else Echo $line
Chfs-a Size=3g/export/um_lpp_source
Exit 0
Fi
Done
8. Test the IP address
#/bin/bash
For I in 1 2 3 4 5 6 7 8 9
Do
echo "The number of $i computer is"
Ping-c 1 192.168.0. $i
Done
9. If the size of the Test.log is greater than 0, then the *.tar.gz file in the/opt directory
#/bin/sh
a=2
While Name= "Test.log"
Do
Sleep 1
b=$ (ls-l $name | awk ' {print $} ')
if test $b-ge $a
#then echo "OK"
Then ' cp/opt/*.tar.gz. '
Exit 0
Fi
Done
10. Print the contents of the read, prepare for the following example
#/bin/bash
While read name
Do
Echo $name
Done
11. Read content from 0.sh and print
#/bin/bash
While Read line
Do
Echo $line
Done < 0.sh
12. Read the contents of the A.C and add 1 operations
#/bin/bash
TEST-E A.C
While Read line
Do
a=$ (($line + 1))
Done < A.C
Echo $a
13. Ordinary parameterless function
#/bin/bash
P ()
{
echo "Hello"
}
P
14. Passing parameters to a function
#/bin/bash
P_num ()
{
Num=$1
Echo $num
}
for n in [email protected]
Do
P_num $n
Done
15. Create a Folder
#/bin/bash
While:
Do
echo "Please input file ' s name:"
Read a
If TEST-E/root/$a
Then
echo "The file is existing." Please input new file name: "
Else
mkdir $a
echo "You aye sussesful!"
Break
Fi
Done
16. Get the native IP address
#/bin/bash
Ifconfig | grep "inet Addr:" | awk ' {print $} ' | Sed ' s/addr://g '
17. Find the largest file
#/bin/bash
A=0
For name in *. *
Do
b=$ (ls-l $name | awk ' {print $} ')
if test $b-ge $a
Then a= $b
namemax= $name
Fi
Done
echo "The max file is $namemax"
18. Locate the IP user in the current segment and redirect to the Ip.txt file
#/bin/bash
A=1
While:
Do
a=$ (($a + 1))
if test $a-GT 255
Then break
Else
echo $ (ping-c 1 192.168.0. $a | grep "TTL" | awk ' {print $4} ' | sed ' s/://g ')
ip=$ (ping-c 1 192.168.0. $a | grep "TTL" | awk ' {print $4} ' | sed ' s/://g ')
echo $ip >> Ip.txt
Fi
Done
19. Print the current user
#/bin/bash
echo "Current User is:"
echo $ (PS | grep "$$" | awk ' {print $} ')
20.case Statement Exercise
#!/bin/bash
Clear
echo "Enter a number from 1 to 5:"
Read num
Case $num in
1) echo "You enter 1"
;;
2) echo "You enter 2"
;;
3) echo "You enter 3"
;;
4) echo "You enter 4"
;;
5) echo "You enter 5"
;;
*) echo "error"
;;
Esac
21.yes/no return to different structures
#!/bin/bash
Clear
echo "Enter [y/n]:"
Read a
Case $a in
y| y| yes| YES) echo "You enter $a"
;;
n| N|no|no) echo "You enter $a"
;;
*) echo "error"
;;
Esac
22. Killing process
#/bin/bash
Pid= ' Ps-ef | grep ' process-related content ' | Grep-v ' grep ' | awk ' {print $} '
If [-N "$pid"]; Then
Kill-9 $pid
Fi
23. Use of built-in commands
#/bin/bash
Clear
echo "Hello, $USER"
Echo
echo "Today ' s date id ' date '"
Echo
echo "The user is:"
W.H.O.
Echo
echo "This is ' uname-s '"
Echo
echo "That's all folks!"
24.
25.
#/bin/bash
26. Print user without password
#/bin/bash
echo "No Password User is:"
echo $ (Cat/etc/shadow | grep "!!" | awk ' BEGIN {fs= ":"}{print $} ')
27.
#/bin/bash
Clear
echo "Hello, $USER"
Echo
echo "Today ' s date id ' date '"
Echo
echo "The user is:"
W.H.O.
Echo
echo "This is ' uname-s '"
Echo
echo "That's all folks!"
28. Check if the port number is started
#!/bin/bash
N=1
echo "Check XXX service ..."
While True
Do
if test $n-gt 20
Then
echo "XXX service failed to start"
Break
Fi
Sleep 5
n=$ (($n + 1))
Port= ' NETSTAT-ANTP | grep "0.0.0.0:8080" '
If [${#port}-GT 3]; Then
echo "XXX service has been started"
Break
Fi
Done
Some of my simple shell script instances