In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you the basis and principle of shell script example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to understand it!
1. Special variable
$#: view the number of variable parameters
$0: view the name of the script
$! : view the pid of shell backend
$@: view a list of all the parameters of the passing script
$*: view a list of all parameters, displayed as a single string
$: ID of the process of the script itself
$?: the result of the previous command shows that 0 succeeds and fails if it is not 0
two。 Internal environment variable $PATH SHELL current shellUID current user environment {0 | other numbers} = {root | other users} HOME current user directory PWD current directory HISTFILE history command path PS1 # [\ u@\ h\ W]\ $user @ hostname\ directory\ $3. Integer and character judgment 3.1 Integer judgment
-eq tests whether two integers are equal (equal)-ne tests whether two integers are equal (unequal)-gt tests whether a number is greater than a number (greater than)-lt tests whether a number is less than a number (less than)-ge tests a number greater than or equal to-le tests a number Less than or equal to 3.2character test = ~ Test whether it is matched by a regular expression-z "string" detects whether the character is empty Empty is true, not empty is false, for example: [- z ""] is true if vacuum is true-n "string" detects whether the character is not empty, if it is not empty, it is true, and if it is not empty, false characters are compared with [[]] Compare the first letter (a-zA-Z) in uppercase or lowercase; the higher the ascii value, the greater the uppercase and lowercase case A > a B > b but An is not greater than b [root@slave02 ~] # [["A")
< "B" ]][root@slave02 ~]# echo $?0[root@slave02 ~]# [[ "a" < "b" ]][root@slave02 ~]# echo $?04.文件判断-e:文件是否存在 -b:测试是否块设备文件-c:测试是否字符设备文件-f:测试是否普通文件-d:测试是否目录-h:测试是否符号链接文件-L:测试是否是符号链接文件-p:测试是否是命名管道文件-S:测试是否是套接字文件权限相关:-r 读-w 写-x 执行特殊权限-g-u-k等5.read输入选项:-p:指定提示符-t:指定提示等待的时间(秒)6.if判断多分支:if [ 条件 ];then statement1 .....elif [ 条件2 ];then statement2 ....else statement3 ....fi7.案例选择判断case $变量名 in 'value1') statement ... ;; 'value2') statement ... ;; *) statement .. ;; esac#case支持的通配符: * //任意长度任意字符 ? //任意单个字符 [] //指字范围内的任意单个字符 start|START //俩种选择8.for循环第一种:for ((expr1;expr2;expr3)) # expr1:初始值条件 #expr2:循环的范围进行退出 #expr3:变量的值使用{ 循环体}for ((expr1;expr2;expr3));do 循环体done第二种:for 变量 in 列表; do 循环体done9.while循环while循环用于不知道循环次数的场景,注意有退出条件while [ 条件 ];do statement .....done10.深入练习1.写一个脚本,输入三个数字进行相应的加减乘除[root@slave02 ~]# cat script01.sh #!/bin/basha=$1b=$2c=$3num1=$[$a+$b+$c]num2=$[$a-$b-$c]num3=$[$a*$b*$c]echo "$a + $b + $c" = $num1echo "$a - $b - $c" = $num2echo "$a * $b * $c" = $num3awk "BEGIN{printf \"$a/$b/$c=%.2f\n\",$a/$b/$c}"[root@slave02 ~]# source script01.sh 100 10 9100 + 10 + 9 = 119100 - 10 - 9 = 81100 * 10 * 9 = 9000100/10/9=1.112.猜数字游戏 规则:指定一个数字,只要猜到了这个数字则过关,否则显示数字大了或者数字小了 [root@master ~]# cat test03.sh #!/bin/bashnums=99read -p "please enter a number: " numif [ $num -gt $nums ];then echo "数字大了"elif [ $num -lt $nums ];then echo "数字小了"else echo "猜对"fi [root@master ~]# . test03.sh please enter a number: 10数字小了[root@master ~]# . test03.shplease enter a number: 100数字大了[root@master ~]# . test03.shplease enter a number: 99猜对3.写一个脚本,让nginx服务设置开机自启#$0是nginx本身 $1是变量对应着下面的start|stop|restart|status[root@192 init.d]# pwd/etc/init.d[root@192 init.d]# cat nginx #!/bin/bashcase $1 in 'start') /usr/local/nginx/sbin/nginx ;; 'stop') /usr/local/nginx/sbin/nginx -s stop ;; 'restart') /usr/local/nginx/sbin/nginx -s stop /usr/local/nginx/sbin/nginx ;; 'status') num=$(ps -ef |grep -v 'grep'|grep -c nginx:) if [ $num -eq 0 ];then echo "nginx is stoped" else echo "nginx is running" fi ;; *) echo "Usage: service $0 start|stop|restart|status" ;; esac #当判断有nginx进程数量则认为开启服务,否则认为服务开启失败4.利用for循环,创建user序号1-100的用户#创建用户user1-100[root@master ~]# cat test05.sh #!/bin/bashfor (( i=1;i/dev/null if [ $? -eq 0 ];then #只要判断用户成功,$?才会显示0,显示0则代表执行下一条命令,否则显示user以及存在 echo "success" else echo "user is exis" fidone5.利用while循环,计算1+2…100的值[root@slave02 ~]# cat which.sh #!/bin/bashs=0 #初始值0i=1 #判断的数值,最终到100停止while [ $i -le 100 ];dos=$[$s+$i] i=$[$i+1] #自增加数doneecho $s[root@slave02 ~]# source which.sh 5050 #随便输入一个数字进行计算的话,把100改为$1即可6.apache简单的一个编译部署脚本1.一般项目或者脚本,文件,放在相应的位置里,方便查找[root@slave02 tmp]# pwd/tmp[root@slave02 tmp]# lsapache[root@slave02 apache]# lsinstall_apache.sh soft[root@slave02 soft]# lsapr-1.7.0.tar.bz2 apr-util-1.6.1.tar.bz2 httpd-2.4.48.tar.bz2 httpd.service[root@slave02 apache]# cat install_apache.sh #!/bin/bash echo "欢迎使用此脚本" apachedir=/usr/local/apache if [ $UID -ne 0 ];then echo "伙计,请使用管理员身份运行"fiecho "正在安装依赖包..."yum -y install epel-release bzip2 "@Development Tools" &>/ dev/nullyum-y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make & > / dev/nullid apache & > / dev/nullif [$?-ne 0] Then useradd-r-M-s / sbin/nologin apacheficd / tmp/apache/soft/tar-xf apr-1.7.0.tar.bz2tar-xf apr-util-1.6.1.tar.bz2tar-xf httpd-2.4.48.tar.bz2sed-I'/ $RM "$cfgfile" / d'apr-1.7.0/configureecho "is compiling and installing apr, please listen to the song to relax." Cd apr-1.7.0/ [!-d / usr/local/apr] if [$?-eq 0]; then. / configure-- prefix=/usr/local/apr & & make & & make install & > / dev/nullelse echo "apr has installed" ficd.. / apr-util-1.6.1/ [!-d / usr/local/apr-util] if [$?-eq 0] Then. / configure-- prefix=/usr/local/apr-util-- with-apr=/usr/local/apr & & make & & make install & / dev/nullelse echo "apr-util has installed" ficd.. / httpd-2.4.48/ [!-d / usr/local/apache/] if [$?-eq 0] Then./configure-- prefix=$apachedir\-- sysconfdir=/etc/httpd24\-- enable-so\-- enable-ssl\-- enable-cgi\-- enable-rewrite\-- with-zlib\-- with-pcre\-with-apr=/usr/local/apr\-- with-apr-util=/usr/local/apr-util/\ -- enable-modules=most\-- enable-mpms-shared=all\-- with-mpm=prefork make & & make install & > / dev/nullelse echo "httpd has been installed" ficd# is influential Ignore echo "export PATH=$apachedir/bin:\ $PATH" > / etc/profile.d/httpd.shln-s $apachedir/include/ / usr/include/apache & > / dev/nullgrep 'apache/man' / etc/man_db.conf & > / dev/nullif [$?-eq 1] Then sed-I "20aMANDATORY_MANPATH $apachedir/man" / etc/man_db.confelse echo "apache is help exists" fi [!-f / usr/lib/systemd/system/httpd.service] if [$?-eq 0] Then cp / clq/apache/soft/httpd.service / usr/lib/systemd/system/else echo "File already exists" fisystemctl daemon-reloadsystemctl enable-- now httpdnum02=$ (ps-ef | grep-v 'grep' | grep-c httpd) if [$num02-eq 0] Then echo "httpd Boot failed" else echo "httpd Boot success" fiecho "Welcome next time use [root@slave02 apache] # chmod + x install_apache.sh [root@slave02 apache] # source install_apache.sh [root@slave02 apache] # source install_apache.sh Welcome to use this script to install dependency packages. Apr is being compiled and installed, please listen to songs to relax. Apr and install apr-util and install httpd already installed apache is help exists already exists files skip httpd self-startup successfully welcome next time to use [root@slave02 ~] # systemctl status httpd.service ● httpd.service-Start http Loaded: loaded (/ usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled) Active: active (running) since Sat 2021-09-04 17:45:33 CST 5h 57min ago Main PID: 834761 (httpd) Tasks: 7 (limit: 5782) Memory: 6.3m CGroup: / system.slice/httpd.service ├─ 834761 / usr/local/apache/bin/httpd-k start ├─ 835358 / usr/local/apache/bin/httpd-k start ├─ 835359 / usr/local/apache/bin/httpd-k start ├─ 835360 / usr/local/apache/bin/httpd- K start ├─ 835361 / usr/local/apache/bin/httpd-k start ├─ 835362 / usr/local/apache/bin/httpd-k start └─ 836063 / usr/local/apache/bin/httpd-k start [root@slave02] # ss-antlState Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0 22 0.0.0.015 * LISTEN 0 128 *: 80 *: * LISTEN 0128 [:]: 22 [::]: * these are all the contents of the article "sample Analysis of the fundamentals and principles of shell scripts" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.