Some simple shell scripts that I've knocked on

Source: Internet
Author: User
Tags stdin server port yum repolist egrep

Some of the simple scripts that you've knocked over and put them on the occasional look, it won't be long before you feel very pediatric, but it should be helpful to understand the process control of shell scripts.

#!/bin/bash
p=176.202.52.
For I in $ (SEQ 1 254)
For ((i=1;i<=5;i++))
Do
PING-C2-I0.1-W1 $p $i &>/dev/null
If [$?-eq 0];then
echo "$p $i Neng Ping Tong"
Fi
Done

I=1
While:
Do
echo "$i"
Let i++
Sleep 0.1
Done
p=176.202.52.
I=1
While [$i-le 254]
Do
PING-C1-I0.1-W1 $p $i &>/dev/null

if [ $? -eq 0 ];thenecho "$p$i  

Done

#!/bin/bash
//read-p "Plese input a User:" A
//[-Z $a] && echo "No user inputed" && exit
// Stty-echo
//read-p "plese input password:" b
//stty echo
//echo ""
//[-Z $b] && echo "No password Inputed "&& exit
//useradd $a &>/dev/null
//echo $b | passwd--stdin $a
//if [-d/media/iso]; then
//echo the dir is exsit
//else
//mkdir-p/media/iso
//E Cho "This dir has been created"
//fi
//ping-c 3 192.168.4.254 &>/dev/null
//ping-c2-i0.2 W1 192.168.4. 10 | Head-2
//read-p "Qing shu ru IP:" A
//ping-c3-i0.2 W1 $a &>/dev/null
//if [$ (echo $?) -eq 0]; Then
//if [$?-eq 0];then
//echo "Keyi Ping Tong $a"
//else
//echo "Bukeyi Ping Tong $a"
//fi

//while:
num=$[random%10+1]//random number once generated will be fixed, thought only executed once the script, loop multiple times,
//If there is no for loop, then execute the script to guess the random number will change!!
//for i in {1..5}
i=0
When:
do
read-p ' please input your number1-10: ' Cai
Let i++
If [$num-eq $ Cai];then
Echo "You is right!"
echo "You cai le $i ci"
Exit
Elif [$num-lt $cai];then
echo "Your number is big"
//echo "The Currect Number is $num '
Else
Echo ' Your number is small '
//echo ' The currect number is $num '
Fi
done
//read- P "Plese input your score0-100:" num
//if [$num-ge 90]; Then
//echo "Sheng gong gai si"
//elif [$num-ge +];then
//echo "Deng Feng Zao ji"
//elif [$num-ge 70 ];then
//echo "Lu Huo Cun Qing"
//elif [$num-ge];then
//echo "Lue you Xiao Cheng"
//else
//Ech o "hehe da"
//fi
//a=$ (cat/etc/passwd | head-10 | tail-1 | cut-d:-F3)

Echo
echo $
Shift
Echo
Echo $*
Echo $//
//
I=1
While:
Do
echo "$i"
Let i++
Done
//

#!/bin/bash
Simple script application of case!
Case $ in
Redhat
echo "Fedora";;
Fedora
echo "Redhat";;
//*)
echo "Error";;
Esac
//

Case $ in
-L)
LS-LH;;
-u)
Useradd;;
-c)
CD $;;
-R)
RM-RF;;
//*)
echo "Usage $ [-n|-e|-c|-r] File"
Esac

a=$ (seq 9)
b=$ (seq 9)
C=haha
For I in $
Do
MyColor () {ECHO-E "\033[3$a;4$bm$c\033[0m";}
MyColor $c
Done

#!/bin/bash
Several jumps or interrupts the script continue break exit.
For I in $ (seq 9)
Do
b=$ (seq 9)
Mcolor () {
Echo-e "\033[3$im$b\033[0m"
}
Mcolor $b
Done

For i in {1..5}
Do
[$i-eq 3] && exit
Echo $i
Done
echo "Over"

#!/bin/bash
Dead Circle Ball and bounce method.
Sum=0
While:
Do
Read-p "Please input number:" num
[$num-eq 0] && break
Let sum+= $num
Done
Echo $sum

#!/bin/bash
Create a user and give the password in bulk based on the supplied file.
User= cat $1
For I in $user
Do
Useradd $i
Echo 1 | passwd--stdin $i
Done

