Simple example of a shell script

Source: Internet
Author: User
Tags case statement gz file

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 the size of two numbers

#/bin/bash
echo "Please enter two number"
Read a
Read B
if test $a-eq $b
Then echo "no.1 = no.2"
Elif Test $a-gt $b
Then echo "No.1 > No.2"
else echo "No.1 < No.2"
Fi

3. Find out if the file exists in 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
Todo
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 under the current directory

#/bin/bash
for filename in ' ls '
Todo
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 are files under/export/um_lpp_source, then change their file system size to 3G

#/bin/bash
While Line= ' Ls/export/um_lpp_source '
Todo
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 IP Address

#/bin/bash
For I in 1 2 3 4 5 6 7 8 9
Todo
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"
Todo
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 Read content, prepare for the following example

#/bin/bash
While read name
Todo
Echo $name
Done

11. Read content from 0.sh and print

#/bin/bash
While Read line
Todo
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
Todo
a=$ (($line + 1))
Done < A.C
Echo $a

13. Ordinary parameter-free function

#/bin/bash
P ()
{
echo "Hello"
}
P

14. Pass parameters to function

#/bin/bash
P_num ()
{
Num=$1
Echo $num
}
For N in $@
Todo
P_num $n
Done

15. Create a Folder

#/bin/bash
While:
Todo
echo "Please input file ' s name:"
Read a
If TEST-E/root/$a
Then
echo "The" 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 *.*
Todo
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 network segment and redirect to the Ip.txt file

#/bin/bash
A=1
While:
Todo
a=$ (($a + 1))
if test $a-GT 255
Then break
Else
echo $ (ping-c 1 192.168.0. $a | grep "TTL" | awk ' {print $} ' | sed ' s/://g ')
ip=$ (ping-c 1 192.168.0. $a | grep "TTL" | awk ' {print $} ' | sed ' s/://g ')
echo $ip >> Ip.txt
Fi
Done

19. Print Current User

#/bin/bash
echo "Current User is:"
echo $ (PS | grep "$$" | awk ' {print $} ')

20.case Statement Practice

#!/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 password-free user

#/bin/bash
echo "No Password User are:"
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 port number is started
#!/bin/bash
N=1
echo "Check XXX service ..."
While True
Todo
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 already started"
Break
Fi
Done

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.