以下脚本已经是在ubuntu下测试的
- #!/bin/bash
-
- i=1
- for i in {1..254}
- do
- # 每隔0.3s Ping 一次,每次超时时间3s,Ping的结果直接废弃
- ping-w 3 -i 0.3 192.168.110.$i > /dev/null
- if [ $? -eq 0 ];then
- echo "192.168.120.$i is rechable"
- else
- echo "192.168.120.$i is unrechable"
- fi
- #let i++
- done
- #!/bin/bash
-
- user=$(cat /opt/user.txt)
-
- for i in $user
- do
- echo "$i"
- useradd "$i"
- echo $i:$i | chpasswd
- #echo "1234" | passwd --stdin $i
- done
注意点:
有批量添加,就有批量删除
- #!/bin/bash
-
- user=$(cat /opt/user.txt)
-
- for i in $user
- do
- userdel -f $i
- done
- #!/bin/bash
-
- for((i=0;i<=100;i++))
- do
- if [ $[i%2] -eq 1 ];then
- array[$[$[$i-1]/2]]=$i
- fi
- done
- #echo ${array[0]}
- echo ${array[*]}
运行结果
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99
知识点:
- #!/bin/bash
-
- i=0
-
- while true
- do
- read -p "input? yes or no:" operate
- if [ $operate = no ];then
- break
- fi
- read -p "input the value:" value
- array[$i]=$value
- let i++
- done
-
- echo ${array[*]}
知识点:
- #!/bin/bash
-
- array=(61 62 63 50 59 64)
- count=${#array[*]}
-
- echo before:${array[*]}
- for((i=0;i<$count;i++))
- do
- if [ ${array[$i]} -lt 60 ] ; then
- array[$i]=60
- fi
- done
-
- echo after:${array[*]}
知识点:
- #!/bin/bash
-
- max=0
- array=(1 2 5 3 7 9)
- count=${#array[*]}
-
- for((i=0;i<$count;i++))
- do
- if [ ${array[$i]} -gt $max ];then
- max=${array[$i]}
- fi
- done
-
- echo "the max num of array is ${max}"
知识点:
- #!/bin/bash
-
- seed=(0 1 2 3 4 5 6 7 8 9)
- len=${#seed[*]}
- random=$[RANDOM%$len]
- guess=${seed[$random]}
-
- while :
- do
- read -p "0-9? you guess which one? " doing
- if [ $doing -eq $guess ]; then
- echo "bingo"
- exit 0
- elif [ $doing -gt $guess ]; then
- echo "oops, too big"
- else
- echo "oops, too small"
- fi
-
- done
-
知识点
- #!/bin/bash
-
- num=(58 59 60 61 62)
-
- i=0
- for p in ${num[*]}
- do
- if [ $p -lt 60 ]; then
- unset num[$i]
- fi
- let i++
- done
-
- echo the final result is ${num[*]}
- exit 0
知识点:
- tt=1
- echo $tt
- unset tt
- echo $tt
- #!/bin/bash
-
- sum=0
- for i in {1..100}
- do
- sum=$[$sum+$i]
- done
-
- echo the result is ${sum}
- exit 0
- #!/bin/bash
-
- sum=0
- i=0
-
- until [ $i -gt 50 ]
- do
- #sum=$[$sum+$i]
- let sum+=$i
- let i++
- done
-
- echo the result is ${sum}
知识点:
- #!/bin/bash
-
- game=(rock paper sissor)
- random=$[$RANDOM%3]
- scriptResult=${game[$random]}
-
- echo $scriptResult
-
- echo "choose your result:"
- echo "1.rock"
- echo "2.paper"
- echo "3.sissor"
-
- read -p "choose 1, 2 or 3: " choice
-
- case $choice in
-
- 1)
- if [ $[$random+1] -eq 1 ]; then
- echo "equal"
- elif [ $[$random+1] -eq 2 ]; then
- echo "you lose"
- else
- echo "you win"
- fi
- ;;
- 2)
-
- if [ $[$random+1] -eq 1 ]; then
- echo "you win"
- elif [ $[$random+1] -eq 2 ]; then
- echo "equal"
- else
- echo "you lose"
- fi
- ;;
- 3)
- if [ $[$random+1] -eq 1 ]; then
- echo "you lose"
- elif [ $[$random+1] -eq 2 ]; then
- echo "you win"
- else
- echo "equal"
- fi
- ;;
- *)
- echo "wrong number"
- ;;
-
- esac
-
-
- exit 0
- #!/bin/bash
-
- while :
- do
- read -p "input score: " score
- if [ $score -le 100 ] && [ $score -gt 85 ]
- then
- echo "great"
- elif [[ $score -lt 85 && $score -ge 60 ]]; then
- echo "normal"
- else
- echo "failed"
- fi
- done
-
- exit 0
知识点:
[[ ]] 不需要注意某些细枝末节,比[]功能强大
[[ ]] 支持逻辑运算
[[ ]] 支持正则表达式
- #!/bin/bash
-
- mem_line=$(free -m | grep "Mem")
- memory_total=$(echo $mem_line | awk '{print $2}')
- memory_used=$(echo $mem_line | awk '{print $3}')
-
- echo "total memory: $memory_total MiB"
- echo "used memory: $memory_used MiB"
- #echo "usage rate: $[$memory_used/$memory_total * 100]%"
- echo "usage rate:" $(echo $mem_line | awk '{printf("%0.2f%", $3 / $2 * 100)}')
-
- exit 0
知识点:
1、free -m 命令以MiB为单位打印
1 MiB = 1024KiB, 1MB=1000KiB
- man free
-
- -m, --mebi Display the amount of memory in mebibytes.
- total used free shared buff/cache available
- Mem: 3876 1182 1518 3 1175 2428
- Swap: 923 0 923
grep "Mem" 会将第二行(Mem开头的那行)抽取出来
2、awk 会进行文本处理, 利用printf函数处理小数
awk '{printf("%0.2f%", $3 / $2 * 100)}'
- #!/bin/bash
-
- eth=$(ifconfig | grep "ether" | awk '{print $2}')
-
- echo $eth
-
- exit 0
思路也简单,通过ifconfig 获取ether所在行,通过awk获取对应行的第二个参数$2
ens37: flags=4163
mtu 1500
inet6 fe80::5e0a:f1cf:cbf8:4c2d prefixlen 64 scopeid 0x20
ether 00:0c:29:f6:b6:99 txqueuelen 1000 (Ethernet)
RX packets 87 bytes 10378 (10.3 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 3992 bytes 720667 (720.6 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73
mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 1000 (Local Loopback)
RX packets 68614 bytes 4911600 (4.9 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 68614 bytes 4911600 (4.9 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
- #!/bin/bash
-
- count=0
-
- for file in $(ls /var/log)
- do
- echo $file
- if [ -f "/var/log/${file}" ]
- then
- let count++
- echo "file name: $file"
- else
- echo "no file"
- fi
- done
-
- echo "the count of files is: ${count}"
-
- echo "the count of files is:" `find /var/log -maxdepth 1 -type f |wc -l`
-
- exit 0
知识点:
- for e in `cat ./nowcoder.txt`
- do
- if [ ${#e} -lt 8 ];then
- echo ${e}
- fi
- done
-
- exit 0
知识点
统计所有进程占用内存百分比的和_牛客题霸_牛客网 (nowcoder.com)
- #!/bin/bash
-
- sum=0
- i=0
- for m in `awk '{print $4}' ./nowcoder.txt`
- do
- if [ $i -eq 0 ];then
- let i++
- continue
- fi
- r=$(echo $m | bc)
- sum=$(echo $r + $sum | bc)
- done
- echo $sum | bc
-
- exit 0
知识点:
打印无顺序(利用字典)
- #!/bin/bash
-
- declare -A dic
-
- for word in `cat nowcoder.txt`
- do
- if [ $word = "" ];then
- continue
- fi
- if [ -v dic[$word] ]; then
- r=${dic[$word]}
- dic[$word]=`expr $r + 1`
- else
- dic[$word]=1
- fi
- done
-
- for key in `echo ${!dic[*]}`
- do
- echo "$key ${dic[$key]}"
- done
-
- exit 0
-
-
利用文件处理顺序
- #!/bin/bash
-
- declare -A dic
-
- for word in `cat nowcoder.txt`
- do
- if [ $word = "" ];then
- continue
- fi
- if [ -v dic[$word] ]; then
- r=${dic[$word]}
- dic[$word]=`expr $r + 1`
- else
- dic[$word]=1
- fi
- done
-
- rm tmp.txt
- touch tmp.txt
-
- for key in `echo ${!dic[*]}`
- do
- echo "${dic[$key]} $key" >> tmp.txt
- done
-
- cat tmp.txt | sort -n |awk '{print $2,$1}'
- exit 0
知识点:
1、字典声明用declare -A dic
2、字典赋值dic[key]=value
3、获取所有key , ${!dic[*]}
4、获取所有value, ${dic[*]}
已调试,大致没问题,想学习的复制拿去
- #!/bin/sh
-
- menu_choice=""
- current_cd=""
- title_file="title.cdb"
- tracks_file="tracks.cdb"
- temp_file=/tmp/cdb.$$
- trap 'rm -f $temp_file' EXIT
-
- get_return() {
- echo "Please return \c"
- read x
- return 0
- }
-
- get_confirm() {
- echo -e "Are you sure? \c"
- while true
- do
- read x
- case "$x" in
- y | yes|Y|Yes|YES) return 0;;
- n | no|N|No|NO)
- echo
- echo "Cancelled"
- return 1;;
- *) echo "Please enter yes or no";;
- esac
- done
-
- }
-
- set_menu_choice() {
- clear
- echo "Options : -"
- echo
- echo " a) Add new CD"
- echo " f) Find CD"
- echo " c) Cont the CDs and tracks in the catalog"
- if [ "$cdcatnum" != "" ]; then
- echo " l) List track on $cdtitle"
- echo " r) Remove $cdtitle"
- echo " u) Update track information for $cdtitle"
- fi
- echo " q) Quit"
- echo
- echo "Please enter choice then press return \c"
- read menu_choice
- return
- }
-
- insert_title() {
- echo $* >> $title_file
- return
- }
-
- insert_track() {
- echo $* >> $tracks_file
- return
- }
-
- add_record_tracks() {
- echo "Enter track information for this CD"
- echo "When no more tracks enter q"
- cdtrack=1
- cdtitle=""
- while [ "$cdtitle" != "q" ]
- do
- echo "Track $cdtrack, track title? \c"
- read tmp
- cdtitle=${tmp%%, *}
- echo ${tmp}======
- echo ${cdtitle}-------
- if [ "$tmp" != "$cdtitle" ]; then
- echo "Sorry, no commas allowed"
- continue
- fi
-
- if [ -n "$cdtitle" ]; then
- echo enter
- echo ${cdtitle}-------
- if [ "${cdtitle}" != "q" ]; then
- echo enter1
- insert_track $cdcatnum, $cdtrack, $cdtitle
- fi
- else
- cdtrack=$((cdtrack-1))
- fi
- cdtrack=$((cdtrack+1))
- done
- }
-
- add_records() {
- echo "Enter catalog name \c"
- read tmp;
- cdcatnum=${tmp%%, *}
- echo "Enter title \c"
- read tmp
- cdtitle=${tmp%%, *}
-
- echo "Enter type \c"
- read tmp
- cdtype=${tmp%%, *}
-
- echo "Enter artist/composer \c"
- read tmp
- cdac=${tmp%%, *}
-
- echo About to add new entry
- echo "$cdcatnum $cdtitle $cdtype $cdac"
-
- if get_confirm; then
- insert_title $cdcatnum, $cdtitle, $cdtype, $cdac
- add_record_tracks
- else
- remove_records
- fi
- return
- }
-
- find_cd() {
- if [ "$1" = "n" ] ; then
- asklist=n
- else
- asklist=y
- fi
- cdcatnu=""
- echo -e "Enter a string to search for in the CD titles \c"
- read searchstr
-
- if [ "$searchstr" = "" ] ; then
- return 0
- fi
-
- grep "$searchstr" $title_file > $temp_file
-
- set $(wc -l $temp_file)
- linesfound=$1
-
- case "$linesfound" in
-
- 0) echo "Sorry, nothing found"
- get_return
- return 0;;
- 1) ;;
- 2) echo "Sorry, not unique"
- echo "Found the following"
- cat $temp_file
- get_return
- return 0
- esac
-
- IFS=","
- read cdcatnum cdtitle cdtype cdac < $temp_file
- IFS=" "
-
- if [ -z "$cdcatnum" ]; then
- echo "Sorry, cound not extract catalog field from $temp_file"
- get_return
- return 0
- fi
-
- echo
-
- echo Catalog num: $cdcatnum
- echo Title: $cdtitle
- echo TYPe: $cdtype
- echo Artist: $cdac
- echo
- get_return
- if [ "$asklist" = "y" ]; then
- echo -e "View tracks for this CD? \c"
- if [ "$x" = "y" ]; then
- echo
- list_tracks
- echo
- fi
- fi
- return 1
- }
-
-
- update_cd() {
-
- if [ -z "$cdcatnum" ]; then
- echo "You must select a CD first"
- find_cd n
- fi
-
- if [ -n "$cdcatnum" ]; then
- echo "Current tracks are: -"
- list_tracks
- echo
- echo "This will re_enter the tracks for $cdtitle"
- get_confirm && {
- grep -v "^${cdcatnum}," $tracks_file > $temp_file
- mv $temp_file $tracks_file
- echo
- add_record_tracks
- }
- fi
- return
- }
-
- count_cds() {
- set $(wc -l $title_file)
- num_titles=$1
- set $(wc -l $tracks_file)
- num_tracks=$1
- echo found $num_title CDs, with a total of $num_tracks tracks
- get_return
- return
- }
-
- remove_records() {
- if [ -z "$cdcatnum" ]; then
- echo You must select a CD first
- find_cd n
- fi
-
- if [ -n "$cdcatnum" ]; then
- echo "You are about to delete $cdtitle"
- get_confirm && {
- grep -v "^${cdcatnum}," $title_file > $temp_file
- mv $temp_file $title_file
- grep -v "^${cdcatnum}," $tracks_file > $temp_file
- mv $temp_file $tracks_file
- cdcatnum=""
- echo Entry removed
- }
- get_return
- fi
-
- return
- }
-
-
- list_tracks() {
- if [ "$cdcatnum" = "" ]; then
- echo no CD selected yet
- return
- else
- grep "^${cdcatnum}," $tracks_file > $temp_file
- num_tracks=$(wc -l $temp_file)
- if [ "$num_tracks" = "0" ]; then
- echo no tracks found for $cdtitle
- else {
- echo
- echo "$cdtitle :-"
- echo
- cut -f 2- -d, $temp_file
- } | ${PAGER:-more}
- fi
- fi
- get_return
- return
- }
-
- rm -f $temp_file
-
- if [ ! -f $title_file ];then
- touch $title_file
- fi
-
- if [ ! -f $title_file ]; then
- touch $tracks_file
- fi
-
- clear
- echo
- echo
- echo "Mini CD manager"
- sleep 1
-
- quit=n
- while [ "$quit" != "y" ];
- do
- set_menu_choice
- case "$menu_choice" in
- a) add_records;;
- r) remove_records;;
- f) find_cd y;;
- u) update_cd;;
- c) count_cds;;
- l) list_tracks;;
- b)
- echo
- more $title_file
- echo
- get_return
- ;;
- q|Q) quit=y;;
- *) echo "Sorry, choice not recognized";;
- esac
- done
-
- rm -f $temp_file
- echo "Finished"
- exit 0
-