Several methods of building numbers.
For ((i=1;i<=5;i+=2)) initial value 1 pieces less than 5 step 2
For I in $ (SEQ 1 2 5) starting from 1 steps 2 to 5 end
For i in {1..5} from 1 to 5 consecutive natural numbers.
For i in $[$RANDOM%10+1] 1-10 random number.
a=$[$RANDOM%10+1]
For I in $ (seq 1 $a)
Do
Echo $i
Done

Until the type loop creates the user regularly
I initial value is 1 less than 10 create User1 then let I become 2 again Judge I=2 10 continue to create User2
Then judge until I 10 jumps out of the loop.
A=user
I=1
While [$i-le 10]
Do
Useradd $a $i
Echo 1 | passwd--stdin $a $i
Let i++
Done

Until you guess the right jumps out of the four loop and displays the number of guesses.
a=$[$RANDOM%100+1] on the outside means that the random number generation is fixed.
Putting it inside means that each cycle will regenerate a random number.
Then the size of the hint doesn't make any sense to the next guess.
I=0
While:
a=$[$RANDOM%10+1]
Do
Read-p "Plesae input a number 1-100:" num
Let i++//each input is recorded once, and i=0 must be put out of the loop
Put inside: Every time after the cycle i=0, so as long as the right to guess is always 1 times!!!
If [$num-eq $a];then
echo "You is right!"
echo "You guess $i ci"
Exit
elif [$num-gt $a];then
echo "You is great"
Else
echo "You is less"
Fi
Done

[$a $i = = "192.168.4.5"] && continue
a=192.168.4.
I=1
While [$i-le 254]
[$a $i = = "192.168.4.5"] && continue
Do
PING-C2-I0.1-W1 $a $i &>/dev/null
If [$?-eq 0];then
echo "host $a $i is up"
Else
echo "host $a $i is down"
Fi
Let i++
Done

#!/bin/bash
x=abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789
echo ${x:$[$RANDOM%62]:1}
echo ${x:$[$RANDOM%54]:8}
y=$[$RANDOM%62]
echo "$x" | Cut-b $y-$[$y]
A=1
C=
While:
Do

b=$ (Echo ${x:$[$RANDOM%62]:1})
c= $c $b
Echo $b
Let a++
[$a-gt 8] && break

Let a++
Done
echo "$c"

#!/bin/bash
Batch change extension.
Cd/root
For I in $ (LS . $)
Do
A=${I/$1/$2}
b=${i%.
}
MV $i $a
MV $i {$b}$2
${i%. *}
//
Done

Variable to assign an initial value.

Read-p "Please input user name:" User
[-Z $user] && exit
Read-p "Please imput User pass:" Pass
Stty-echo
pass=${pass:-123456}
Stty Echo
Useradd $user
echo "$pass" | passwd--stdin $user

Sum=0
Read-p "Please imput sum number:" num
NUM=${NUM:-100}
For I in $ (seq $num)
Do
Let Sum+=i
Done
echo "$sum"
//
//
Read-p "Please imput a number:" X
I=1
Sum=0
X=${X:-100}
While:
Do
[$i-gt $x] && break
Let Sum+=i
Let i++
Done
echo "$sum"
//
Definition and use of arrays

Expect simulation people to interact with the first is to see, see the end of the line is password (appear on the line!! Not necessarily write this)
Enter the password Taren1 then the carriage return \ n is the meaning of the analog carriage return. See//See the command line to enter the command to execute.
Expect <<eof
Set Timeout 60
Spawn Ssh-o Stricthostkeychecking=no 192.168.4.$1
Expect "yes/no" {send "yes\n"}
Expect "password" {send "taren1\n"}
Expect "//" {Send "id haha\n"}
Expect "//" {Send "exit\n"}
Eof
The last line is not executed exit can be written as ABC.
The first time even-O Stricthostkeychecking=no will not prompt "yes/no"
Set timeout 60 Sets the expected time-out to 60s to prevent the computer from slowing down.

