30 classic shell script (under)

Article directory

21, download files from FTP server

22, five consecutive numbers within an input 100, and statistics, the minimum and maximum

23, the results are assigned to the variable

24, modify the batch file name

25, the statistics in the current directory ending in .html file always great

26, scan host port status

27, Expect interaction-free implementation of the SSH command execution

28, batch modify server user password

29, print multiplication formulas

30, getopts tool perfect script command-line arguments

21, download files from FTP server

#!/bin/bash

if [ $# -ne 1 ]; then

echo "Usage: $0 filename"

be

dir=$(dirname $1)

file=$(basename $1)

ftp -n -v << EOF # -n automatic login

open 192.168.1.10 # ftp server

user admin password

binary # Set ftp binary transfer mode, to avoid the MD5 value different compressed format error or .tar.gz

cd $ dir

get "$file"

EOF

22, five consecutive numbers within an input 100, and statistics, the minimum and maximum

#!/bin/bash

COUNT=1

SUM=0

MIN = 0

MAX=100

while [ $COUNT -le 5 ]; do

read -p "Enter integer less than 100:" INT

if [[ ! $INT =~ ^[0-9]+$ ]]; then

echo "input must be an integer!"

exit 1

elif [[ $INT -gt 100 ]]; then

echo "input must be less than 100!"

exit 1

be

SUM=$(($SUM+$INT))

[ $MIN -lt $INT ] && MIN=$INT

[ $MAX -gt $INT ] && MAX=$INT

let COUNT++

done

echo "SUM: $SUM"

echo "MIN: $MIN"

echo "MAX: $MAX"

 

 

23, the results are assigned to the variable

Scenario: assigning an execution result or a desired position to the variable parameter, for subsequent use.

method 1:

for i in $(echo "4 5 6"); do

eval a$i=$i

done

echo $ $ a4 a5 $ a6

Method 2: The position parameter 192.168.1.1 {1,2} to split each variable

a = 0

for i in $ (eval echo $ *); do #eval be decomposed into {1,2} 12

let num+=1

eval node${num}="$i"

done

echo $node1 $node2 $node3

# bash a.sh 192.168.1.1{1,2}

192.168.1.11 192.168.1.12

Method 3:

arr=(4 5 6)

INDEX1=$(echo ${arr[0]})

INDEX2=$(echo ${arr[1]})

INDEX3=$(echo ${arr[2]})

 

24, modify the batch file name

Example:

# touch article_{1..3}.html

# ls

article_1.html article_2.html article_3.html

Objective: To read the article bbs

method 1:

for file in $(ls *html); do

mv $file bbs_${file#*_}

# mv $file $(echo $file |sed -r 's/.*(_.*)/bbs\1/')

# mv $file $(echo $file |echo bbs_$(cut -d_ -f2)

done

Method 2:

for file in $(find . -maxdepth 1 -name "*html"); do

mv $file bbs_${file#*_}

done

Method 3:

# rename article bbs *.html

 

25, the statistics in the current directory ending in .html file always great

method 1:

# find . -name "*.html" -exec du -k {} \; |awk '{sum+=$1}END{print sum}'

Method 2:

for size in $(ls -l *.html |awk '{print $5}'); do

sum=$(($sum+$size))

done

echo $sum

 

26, scan host port status

#!/bin/bash

HOST=$1

PORT="22 25 80 8080"

for PORT in $PORT; do

if echo &>/dev/null > /dev/tcp/$HOST/$PORT; then

echo "$PORT open"

else

echo "$PORT close"

be

done

 

27, Expect interaction-free implementation of the SSH command execution

Expect interactive application is an automated tool, such as telnet, ftp, passwd like.

You need to install expect the package.

Method 1: EOF expect as a standard input standard output

#!/bin/bash

USER=root

PASS=123.com

IP=192.168.1.120

expect << EOF

set timeout 30

spawn ssh $USER@$IP

expect {

"(yes/no)" {send "yes\r"; exp_continue}

"password:" {send "$PASS\r"}

}

expect "$USER@*" {send "$1\r"}

expect "$USER@*" {send "exit\r"}

expect eof

EOF

Method 2:

#!/bin/bash

USER=root

PASS=123.com

IP=192.168.1.120

expect -c "

spawn ssh $USER@$IP

expect {

\"(yes/no)\" {send \"yes\r\"; exp_continue}

\"password:\" {send \"$PASS\r\"; exp_continue}

\"$USER@*\" {send \"df -h\r exit\r\"; exp_continue}

}"

Method 3: expect scripts independent

Login script:

# cat login.exp

#!/usr/bin/expect

set ip [lindex $argv 0]

set user [lindex $argv 1]

set passwd [lindex $argv 2]

set cmd [lindex $argv 3]

if { $argc != 4 } {

puts "Usage: expect login.exp ip user passwd"

exit 1

}

set timeout 30

spawn ssh $user@$ip

expect {

"(yes/no)" {send "yes\r"; exp_continue}

"password:" {send "$passwd\r"}

}

expect "$user@*" {send "$cmd\r"}

expect "$user@*" {send "exit\r"}

expect eof

 

Run the script: write cycles can operate multiple batch servers

#!/bin/bash

HOST_INFO=user_info.txt

for ip in $(awk '{print $1}' $HOST_INFO)

do

user=$(awk -v I="$ip" 'I==$1{print $2}' $HOST_INFO)

pass=$(awk -v I="$ip" 'I==$1{print $3}' $HOST_INFO)

expect login.exp $ip $user $pass $1

done

Linux host SSH connection information:

# cat user_info.txt

192.168.1.120 root 123456

28, batch modify server user password

Linux host SSH connection information: the old password

# cat old_pass.txt

192.168.18.217 root 123456 22

192.168.18.218 root 123456 22

Content format: IP User Password Port

SSH Remote Change Password script: new randomly generated password

#!/bin/bash

OLD_INFO=old_pass.txt

NEW_INFO=new_pass.txt

for IP in $(awk '/^[^#]/{print $1}' $OLD_INFO); do

USER=$(awk -v I=$IP 'I==$1{print $2}' $OLD_INFO)

PASS=$(awk -v I=$IP 'I==$1{print $3}' $OLD_INFO)

PORT=$(awk -v I=$IP 'I==$1{print $4}' $OLD_INFO)

NEW_PASS = $ (mkpasswd -l 8) # random password

echo "$IP $USER $NEW_PASS $PORT" >> $NEW_INFO

expect -c "

spawn ssh -p$PORT $USER@$IP

set timeout 2

expect {

\"(yes/no)\" {send \"yes\r\";exp_continue}

\"password:\" {send \"$PASS\r\";exp_continue}

\"$USER@*\" {send \"echo \'$NEW_PASS\' |passwd --stdin $USER\r exit\r\";exp_continue}

}"

done

Generate a new password file:

# cat new_pass.txt

192.168.18.217 root n8wX3mU% 22

192.168.18.218 root c87;ZnnL 22

 

29, print multiplication formulas

method 1:

# awk 'BEGIN{for(n=0;n++<9;){for(i=0;i++<n;)printf i"x"n"="i*n" ";print ""}}'

Method 2:

for ((i=1;i<=9;i++)); do

for ((j=1;j<=i;j++)); do

result=$(($i*$j))

echo -n "$j*$i=$result "

done

echo

done

30, getopts tool perfect script command-line arguments

getopts is an analytical tool scripting options parameters.

Format: getopts optstring name [arg]

Initial use you should pay attention to these points:

Script positional parameters one by one match optstring in a single letter, if it matches the name assigned to, or assigned name is a question mark;

optstring single letter is an option, if the letters a colon, which indicates that the following option parameters, and the parameter value will be assigned to variables OPTARG;

The first is a optstring colon, showing the shield system error (test.sh: illegal option - h);

It allows the option to put together, such as -ab

 

The following simple example of writing a print file specified line to guide your thinking:

#!/bin/bash

while getopts :f:n: option; do

case $option in

f)

FILE=$OPTARG

[ ! -f $FILE ] && echo "$FILE File not exist!" && exit

;;

n)

sed -n "${OPTARG}p" $FILE

;;

?)

echo "Usage: $0 -f -n "

echo "-f, --file specified file"

echo "-n, --line-number print specified line"

exit 1

;;

esac

done

 

Guess you like

Origin www.cnblogs.com/zjz20/p/11519908.html