In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
The format of the for statement
For var in n1,n2,nn
Do
Instruction 1
Instruction 2
...
Done
Or
For var in item1 item2... ItemN; do command1; command2... Done
Example 1
#! / bin/bash
# use literal strings
For x in centos ubuntu gentoo opnesuse
Do
Echo "$x"
Done
# if the list contains space characters, it must be enclosed in quotation marks
For x in Linux "Gnu Hurd" FreeBSD "Mac OS X"
Do
Echo "$x"
Done
# eval variable reference
For x in ls "df-h"du-sh"
Do
Echo "= = $xboys ="; eval $x
Done
Running result
[root@localhost shell] # sh for1.sh
Centos
Ubuntu
Gentoo
Opnesuse
Linux
Gnu Hurd
FreeBSD
Mac OS X
= ls==
Case2.sh case4.sh for1.sh if-elif.sh liu myshell1.sh
Case3.sh case.sh if-elif-if-else.sh if-if.sh liub qq
= df-hype =
Filesystem Size Used Avail Use% Mounted on
/ dev/mapper/centos-root 27G 5.9G 22G 22% /
Devtmpfs 475M 0 475m 0% / dev
Tmpfs 487m 0 487m 0% / dev/shm
Tmpfs 487m 7.7m 479m 2% / run
Tmpfs 487m 0 487m 0% / sys/fs/cgroup
/ dev/sda1 1014M 133M 882M 14% / boot
Tmpfs 98m 0 98m 0% / run/user/0
= du-sh==
36K.
[root@localhost shell] #
Example 2
! / bin/bash
# using variables
I, 1
Weekdays= "Mon Tue Wed Thu Fri"
For day in $weekdays; do
Echo "Weekday $((iTunes +)): $day"
Done
# the default is a space. If the output is not set, the result is
# Linux
# 'Gnu
# Hur'
# FeeBSD
# 'Mac
# OS
# X'
# Others
# set IFS
IFS=:
OSlist= "Linux:'Gnu Hur':FeeBSD:'Mac OS X'"
For x in $OSlist Others; do
Echo "$x"
Done
Running result
[root@localhost shell] # sh for2.sh
Weekday 1: Mon
Weekday 2: Tue
Weekday 3: Wed
Weekday 4: Thu
Weekday 5: Fri
Linux
'Gnu Hur'
FeeBSD
'Mac OS X'
Others
[root@localhost shell] #
Example 3
#! / bin/bash
# use the location variable $@, in $@ can be omitted
I, 1
For day;do
Echo-n "position parameter $((iTunes +)): $day"
Case $day in
[Mm] on | [Tt] ue | [Ww] ed | [Tt] hu | [Ff] ri)
Echo "(weekday)"
[Ss] at | [Ss] un)
Echo "(weekeno)"
*)
Echo "(Invalid weekday)"
Esac
Done
Output result
[root@zabbix shell] # sh for3.sh mon tue wed thu fri sat sun
Position parameter 1: mon (weekday)
Position parameter 2: tue (weekday)
Position parameter 3: wed (weekday)
Position parameter 4: thu (weekday)
Position parameter 5: fri (weekday)
Position parameter 6: sat (weekeno)
Position parameter 7: sun (weekeno)
Example 4
#! / bin/bash
# use a file name or directory
For frname in *
Do
# replace the lowercase file name with the command and assign it to the variable fn
Fn=$ (echo $frname | tr Amurz Amurz)
# determine whether the newly produced file name is the same as the original name, and change it to lowercase if different
If [[$frname! = $fn]]; then mv $frname $fn;fi
# [[$frname! = $fn]] & & mv $frname $fn
# [[$frname = = $fn]] | | mv $frname $fn
Done
Running result
[root@zabbix shell] # touch ABC
[root@zabbix shell] # mkdir qwe
[root@zabbix shell] # sh for4.sh
[root@zabbix shell] # ll
Total dosage 8
-rw-r--r-- 1 root root 0 August 26 15:23 abc
-rw-r--r-- 1 root root 330 August 26 15:11 for3.sh
-rw-r--r-- 1 root root 218 August 26 15:23 for4.sh
Drwxr-xr-x 2 root root 6 August 26 15:23 qwe
Example 5
#! / bin/bash
For i in * .zip
Do
# removing the tail is the .zip section
J = "${i%.zip}"; mkdir "$j" & & unzip-d "$j"$I"
Echo $j
# j = "${i%.zip}"
# echo $j
Done
Execution result
[root@zabbix shell] # sh for5.sh
Archive: adf.zip
Extracting: adf/abc
Inflating: adf/for3.sh
Inflating: adf/for4.sh
Extracting: adf/qq.zip
Creating: adf/qwe/
Extracting: adf/we.zip
Adf
Archive: qq.zip
Extracting: qq/abc
Inflating: qq/for3.sh
Inflating: qq/for4.sh
Creating: qq/qwe/
Extracting: qq/we.zip
Archive: we.zip
Extracting: we/abc
Inflating: we/for3.sh
Inflating: we/for4.sh
Creating: we/qwe/
We
Example 6
! / bin/bash
# use the execution result of the command as a list
I, 1
For usrname in awk-F:'{print $1}'/ etc/passwd
Do
Echo "Username $((iTunes +)): $usrname"
Done
Echo "* *"
For line in $(cat / etc/yum.conf | egrep-v "^ / | ^ #")
Do
Echo "$line"
Done
Echo "seq Generation sequence"
For suffix in $(seq 5)
Do
Echo "192.168.1.$ {suffix}"
Done
Echo "* *"
For f in $(ls / var/); do
Echo $f
Done
Execution result
Username 10: operator
Username 11: games
Username 12: ftp
Username 13: nobody
Username 14: systemd-network
Username 15: dbus
Username 16: polkitd
Username 17: sshd
Username 18: postfix
Username 19: chrony
Username 20: www
Username 21: mysql
Username 22: saslauth
Username 23: zabbix
Username 24: grafana
Username 25: ntp
[main]
Cachedir=/var/cache/yum/$basearch/$releasever
Keepcache=0
Debuglevel=2
Logfile=/var/log/yum.log
Exactarch=1
Obsoletes=1
Gpgcheck=1
Plugins=1
Installonly_limit=5
Bugtracker_url= http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_repo
Rt_page.php?category=yum
Distroverpkg=centos-release
192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.4
192.168.1.5
Adm
Cache
Crash
Db
Empty
Games
Gopher
Example 7
#! / bin/bash
# judging host
For host in cat / etc/hosts
Do
If ping-C1-w2 $host & > / dev/null
Then
Echo "Host (${host}) is active."
Else
Echo "Host (${host}) is down."
Fi
Done
Execution result
Root@zabbix shell] # sh for7.sh
Host (127.0.0.1) is active.
Host (localhost) is active.
Example 8
#! / bin/bash
# use numeric values
Myip= "192.168.0"
For num in {1..5}
Do
Echo "IPAddr $num: $myip.$num"
Done
# step size
For num in {1..10..2}
Do
Echo "Number: $num"
Done
Execution result
[root@zabbix shell] # sh for8.sh
IPAddr 1: 192.168.0.1
IPAddr 2: 192.168.0.2
IPAddr 3: 192.168.0.3
IPAddr 4: 192.168.0.4
IPAddr 5: 192.168.0.5
Number: 1
Number: 3
Number: 5
Number: 7
Number: 9
Example 9
#! / bin/bash
For x in {1..5}
Do
Useradd user$ {x}
# stdin is to receive the string after echo as the password. Stdin is a non-exchangeable direct incoming password
# passwd defaults to using the terminal as standard input. Plus-- stdin can be used as standard input.
Echo "centos" | passwd-- stdin user$ {x}
# user first login to change password; chage password invalidation command-d time of last change
Chage-d 0 user$ {x}
Done
Execution result
[root@localhost shell] # sh for3.sh
Changing password for user user1.
Passwd: all authentication tokens updated successfully.
Changing password for user user2.
Passwd: all authentication tokens updated successfully.
Changing password for user user3.
Passwd: all authentication tokens updated successfully.
Changing password for user user4.
Passwd: all authentication tokens updated successfully.
Changing password for user user5.
Passwd: all authentication tokens updated successfully.
[root@localhost shell] # ls / home/
User1 user2 user3 user4 user5
Reopen a terminal with a new user such as: user1
WARNING: Your password has expired.
You must change your password now and login again!
Changing password for user user1.
Changing password for user1.
(current) UNIX password:
Example 10
! / bin/bash
# for nesting
For i in 0 1terdo
For suffix in {1..5}
Do
Ip=192.168.$i.$ {suffix}
If ping-C1-w2 $ip & > / dev/null
Then
Echo "$ip is active."
Else
Echo "Host ($ip) is down."
Fi
Done
Done
Execution result
[root@localhost shell] # sh for4.sh
Host (192.168.0.1) is down.
Host (192.168.0.2) is down.
Host (192.168.0.3) is down.
Host (192.168.0.4) is down.
Host (192.168.0.5) is down.
Host (192.168.1.1) is down.
Host (192.168.1.2) is down.
Host (192.168.1.3) is down.
Host (192.168.1.4) is down.
Host (192.168.1.5) is down.
Example 11
#! / bin/bash
# C language mode
For ((iSuppli)
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.