#!/bin/bash
Write an international chessboard table similar to the 99 multiplication table loop.
a=$ (ECHO-E $ "\033[32;42m" [] "\033[0m")
b=$ (ECHO-E $ "\033[34;44m" [] "\033[0m")
c=$ (Echo $a $b$a$b$a$b$a$b)
d=$ (Echo $b $a$b$a$b$a$b$a)
Echo $a $b$a$b$a$b$a$b
Echo $b $a$b$a$b$a$b$a
Echo $a $b$a$b$a$b$a$b
Echo $b $a$b$a$b$a$b$a
Echo $a $b$a$b$a$b$a$b
Echo $b $a$b$a$b$a$b$a
For i in {1..4}
Do
Echo $c
Echo $d
Done
a=$ (ECHO-E $ "\033[32;42m" [] "\033[0m")
b=$ (ECHO-E $ "\033[34;44m" [] "\033[0m")
For i in {1..8}
Do
For j in {1..4}
Do
c=$[$i%2]
[$c-eq 0] && echo-n $a $b | | Echo-n $b $ A
Done
echo ""
Done

#!/bin/bash
Yum-y Install VSFTPD
Sed-i ' s///chown_uploads/chown_uploads/g;/^//anon/s/^////'/etc/vsftpd/vsftpd.conf
chmod 777/var/ftp/pub
Systemctl Restart VSFTPD
Systemctl Enable CSFTPD

#!/bin/bash
Find the password MD5 value for the user name
user=$ (sed-n '/bash$/s/:.*//p '/etc/passwd)
For I in $user
Do
pass=$ (egrep $i/etc/shadow | cut-d:-F2)
pass=$ (egrep $i/etc/shadow | awk-f: ' {print $} ')

echo "$i --> $pass"

Done
[[email protected] ~]//grep Root/etc/shadow is observed/etc/shadow the file has only one user name in a row, so you can directly egrep the user name to determine its location.
Root:$6$ozdrfzxw$/nakdjt.ftewubqac
[[email protected] ~]//pass=$ (grep Root/etc/shadow) The end of the head can only be done on the variable, so the result is given to the variable before the operation.
[[email protected] ~]//pass1=${pass//:} Pair of variable pinch heads
[[email protected] ~]//pass2=${pass1%%:
} to the variable tail
[Email protected] ~]//echo $pass 2
$6$ozdrfzxw$/nakdjt.ftewubqac8azzinmjpkhgpgx1icyuramdy3e9eqkordzgglifbh/ixykyrp3ejdb0ftp8gebsmw44.

#!/bin/bash
//echo "content-type:text/html"
//echo ""
//ifconfig eth0 | awk '/netmask/{print ' Ip: "$ $} '
//echo" <br> "
//ifconfig eth0 | awk '/rx p/{print ' receive traffic: ' $ $ '
//echo <br> '
//ifconfig eth0 | awk '/tx p/{print ' send traffic: ' $ $ '
//echo ' <br> '
//df | awk '/\/$/{print ' root partition available: "$4} '//No plus-H for post-numeric comparisons!
//echo "<br>"
//free |awk '/mem/{print "Memory available:" $NF} '
echo "Content-type:text/html;charset=utf-8"
echo

Echo server_software = $SERVER _software//Server Software
Echo server_name = $SERVER _name//server host name
Echo Gateway_ INTERFACE = $GATEWAY _interface//cgi version
Echo server_protocol = $SERVER _protocol//protocol used for communication
echo Server_port = $ Server_port//server port number
echo Request_method = $REQUEST _method//Request Method (Get/post/put/delete.)
echo http_accept = $HTTP _accept//http defined by the browser can accept the data type
echo script_name = $SCRIPT _name//The currently running script name (including path)
Echo Query_string = $QUERY _string//The data passed in the Address bar (get mode)
echo remote_addr = $REMOTE _addr//Client IP

#!/bin/bash
a=$ (yum repolist |awk '/repolist/{print $} ' |sed ' s/,//g ')
[$a-le 0] && echo "Yum not available" &am p;& exit
yum-y install gcc openssl-devel pcre-devel &>/dev/null
[$?-eq 0] && echo "GCC open SSL pcre installation Complete "| | echo "Software for successful Installation"
Tar-zxf/root/share/nginx-1.10.3.tar.gz-c/root/share
cd/root/share/nginx-1.10.3
. Configure &>/dev/null
[$-eq 0] && echo "Configuration Complete" | | echo "configuration failed"
make &>/dev/null
[$?-E Q 0] && echo "Compile Complete" | | echo "Compilation failed"
make install &>/dev/null
[$?-eq 0] && echo "Nginx installation Complete" | | echo "Installation Failed"
b=$ (netstat -ntulp |awk '/:80/{print $7} ' |cut-d/-f2 '
C=/usr/local/nginx/sbin/nginx
Netstat-ntulp |grep ":" &>/dev /null
If [$?-ne 0];then
$c
Echo "Nginx service from shutdown to startup"
Elif [$b = = "httpd"];then
Systemctl stop $b
$c
E Cho "closed httpd and started Nginx service"
Else
$c-S Stop
$c
echo "Nginx service reboot successfully"
Fi

!/bin/base
C=/usr/local/nginx/sbin/nginx
Case $ in
Stop
$c-S Stop
echo "Nginx service closed";;
Restart
$c-S Stop
$c
echo "Nginx Service restart success";;
Status
Netstat-ntulp |grep Nginx
[$?-ne 0] && echo "Nginx Service not open";;
*)
echo "Useage Stop restart status"
Esac

