shell100例 不会的语句 man bash查询(3)

12、使用while和for语句写1+100的值
#/bin/bash
#:==ture
i=1
NUM=0
#做数字判断需要加((IF))
while ((i<=100)); do
#lst下可以不加$
let NUM=i+NUM
#i++==i=i+1 i--
let i++
done
echo $NUM

for (( j=1,TOTAL=0;j<=100;j++ ));do
    let TOTAL+=j
done
echo "$TOTAL"


for (( J=1,total=0;J<=100;total+=J,J++ ));do
    true
done
echo "$total"

13、制作一个批量备份某一个文件夹下文件的脚本
#!/bin/bash
set -u
DIR=$1
[ -d $1 ] && cd $DIR || { echo -e "\E[1;31msyntax error\E[0m \nesage: $0 [directory]";exit; }
for FILE in *;do
    if [ -f $FILE ];then
        cp "$FILE" "$FILE.bak"
    else
        continue
    fi
done
14、比较数字大小,显示最大和最小的数字
#!/bin/bash
#filename=Digital_Compare.sh

#判断是否有位置参数 $#:参数的个数 $0:脚本名称
[ $# -eq 0 ] && echo "usage: $0 num1 num2..."
#判断输入的参数是否都是数字
#[[]]中=~右边的正则不需要""
for j in "$@";do
[[ $j =~ [[:digit:]] ]] || { echo -e "please \E[1;31m"$j"\E[0m input number";exit; }
done


#$@需要使用""
for i in "$@";do

[ "$i" -eq "$1" ] && { MAX="$i";MIN="$i";continue; }
[ "$i" -lt "$MIN" ] && { MAX="$MAX";MIN="$i";continue; }
[ "$i" -gt "$MAX" ] && { MAX="$i";MIN="$MIN"; }

done
echo "MAXNUM="$MAX",MINNUM="$MIN""
[root@centos8-3 /date/shell ]$ bash Digital_Compare.sh 555 666 777  
MAXNUM=777,MINNUM=555
15、linux中$random用于生成0—32767的随机数,破解字符串对应的RANDOM值

while read 第一种语法

while read line; do
循环体
done < /PATH/FROM/SOMEFILE
while read val;do
	for ran in {1..32767};do
		num=`echo $ran| md5sum |cut -c1-10`
		 [[ $val == $num ]] && echo "${val} :${ran}"
	done
done <<EOF 
efbaf275cd
4be9c40b8b
44b2395c46
f8c8873ce0
b902c16c8b
ad865d2f63
EOF
————————————————
版权声明:本文为CSDN博主「思0霖」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/mqs915860370/article/details/108216502

while read 第二种语法

echo -e "$NUM\n"|grep -v "^$"|while read VAR;do
循环体
done
#!/bin/bash
#filename==random_mad5sum_while.sh
NUM="efbaf275cd
4be9c40b8b
44b2395c46
f8c8873ce0
b902c16c8b
ad865d2f63"
#echo -e "$NUM\n"
echo -e "$NUM\n"|grep -v "^$"|while read VAR;do
    for ((i=0;i<=32767;i++));do        
          {
          [[ `echo $i|md5sum|cut -c1-10` == $VAR ]] && echo "$VAR=$i'|md5sum|cut -c1-10'" 
          }&
    done
wait
done

没有解决匹配第六个之后立马停止

#!/bin/bash
#filename==random_mad5sum_for.sh
NUM1="efbaf275cd"
NUM2="4be9c40b8b"
NUM2="4be9c40b8b"
NUM3="44b2395c46"
NUM4="f8c8873ce0"
NUM5="b902c16c8b"
NUM6="ad865d2f63"
for ((i=0;i<=32767;i++));do
    {
    j=`echo $i|md5sum|cut -c1-10`
    [[ $j =~ $NUM1|$NUM2|$NUM3|$NUM4|$NUM5|$NUM6 ]] && echo "$j= $i|md5sum|cut -c1-10"
    }&
done
wait
16、查询执行最多的10条命令
#!/bin/bash
#HISTFILE=/root/.bash_history
set -o history
history -w
cat ~/.bash_history | cut -d " " -f1-100 | sort | uniq -c | sort -rn |head -10 > history.txt
while read row content;do

echo "history command max NUM $row,command is $content"

done < history.txt
rm ./history.txt
#history | awk '{a[$5]++ } END{for(i in a){print a[i] " " i}}'|sort -rn|head
[root@centos8-3 /date/shell ]$ cat -A history.txt 
     77 vim grep_file.sh $
     49 bash grep_file.sh$
     48 bash grep_file.sh $
     21 vim grep_file.sh$
     16 ll$
     13 pwd$
     11 ls$
     11 history $
     10 history --help$
      9 #while read row content;do$
17、制作国际象棋棋盘
#!/bin/bash
for i in {1..8};do
    for j in {1..8};do
        if [[ $i =~ 1|3|5|7 ]];then
                if [[ $j =~ 1|3|5|7 ]];then
                    echo -ne "\E[46m \E[0m"
                else
                    echo -ne "\E[47m \E[0m"
                fi
        else
                if [[ $j =~ 2|4|6|8 ]];then
                    echo -ne "\E[46m \E[0m"
                else
                    echo -ne "\E[47m \E[0m"
                fi
        fi
    done
    echo
done
#!/bin/bash
#棋盘的规律是 i+j 如果是偶数,就打印蓝色色块,如果是奇数就打印灰色色块
for i in {1..8}
do
        for j in {1..8}
        do
                sum=$[i+j]
                if [  $[sum%2] -eq 0 ];then
                        echo -ne "\033[46m  \033[0m"
                else
                        echo -ne "\033[47m  \033[0m"
                fi
        done
        echo
done
18、基于CentOS8 一键编译安装httpd脚本
scp 10.0.0.251:/date/shell/httpd_source_install.sh ./
#!/bin/bash
# File Name: httpd_source_install.sh
. /etc/init.d/functions 
INSTALLDIR=/usr/local/src/
DIR=/app/httpd/
software_version=httpd-2.4.46
sysconfdir=/etc/httpd/

echo -e "\E[1;33m安装大概需要三分钟,请稍等\E[0m"
systemctl disable --now firewalld &> /dev/null

install() {
mkdir $DIR -pv &> /dev/null
mkdir $sysconfdir &> /dev/null
cd $INSTALLDIR


yum -y install gcc make bzip2 apr-devel apr-util-devel pcre-devel openssl-devel redhat-rpm-config wget &> /dev/null && \
{ action "安装依赖包" ; } || { action "安装软件包失败,请检查网络配置" false ; exit; }

wget https://mirrors.bfsu.edu.cn/apache//httpd/"$software_version".tar.bz2 &> /dev/null && \
{ action "httpd 源码下载" ; } || { action "httpd 源码下载失败" false ; exit; }


tar xf "$software_version".tar.bz2 &> /dev/null
cd "$software_version"
./configure --prefix="$DIR" --sysconfdir="$sysconfdir" --enable-ssl --enable-proxy &> /dev/null
make -j 2 &> /dev/null && make install &> /dev/null && { action "httpd 编译安装" ; }|| { action "httpd 编译安装失败" false ;exit ; }


groupadd -r -g 88 Apache &> /dev/null
useradd -r -g Apache -u 88 -d /app/httpd/ -s /sbin/nologin Apache && action "Httpd 用户创建成功"


sed -i.bak -e "/^User/s@.*@User Apache@" -e "/^Group/s@.*@Group Apache@" -e '/^#ServerName/s@www.example.com:80@'`hostname`':88@' -e 's/Listen 80/Listen 88/' /etc/httpd/httpd.conf
ln -s /app/httpd/bin/apachectl /usr/bin/
sed -ri "s@(.*>)(.*\!)(<.*)@\1runoob\3@p" "$DIR"htdocs/index.html

echo -e "\E[1;32mhttp安装完成,版本为:$software_version,软件路径为$DIR,配置文件路径为$sysconfdir,端口port为88,请使用apachectl start 开启服务\E[0m"
}

install
19、显示当前主机所有进程状态
#!/bin/bash
# File Name: count_process.sh
#Tasks: 257 total,   1 running, 256 sleeping,   0 stopped,   0 zombie
TASKS_TOTAL=0
RUNNING=0
SLEEPING=0
STOPPED=0
ZOMBIE=0
NOKNOW=0


for PID in `ls /proc/|grep -E "[0-9]+"`;do
#2> /dev/null,正常结果输出,异常结果不输出
PROCESS=$(sudo cat /proc/"$PID"/stat 2> /dev/null|awk '{print $3}')

#case $PROCESS in,case中间需要$变量
case $PROCESS in
R)
    let RUNNING="$RUNNING+1"
    ;;
S|I)
    let SLEEPING="$SLEEPING+1"
    ;;
T)
    let STOPPED="$STOPPED+1"
    ;;
Z)
    let ZOMBIE="$ZOMBIE+1"
    ;;
*)
    let NOKNOW="$NOKNOW+1"
    ;;
esac
done
TASKS_TOTAL=$[$RUNNING+$SLEEPING+$STOPPED+$ZOMBIE]

echo "正在运行的程序共:$TASKS_TOTAL"
echo "运行正常的程序共:"$RUNNING""
echo "沉睡的程序共:"$SLEEPING""
echo "停止的程序共:"$STOPPED""
echo "僵尸的程序共:"$ZOMBIE""
kill -STOP [pid]
发送SIGSTOP (17,19,23)停止一个进程,而并不消灭这个进程。
kill -CONT [pid]
发送SIGCONT (19,18,25)重新开始一个停止的进程。

#/proc/pid/下记录了进程使用的信息
上一篇:2021-03-26


下一篇:Java8 默认方法和静态方法,新特性