Some of their common shell scripts share _linux Shell

Source: Internet
Author: User
Tags case statement gz file

Wrote a little shell instance of myself, though very small, but all the big programs are stacked up by small modules, programmers must understand a script writing, and I, will only work under Linux, so you can only write Linux shell script, oh, this article will be updated, to refuel themselves!

1. Analog Linnux Login Shell

Copy Code code as follows:

#/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
Copy Code code as follows:

#/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
Copy Code code as follows:

#/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
Copy Code code as follows:

#/bin/bash
Clear
For NUM in 1 2 3 4 5 6 7 8 9 10
Todo
echo "$num"
Done

5.
Copy Code code as follows:

#/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
Copy Code code as follows:

#/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
Copy Code code as follows:

#/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
Copy Code code as follows:

#/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
Copy Code code as follows:

#/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
Copy Code code as follows:

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

11. Read content from 0.sh and print
Copy Code code as follows:

#/bin/bash
While Read line
Todo
Echo $line
Done < 0.sh

12. Read the contents of the A.C and add 1 operations
Copy Code code as follows:

#/bin/bash
TEST-E A.C
While Read line
Todo
a=$ (($line + 1))
Done < A.C
Echo $a

13. Ordinary parameter-free function
Copy Code code as follows:

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

14. Pass parameters to function
Copy Code code as follows:

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

15. Create a Folder
Copy Code code as follows:

#/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
Copy Code code as follows:

#/bin/bash
Ifconfig | grep "inet Addr:" | awk ' {print $} ' | Sed ' s/addr://g '

17. Find the largest file
Copy Code code as follows:

#/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
Copy Code code as follows:

#/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
Copy Code code as follows:

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

20.case Statement Practice
Copy Code code as follows:

#!/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
Copy Code code as follows:

#!/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. Use of built-in commands

Copy Code code as follows:

#/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!"

23. Print password-free user

Copy Code code as follows:

#/bin/bash
echo "No Password User are:"
echo $ (Cat/etc/shadow | grep "!!" | awk ' BEGIN {fs= ': '}{print $} ')

24.
Copy Code code as follows:

#/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!"

25. Check if port number is started

Copy Code code as follows:

#!/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.