#!/bin/bash
a=$ (Uptime |awk ' {print$8,$9,$10} ')
b=$ (ifconfig eth0 | awk '/rx p/{print} ')
c=$ (ifconfig eth0 | awk '/tx p/{print} ')
d=$ (Free |awk '/mem/{print $NF} ')
e=$ (DF | awk '/\/$/{print $4} ')
f=$ (cat/etc/passwd |wc-l)
g=$ (who |wc-l)
h=$ (Rpm-qa |wc-l)
echo "CPU top 1, 5, 15 min load: $a"
echo "The received traffic for the network card is: $b"
echo "The received traffic for the network card is: $c"
echo "Current Memory free space: $d"
echo "root partition currently available space is $e"
echo "The current number of users of this machine: $f"
echo "The current number of users logged in this machine: $g"
echo "The number of packages currently installed on this machine is: $h"

#!/bin/bash
a=$ (awk '/failed/&& $9!~/invalid/{print $11} '/var/log/secure |awk ' {a[$1]++}end{for (i in a) {print i,a[i]}} ' |aw K ' {if ($2>5) {print$1}} ')
b=$ (awk '/failed/&& $9!~/invalid/{print $11} '/var/log/secure |awk ' {a[$1]++}end{for (i in a) {print I, A[i]}} ' |aw K ' {if ($2>5) {print$1}} ')
Echo $a
Echo $b

#!/bin/bash
Jindu () {
While:
Do
Echo-n "//"
Sleep 1
Done
}
Jindu &
Cp-r $
Kill $!

#!/bin/bash
Read-p "Please imput one number:" A
Read-p "Please imput number:" B
Read-p "Please imput three number:" C
If [$a-gt $b];then
If [$b-gt $c];then
Echo $a $b$c
Else
[$a-gt $c] && echo $a $c$b | | Echo $c $a$b
Fi
Else
If [$a-gt $c];then
Echo $b $a$c
Else
[$b-gt $c] && echo $b $c$a | | Echo $c $b$a
Fi
Fi
Three numbers can also be used if forced to judge 6 times the number is not forced to judge so!!! To replace with a location.

D=0
If [$a-gt $b];then
D= $a
A= $b
b= $d
Fi
If [$a-gt $c];then
D= $a
A= $c
c= $d
Fi
If [$b-gt $c];then
d= $b
b= $c
c= $d
Fi
echo $a, $b, $c

These two methods have the following knowledge points
①:IF Double Branch sets of two branches, to clarify the structure of the relationship. The algorithm is three numbers sorted with 6 kinds of row method
②:1, a variable is a container, in order to exchange the values of the two variables, that is to want two containers in the exchange of things, you must introduce a third container. First put a into the c,a loaded into the b,b and then installed C can.
[Email protected] ~]//a=1
[Email protected] ~]//b=2
[Email protected] ~]//c=3
[Email protected] ~]//c= $b
[Email protected] ~]//b= $a
[Email protected] ~]//a= $c
[Email protected] ~]//echo $a
2
[Email protected] ~]//echo $b
1
If it is not introduced, only one of them is poured out, and the thing that is first poured into that container has changed, and the container has not changed. Well, the two containers will certainly be the same thing.
2, three variables (containers) from small to large sort: only need to ensure that the first container is loaded with the smallest value, the second container has a second small value, the third container can be loaded with the largest value.
The first minimum is determined first: a If the value of B is larger than B, the value for a A is small. Again, the value of a and the value of C, a greater than C to the C-loaded to the A at this time a pack must be the smallest.
Ok the smallest, the remaining two to compare, the value of B is large and c to the C of the inverted to B let the value of B to become a small value of the BC loaded. The final container is always placed in ABC but the D container is used in the process.

Some simple shell scripts that I've tapped

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.