1, create an account and password linux system variable position
$ 1 is the first parameter to execute the script, $ 2 is the second argument to execute the script
#!/bin/bash
# Create a user and password
declare -i c=0
if [ -z $1 ] || [ -z $2 ];then
echo "Please enter your username and password!"
be
for i in $(cat /etc/passwd|cut -d: -f1);do
if [ $1 == $i ];then
let c=1
be
done
if [ $c -ne 1 ];then
useradd $1
`echo $2|passwd $1 --stdin &>/dev/null`
if [ $? -eq 0 ];then
echo "added successfully"
be
else
echo "This user already exists'
be
2, 5 week the tar command to back up all files in the log / var / log, to prevent the document cover
#!/bin/bash
All log files under # five weekly backup / var / log
tar -zcf log-`date +%Y%m%d`.tar.gz /var/log
[root@sv7 ~]# crontab -e
0 3 5 /root/tar.sh
3、
Script generates a random value less than 100, the user is prompted guessing, more user input, the user is prompted guessed it, I guess guess playing or small, until you guess right end of the script.
#!/bin/bash
num=$(($RANDOM%100+1))
c=0
while [ $c -lt 5 ]
do
read -p "Enter your guess of the number of >>" mynum
if [ $mynum -eq $num ];then
echo "You got it !!"
break
elif [ $mynum -gt $num ];then
echo "Guess big !! please continue !!"
elif [ $mynum -lt $num ];then
echo "Guess small !! please continue !!"
be
c=$(($c+1))
done
if [ $c -eq 5 ];then
echo "random number $ num"
be
4, in turn prompts the user for an integer of 3, 3 digital script output sequence sorted according to size of the figures
#!/bin/bash
read -p "Please enter the first number >>" a
read -p "Please enter the second number >>" b
read -p "Enter the third number >>" c
if [ $a -gt $b ];then
if [ $b -gt $c ];then
echo "$a>$b>$c"
elif [ $b -lt $c ];then
if [ $a -gt $c ];then
echo "$a>$c>$b"
elif [ $a -lt $c ];then
echo "$c>$a>$b"
be
be
elif [ $a -lt $b ];then
if [ $b -gt $c ];then
if [ $a -gt $c ];then
echo "$b>$a>$c"
elif [$a -lt $c ];then
echo "$b>$c>$a"
be
elif [ $b -lt $c ];then
echo "$c>$b>$a"
be
be
5, scripting, man-machine "rock, paper, scissors" game
#!/bin/bash
j=1
while [ $j -le 5 ]
do
echo "1. 2. stone scissors cloth 3."
read -p "Please punches 1-3:" i
if [$ i -ne $ 1 election to election -ne 2 to $ 3 -ne]: A
echo "Please enter a number between 1-3"
be
game = (Janken)
num=$((RANDOM%3))
echo computer=${game[$num]}
case $i in
1)
if [ 0 -eq $num ];then
echo "draw"
elif [ 1 -eq $num ];then
echo "You lose."
else
echo "You win."
be;;
2)
if [ 1 -eq $num ];then
echo "draw"
elif [ 0 -eq $num ];then
echo "You lose."
else
echo "You win."
be;;
3)
if [ 2 -eq $num ];then
echo "draw"
elif [ 1 -eq $num ];then
echo "You lose."
else
echo "You win."
be;;
esac
let j++
done
6, scripting test 192.168.4.0/24 entire segment is turned on in those host, those hosts turned off (for release)
#!/bin/bash
declare -i sum=0
-I declare mēnsum = 0
for i in $(seq 1 254);do
ping -c 1 -w 1 10.6.12.$i &> /dev/null
if [ $? -eq 0 ];then
let sum++
echo "10.6.12. $ i boot"
else
let mēnsum ++
echo "10.6.12. $ i shutdown"
be
done
echo "There are $ sum computers boot!"
echo "There are $ nsum computers shut down!"
7,9 * 9 multiplication tables (write shell scripts, printed 9 * 9 multiplication table)
#!/bin/bash
for i in {1..9};do
for j in $(seq $i);do
echo -ne "$i*$j=$((i*j)) "
done
echo ''
done
8, using an infinite loop real-time display of data packet traffic sent by the network card eth0
#!/bin/bash
while :
do
echo "local network card eth0 flow information is as follows:"
ifconfig eth0 | grep "RX pack" | awk ‘{print $5}‘
ifconfig eth0 | grep "TX pack" | awk ‘{print $5}‘
sleep 1
done
9, using a list of persons user.txt file automatically creates a corresponding account in the computer and configure the initial password
Script execution, you need to prepare in advance a user.txt file, which contains a number of user information
#!/bin/bash
c=`wc -l user.txt|cut -d' ' -f1`
for i in $(seq $c);do
username=`head -$i user.txt|tail -1 |cut -f1`
passwd=`head -$i user.txt|tail -1 |cut -f2`
c=0
for j in $(cat /etc/passwd|cut -d: -f1);do
if [ $username == $j ];then
echo "$ username already exists can not be added."
c=1
be
done
if [ $c -eq 0 ];then
`useradd $username &>/dev/null`
`echo $passwd|passwd $username --stdin &>/dev/null`
echo "added successfully"
be
done
10, write batch script to modify extensions, such as txt file to modify the batch file to doc
When a script executes, you need to add parameters to the script location
Script name txt doc (txt extension can be modified to doc)
Script name doc jpg (doc extensions can be modified to jpg)
#!/bin/bash
# Author: Sean Martin
# Blog: https://www.cnblogs.com/shy13138/
# Time: 2019-08-16 10:24:42
# Name: houzhui.sh
# Version: v1.0
read -p "Enter the old suffix:" a
read -p "Enter the new suffix:" b
read -p "Enter the folder:" d
$ cd d
for i in $(ls $d | grep .$a)
do
name=$(ls $i | cut -d. -f1)
mv $i ${name}.$b
echo "$name.$a ====> $name.$b"
done
echo "Done Editing"
11 to see how many remote IP connection in the unit (either through web or ftp or ssh all statistics)
Use netstat -atn can view all connections status of the machine, -a view all, -t tcp displays only connection information, -n digital format local address (the fourth column is the IP and port information of the client) Foreign address (V IP is strong remote host and port information) using the awk command to display only the data in column 5, and then display the information in column 1 IP address
sort of numbers can be ordered by size, last used Uniq will delete unnecessary repetition, and count the number of repetitions
#!/bin/bash
# Author: Sean Martin
# Blog: https://www.cnblogs.com/shy13138/
# Time: 2019-08-16 10:32:19
# Name: netstat1.sh
# Version: v1.0
declare -i L=0
declare -i E=0
declare -i O=0
for i in $(netstat -tna|grep "^tcp"|awk '{print $6}');do
if [ $i = "LISTEN" ];then
let L++
elif [ $i = "ESTABLISHED" ];then
let E++
else
let O++
be
done
echo "There are $ L a link monitor, link establishment has a $ E, other links have $ O"
12, for summing all positive integers less than 100 (1 + 2 + 3 + ... 100)
eq 100 can quickly and automatically generate integer 100
#!/bin/bash
# Author: Sean Martin
# Blog: https://www.cnblogs.com/shy13138/
# Time: 2019-08-18 06:26:33
# Name: 1-100.sh
# Version: v1.0
declare -l sum=0
for i in $(seq 0 2 100);do
let sum=$sum+$i
done
echo $sum