3.bash configuration file
There are many bash shell configuration files, which can be divided into the following categories
3.1 it is divided into two categories according to the effective scope
Global configuration: for all users
/etc/profile /etc/profile.d/*.sh /etc/bashrc
Personal configuration: only valid for specific users
~/.bash_profile ~/.bashrc
3.2 classification of two methods of shell login
3.2.1 interactive login
-
Log in directly by entering the account and password through the terminal
-
Users switched using su - UserName
Configuration file execution order:
/etc/profile.d/*.sh /etc/bashrc /etc/profile /etc/bashrc #This file is executed twice ~/.bashrc ~/.bash_profile
Note: the calling relationship between files written in different locations of the same file will affect the execution order of the file
example:
[root@rocky8 ~]# vim .bashrc umask 111 [root@rocky8 ~]# vim .bash_profile umask 333 [root@rocky8 ~]# exit logout [root@rocky8 ~]# umask 0333 #The latter one overwrites the first one
3.2.2 non interactive login
- su UserName
- Terminal opened under graphical interface
- Execute script
- Any other bash instance
Execution sequence:
/etc/profile.d/*.sh /etc/bashrc .bashrc
3.3 classification by function
profile class and bashrc class
3.3.1 Profile class
The profile class provides configuration for an interactive login shell
overall situation:/etc/profile, /etc/profile.d/*.sh Individual:~/.bash_profile
Main functions:
(1) Used to define environment variables
(2) Run a command or script
3.3.2 Bashrc type
bashrc class: provides configuration for non interactive and interactive login shell s
overall situation:/etc/bashrc Individual:~/.bashrc
Main functions:
(1) Define command aliases and functions
(2) Define local variables
3.4 edit configuration file effective
There are two methods to be effective after modifying the profile and bashrc files:
- Restart the shell process
- source |. Configuration file
example:
. ~/.bashrc
3.5 Bash exit task
Save in ~ /. Bash_ In the logout file (user), run when exiting the login shell
Function:
- Create automatic backup
- Clear temporary files
example:
[root@rocky8 ~]# cp /etc/issue /data [root@rocky8 ~]# ls /data app.conf issue systeminfo.sh test.sh test.txt [root@rocky8 ~]# vim .bash_logout rm -rf /data/* [root@rocky8 ~]# exit logout [root@rocky8 ~]# ls /data #.bash_logout this is the operation to be performed when exiting
3.6 practice
1. Make the PATH environment variable of all users have one more PATH, for example: / usr/local/apache/bin
2. When the user root logs in, the command indicator turns red and the following alias is automatically enabled: rm = 'rm -i'
cdnet='cd /etc/sysconfig/network-scripts/'
editnet='vim /etc/sysconfig/network-scripts/ifcfg-eth0'
editnet = 'VIM / etc / sysconfig / network scripts / ifcfg eno16777736 or ifcfg ens33' (if the system is CentOS7)
3. When any user logs in to the system, the warning message "Hi,dangerous!" in red font will be displayed
4. Write a script in the basic format of the generated script, including author, contact information, version, time, description, etc
4. Process control
4.1 condition selection
4.1.1 condition judgment introduction
4.1.1.1 single branch condition
4.1.1.2 multi branch condition
4.1.2 select execute if statement
Format:
if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fi
Single branch
if Judgment conditions;then Branch code with true condition ...... fi
Double branch
if Judgment conditions; then Branch code 1 with true condition ...... else Condition is false branch code 2 ...... fi
Multi branch
if Judgment condition 1; then Branch code 1 with condition 1 as true ...... elif Judgment condition 2; then Branch code 2 where condition 2 is true ...... elif Judgment condition 3; then Branch code 3 where condition 3 is true ...... ... else The above conditions are false branch codes N ...... fi
explain:
- When there are multiple conditions, judge one by one. When the condition is "true" for the first time, execute its branch, and then end the whole if statement
- if statements can be nested
example:
[root@rocky8 bin]# type if if is a shell keyword [root@rocky8 bin]# help if if: if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fi Execute commands based on conditional. The `if COMMANDS' list is executed. If its exit status is zero, then the `then COMMANDS' list is executed. Otherwise, each `elif COMMANDS' list is executed in turn, and if its exit status is zero, the corresponding `then COMMANDS' list is executed and the if command completes. Otherwise, the `else COMMANDS' list is executed, if present. The exit status of the entire construct is the exit status of the last command executed, or zero if no condition tested true. Exit Status: Returns the status of the last command executed.
example:
[root@centos6 ~]# declare -f __udisks () { local IFS=' '; local cur="${COMP_WORDS[COMP_CWORD]}"; if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--show-info" ]; then COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur)); else if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--inhibit-polling" ]; then COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur)); else if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--mount" ]; then COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur)); else if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--unmount" ]; then COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur)); else if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--detach" ]; then COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur)); else if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--ata-smart-refresh" ]; then COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur)); else if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--ata-smart-simulate" ]; then _filedir || return 0; else if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--set-spindown" ]; then COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur)); else if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--poll-for-media" ]; then COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur)); else COMPREPLY=($(IFS=: compgen -W "--dump:--inhibit-polling:--inhibit-all-polling:--enumerate:--enumerate-device-files:--monitor:--monitor-detail:--show-info:--help:--mount:--mount-fstype:--mount-options:--unmount:--unmount-options:--detach:--detach-options:--ata-smart-refresh:--ata-smart-wakeup:--ata-smart-simulate:--set-spindown:--set-spindown-all:--spindown-timeout:--poll-for-media" -- $cur)); fi; fi; fi; fi; fi; fi; fi; fi; fi }
example:
#Execute the command according to the exit state of the command [root@rocky8 bin]# vim if_scanip.sh #!/bin/bash # #********************************************************************************************** #Author: Raymond #QQ: 88563128 #Date: 2021-10-10 #FileName: if_scanip.sh #URL: raymond.blog.csdn.net #Description: The test script #Copyright (C): 2021 All rights reserved #********************************************************************************************* HOST=172.31.0.7 if ping -c1 -w1 $HOST >& /dev/null;then echo "$HOST is UP" elif grep -q "$HOST" ~/maintenance.txt;then echo "$HOST is undergoing maintenance" else echo "$HOST is unexpextedlu DOWN!" exit 1 fi [root@rocky8 bin]# bash if_scanip.sh 172.31.0.7 is UP
Example: body mass index (BMI)
[root@rocky8 bin]# vim if_bmi.sh #!/bin/bash # #********************************************************************************************** #Author: Raymond #QQ: 88563128 #Date: 2021-10-10 #FileName: if_bmi.sh #URL: raymond.blog.csdn.net #Description: The test script #Copyright (C): 2021 All rights reserved #********************************************************************************************* read -p "Please enter your height(m Unit):" HIGH if [[ ! "$HIGH" =~ ^[0-2](\.[0-9]{,2})?$ ]];then echo "Wrong height entered" exit 1 fi read -p "Please enter your weight(kg Unit):" WEIGHT if [[ ! "$WEIGHT" =~ ^[0-9]{1,3}$ ]];then echo "Wrong height entered" exit 1 fi BMI=`echo $WEIGHT/$HIGH^2| bc` if [ $BMI -le 18 ];then echo "It's too thin. Eat more" elif [ $BMI -lt 24 ];then echo "Great figure!" else echo "It's too fat,Pay attention to diet and strengthen exercise" fi [root@rocky8 bin]# bash if_bmi.sh Please enter your height(m Unit):170 Wrong height entered [root@rocky8 bin]# bash if_bmi.sh Please enter your height(m Unit):1.7 Please enter your weight(kg Unit):65 Great figure!
4.1.3 conditional judgment case statement
Format:
case WORD in [PATTERN [| PATTERN]...) COMMANDS ;;]... esac
case Variable reference in PAT1) Branch 1 ;; PAT2) Branch 2 ;; PAT3) Branch 3 ;; ... *) Default branch ;; esac
case supports glob style wildcards:
* Any length, any character ? Any single character [] Any single character within the specified range | Or, if: a|b
example:
[root@rocky8 bin]# type case case is a shell keyword [root@rocky8 bin]# help case case: case WORD in [PATTERN [| PATTERN]...) COMMANDS ;;]... esac Execute commands based on pattern matching. Selectively execute COMMANDS based upon WORD matching PATTERN. The `|' is used to separate multiple patterns. Exit Status: Returns the status of the last command executed.
example:
[root@rocky8 bin]# vim case_yesorno.sh #!/bin/bash # #********************************************************************************************** #Author: Raymond #QQ: 88563128 #Date: 2021-10-10 #FileName: case_yesorno.sh #URL: raymond.blog.csdn.net #Description: The test script #Copyright (C): 2021 All rights reserved #********************************************************************************************* read -p "Do you agree(yes/no)?" INPUT INPUT=`echo $INPUT | tr 'A-Z' 'a-z'` case $INPUT in y|yes) echo "You input is YES" ;; n|no) echo "You input is NO" ;; *) echo "Input fales,please input yes or no!" esac [root@rocky8 bin]# vim case_yesorno2.sh #!/bin/bash # #********************************************************************************************** #Author: Raymond #QQ: 88563128 #Date: 2021-10-10 #FileName: case_yesorno2.sh #URL: raymond.blog.csdn.net #Description: The test script #Copyright (C): 2021 All rights reserved #********************************************************************************************* read -p "Do you agree(yes/no)? " INPUT case $INPUT in [yY]|[Yy][Ee][Ss]) echo "You input is YES" ;; [Nn]|[Nn][Oo]) echo "You input is NO" ;; *) echo "Input fales,please input yes or no!" esac
Example: operation and maintenance menu implementation version 2
[root@rocky8 bin]# vim work_menu2.sh #!/bin/bash # #********************************************************************************************** #Author: Raymond #QQ: 88563128 #Date: 2021-10-10 #FileName: work_menu2.sh #URL: raymond.blog.csdn.net #Description: The test script #Copyright (C): 2021 All rights reserved #********************************************************************************************* . /etc/init.d/functions echo -e "\E["$[RANDOM%7+31]";1m" cat <<EOF Please select: 1)Backup database 2)Clean up log 3)Software upgrade 4)Software rollback 5)Delete library and run EOF echo -e "\E[0m" read -p "Please select the number 1 corresponding to the above item-5:" MENU case $MENU in 1) action "Backup database" ;; 2) action "Clean up log" ;; 3) action "Software upgrade" ;; 4) action "Software rollback" ;; 5) action "Delete library and run" ;; *) action "INPUT FALSE!" esac
4.1.4 practice
1. Write a script createuser.sh to realize the following functions: use a user name as a parameter. If the user with the specified parameter exists, it will be displayed. Otherwise, it will be added. Set the initial password to 123456 and display the id number of the added user. When the new user logs in for the first time, he will be prompted to change the password immediately. If there are no parameters, he will be prompted: Please enter Enter user name
2. Write the script yesorno.sh to prompt the user to enter yes or no, and judge whether the user has entered yes or no, or other information
3. Write the script filetype.sh to judge the file path entered by the user and display its file type (normal, directory, link, other file types)
4. Write a script checkint.sh to judge whether the parameters entered by the user are positive integers
5. Write the script reset.sh to realize the initialization environment after system installation, including: 1. Alias 2. Environment variables, such as PS1. 3. Install common software packages, such as tree 5. Realize the setting of fixed IP, 6. The setting of vim, etc
4.2 cycle
4.2.1 introduction to cycle execution
Run a code segment repeatedly several times, usually with conditions for entering the loop and exiting the loop
Number of repeated runs
- The number of cycles is known in advance
- The number of cycles is unknown in advance
Common circular commands: for, while, until
4.2.2 for loop
[root@rocky8 bin]# type for for is a shell keyword [root@rocky8 bin]# help for for: for NAME [in WORDS ... ] ; do COMMANDS; done Execute commands for each member in a list. The `for' loop executes a sequence of commands for each member in a list of items. If `in WORDS ...;' is not present, then `in "$@"' is assumed. For each element in WORDS, NAME is set to that element, and the COMMANDS are executed. Exit Status: Returns the status of the last command executed. [root@centos7 ~]# help for for: for NAME [in WORDS ... ] ; do COMMANDS; done Execute commands for each member in a list. The `for' loop executes a sequence of commands for each member in a list of items. If `in WORDS ...;' is not present, then `in "$@"' is assumed. For each element in WORDS, NAME is set to that element, and the COMMANDS are executed. Exit Status: Returns the status of the last command executed. for ((: for (( exp1; exp2; exp3 )); do COMMANDS; done Arithmetic for loop. Equivalent to (( EXP1 )) while (( EXP2 )); do COMMANDS (( EXP3 )) done EXP1, EXP2, and EXP3 are arithmetic expressions. If any expression is omitted, it behaves as if it evaluates to 1. Exit Status: Returns the status of the last command executed.
example:
[root@rocky8 bin]# for i in a b c d;do echo $i;done a b c d [root@rocky8 bin]# for i in {a..z};do echo i=$i;done i=a i=b i=c i=d i=e i=f i=g i=h i=i i=j i=k i=l i=m i=n i=o i=p i=q i=r i=s i=t i=u i=v i=w i=x i=y i=z [root@rocky8 bin]# for i in {1..10};do echo i=$i;done i=1 i=2 i=3 i=4 i=5 i=6 i=7 i=8 i=9 i=10 [root@rocky8 bin]# for i in `seq 10`;do echo i=$i;done i=1 i=2 i=3 i=4 i=5 i=6 i=7 i=8 i=9 i=10 [root@rocky8 bin]# for file in /etc/*.conf;do echo file=$file;done file=/etc/dracut.conf file=/etc/fuse.conf file=/etc/host.conf file=/etc/idmapd.conf file=/etc/kdump.conf file=/etc/krb5.conf file=/etc/ld.so.conf file=/etc/libaudit.conf file=/etc/libuser.conf file=/etc/locale.conf file=/etc/logrotate.conf file=/etc/man_db.conf file=/etc/mke2fs.conf file=/etc/nsswitch.conf file=/etc/resolv.conf file=/etc/rsyslog.conf file=/etc/sestatus.conf file=/etc/sudo.conf file=/etc/sudo-ldap.conf file=/etc/sysctl.conf file=/etc/tcsd.conf file=/etc/vconsole.conf file=/etc/xattr.conf file=/etc/yum.conf
Format 1:
for NAME [in WORDS ... ] ; do COMMANDS; done for Variable name [ in list ];do Circulatory body done for Variable name [ in list ] do Circulatory body done
Implementation mechanism:
- Assign the elements in the list to "variable name" in turn; after each assignment, execute the loop body once; until the elements in the list are exhausted, the loop ends
- If [in WORDS...] is omitted, the positional parameter variable in "$@" is used
for loop list generation method:
- Give a list directly
- Integer list:
{start..end} $(seq [start [step]] end)
- Command to return to the list:
$(COMMAND)
- Use glob, such as *. sh
- Variable references, such as: @ , @, @,*,$#
Example: for an interview question, calculate the result of 1 + 2 + 3 +... + 100
[root@rocky8 bin]# sum=0;for i in {1..100};do let sum+=i;done ;echo sum=$sum sum=5050
example:
[root@rocky8 bin]# vim for_sum.sh #!/bin/bash # #********************************************************************************************** #Author: Raymond #QQ: 88563128 #Date: 2021-10-10 #FileName: for_sum.sh #URL: raymond.blog.csdn.net #Description: The test script #Copyright (C): 2021 All rights reserved #********************************************************************************************* sum=0 for i in {1..100};do let sum+=i done echo sum=$sum [root@rocky8 bin]# bash for_sum.sh sum=5050
Example: 99 multiplication table
[root@rocky8 bin]# vim for_99.sh #!/bin/bash # #********************************************************************************************** #Author: Raymond #QQ: 88563128 #Date: 2021-10-10 #FileName: for_99.sh #URL: raymond.blog.csdn.net #Description: The test script #Copyright (C): 2021 All rights reserved #********************************************************************************************* for i in {1..9};do echo $i done [root@rocky8 bin]# bash for_99.sh 1 2 3 4 5 6 7 8 9 [root@rocky8 bin]# vim for_99.sh #!/bin/bash # #********************************************************************************************** #Author: Raymond #QQ: 88563128 #Date: 2021-10-10 #FileName: for_99.sh #URL: raymond.blog.csdn.net #Description: The test script #Copyright (C): 2021 All rights reserved #********************************************************************************************* for i in {1..9};do for j in `seq $i`;do echo $j done done [root@rocky8 bin]# bash for_99.sh 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 9 [root@rocky8 bin]# vim for_99.sh #!/bin/bash # #********************************************************************************************** #Author: Raymond #QQ: 88563128 #Date: 2021-10-10 #FileName: for_99.sh #URL: raymond.blog.csdn.net #Description: The test script #Copyright (C): 2021 All rights reserved #********************************************************************************************* for i in {1..9};do for j in `seq $i`;do echo -e "$j\c" done echo done [root@rocky8 bin]# bash for_99.sh 1 12 123 1234 12345 123456 1234567 12345678 123456789 [root@rocky8 bin]# vim for_99.sh #!/bin/bash # #********************************************************************************************** #Author: Raymond #QQ: 88563128 #Date: 2021-10-10 #FileName: for_99.sh #URL: raymond.blog.csdn.net #Description: The test script #Copyright (C): 2021 All rights reserved #********************************************************************************************* for i in {1..9};do for j in `seq $i`;do echo -e "${j}x${i}=$[j*i]\c" done echo done [root@rocky8 bin]# bash for_99.sh 1x1=1 1x2=22x2=4 1x3=32x3=63x3=9 1x4=42x4=83x4=124x4=16 1x5=52x5=103x5=154x5=205x5=25 1x6=62x6=123x6=184x6=245x6=306x6=36 1x7=72x7=143x7=214x7=285x7=356x7=427x7=49 1x8=82x8=163x8=244x8=325x8=406x8=487x8=568x8=64 1x9=92x9=183x9=274x9=365x9=456x9=547x9=638x9=729x9=81 [root@rocky8 bin]# vim for_99.sh #!/bin/bash # #********************************************************************************************** #Author: Raymond #QQ: 88563128 #Date: 2021-10-10 #FileName: for_99.sh #URL: raymond.blog.csdn.net #Description: The test script #Copyright (C): 2021 All rights reserved #********************************************************************************************* for i in {1..9};do for j in `seq $i`;do echo -e "${j}x${i}=$[i*j]\t\c" done echo done [root@rocky8 bin]# bash for_99.sh 1x1=1 1x2=2 2x2=4 1x3=3 2x3=6 3x3=9 1x4=4 2x4=8 3x4=12 4x4=16 1x5=5 2x5=10 3x5=15 4x5=20 5x5=25 1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36 1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49 1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64 1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81
Production case: change the suffix of all files in the specified directory to bak suffix
[root@rocky8 bin]# mkdir /data/test [root@rocky8 bin]# cd /data/test [root@rocky8 test]# vim ~/for_rename.sh #!/bin/bash # #********************************************************************************************** #Author: Raymond #QQ: 88563128 #Date: 2021-10-10 #FileName: for_rename.sh #URL: raymond.blog.csdn.net #Description: The test script #Copyright (C): 2021 All rights reserved #********************************************************************************************* DIR=/data/test cd $DIR for FILE in * ;do PRE=`echo $FILE | sed -nr 's/(.*)\.([^.]+)$/\1/p'` #SUFFIX=`echo $FILE | sed -nr 's/(.*)\.([^.]+)$/\2/p'` mv $FILE $PRE.bak done [root@rocky8 test]# touch {a..c}.txt [root@rocky8 test]# bash ~/for_rename.sh [root@rocky8 test]# ls a.bak b.bak c.bak
Example: for the interview question, it is required to move all files in the directory YYYY-MM-DD / to YYYY-MM/DD /
#1 yyyy-mm-dd10.sh create YYYY-MM-DD. the current date is 365 days ago. Up to now, there are 365 directories, including 10 files, $RANDOM.log [root@rocky8 bin]# vim for_dir.sh #!/bin/bash # #********************************************************************************************** #Author: Raymond #QQ: 88563128 #Date: 2021-10-10 #FileName: for_dir.sh #URL: raymond.blog.csdn.net #Description: The test script #Copyright (C): 2021 All rights reserved #********************************************************************************************* PDIR=/data/test for i in {1..365};do SUBDIR=`date -d "-$i day" +%F` mkdir $PDIR/$SUBDIR cd $PDIR/$SUBDIR for j in {1..10};do touch $RANDOM.log done done [root@rocky8 bin]# bash for_dir.sh [root@rocky8 bin]# ls /data/test 2020-10-10 2020-11-10 2020-12-11 2021-01-11 2021-02-11 2021-03-14 2021-04-14 2021-05-15 2021-06-15 2021-07-16 2021-08-16 2021-09-16 2020-10-11 2020-11-11 2020-12-12 2021-01-12 2021-02-12 2021-03-15 2021-04-15 2021-05-16 2021-06-16 2021-07-17 2021-08-17 2021-09-17 2020-10-12 2020-11-12 2020-12-13 2021-01-13 2021-02-13 2021-03-16 2021-04-16 2021-05-17 2021-06-17 2021-07-18 2021-08-18 2021-09-18 2020-10-13 2020-11-13 2020-12-14 2021-01-14 2021-02-14 2021-03-17 2021-04-17 2021-05-18 2021-06-18 2021-07-19 2021-08-19 2021-09-19 2020-10-14 2020-11-14 2020-12-15 2021-01-15 2021-02-15 2021-03-18 2021-04-18 2021-05-19 2021-06-19 2021-07-20 2021-08-20 2021-09-20 2020-10-15 2020-11-15 2020-12-16 2021-01-16 2021-02-16 2021-03-19 2021-04-19 2021-05-20 2021-06-20 2021-07-21 2021-08-21 2021-09-21 2020-10-16 2020-11-16 2020-12-17 2021-01-17 2021-02-17 2021-03-20 2021-04-20 2021-05-21 2021-06-21 2021-07-22 2021-08-22 2021-09-22 2020-10-17 2020-11-17 2020-12-18 2021-01-18 2021-02-18 2021-03-21 2021-04-21 2021-05-22 2021-06-22 2021-07-23 2021-08-23 2021-09-23 2020-10-18 2020-11-18 2020-12-19 2021-01-19 2021-02-19 2021-03-22 2021-04-22 2021-05-23 2021-06-23 2021-07-24 2021-08-24 2021-09-24 2020-10-19 2020-11-19 2020-12-20 2021-01-20 2021-02-20 2021-03-23 2021-04-23 2021-05-24 2021-06-24 2021-07-25 2021-08-25 2021-09-25 2020-10-20 2020-11-20 2020-12-21 2021-01-21 2021-02-21 2021-03-24 2021-04-24 2021-05-25 2021-06-25 2021-07-26 2021-08-26 2021-09-26 2020-10-21 2020-11-21 2020-12-22 2021-01-22 2021-02-22 2021-03-25 2021-04-25 2021-05-26 2021-06-26 2021-07-27 2021-08-27 2021-09-27 2020-10-22 2020-11-22 2020-12-23 2021-01-23 2021-02-23 2021-03-26 2021-04-26 2021-05-27 2021-06-27 2021-07-28 2021-08-28 2021-09-28 2020-10-23 2020-11-23 2020-12-24 2021-01-24 2021-02-24 2021-03-27 2021-04-27 2021-05-28 2021-06-28 2021-07-29 2021-08-29 2021-09-29 2020-10-24 2020-11-24 2020-12-25 2021-01-25 2021-02-25 2021-03-28 2021-04-28 2021-05-29 2021-06-29 2021-07-30 2021-08-30 2021-09-30 2020-10-25 2020-11-25 2020-12-26 2021-01-26 2021-02-26 2021-03-29 2021-04-29 2021-05-30 2021-06-30 2021-07-31 2021-08-31 2021-10-01 2020-10-26 2020-11-26 2020-12-27 2021-01-27 2021-02-27 2021-03-30 2021-04-30 2021-05-31 2021-07-01 2021-08-01 2021-09-01 2021-10-02 2020-10-27 2020-11-27 2020-12-28 2021-01-28 2021-02-28 2021-03-31 2021-05-01 2021-06-01 2021-07-02 2021-08-02 2021-09-02 2021-10-03 2020-10-28 2020-11-28 2020-12-29 2021-01-29 2021-03-01 2021-04-01 2021-05-02 2021-06-02 2021-07-03 2021-08-03 2021-09-03 2021-10-04 2020-10-29 2020-11-29 2020-12-30 2021-01-30 2021-03-02 2021-04-02 2021-05-03 2021-06-03 2021-07-04 2021-08-04 2021-09-04 2021-10-05 2020-10-30 2020-11-30 2020-12-31 2021-01-31 2021-03-03 2021-04-03 2021-05-04 2021-06-04 2021-07-05 2021-08-05 2021-09-05 2021-10-06 2020-10-31 2020-12-01 2021-01-01 2021-02-01 2021-03-04 2021-04-04 2021-05-05 2021-06-05 2021-07-06 2021-08-06 2021-09-06 2021-10-07 2020-11-01 2020-12-02 2021-01-02 2021-02-02 2021-03-05 2021-04-05 2021-05-06 2021-06-06 2021-07-07 2021-08-07 2021-09-07 2021-10-08 2020-11-02 2020-12-03 2021-01-03 2021-02-03 2021-03-06 2021-04-06 2021-05-07 2021-06-07 2021-07-08 2021-08-08 2021-09-08 2021-10-09 2020-11-03 2020-12-04 2021-01-04 2021-02-04 2021-03-07 2021-04-07 2021-05-08 2021-06-08 2021-07-09 2021-08-09 2021-09-09 2020-11-04 2020-12-05 2021-01-05 2021-02-05 2021-03-08 2021-04-08 2021-05-09 2021-06-09 2021-07-10 2021-08-10 2021-09-10 2020-11-05 2020-12-06 2021-01-06 2021-02-06 2021-03-09 2021-04-09 2021-05-10 2021-06-10 2021-07-11 2021-08-11 2021-09-11 2020-11-06 2020-12-07 2021-01-07 2021-02-07 2021-03-10 2021-04-10 2021-05-11 2021-06-11 2021-07-12 2021-08-12 2021-09-12 2020-11-07 2020-12-08 2021-01-08 2021-02-08 2021-03-11 2021-04-11 2021-05-12 2021-06-12 2021-07-13 2021-08-13 2021-09-13 2020-11-08 2020-12-09 2021-01-09 2021-02-09 2021-03-12 2021-04-12 2021-05-13 2021-06-13 2021-07-14 2021-08-14 2021-09-14 2020-11-09 2020-12-10 2021-01-10 2021-02-10 2021-03-13 2021-04-13 2021-05-14 2021-06-14 2021-07-15 2021-08-15 2021-09-15 [root@rocky8 bin]# tree /data/test #2 move to YYYY-MM/DD [root@rocky8 bin]# vim for_mv.sh #!/bin/bash # #********************************************************************************************** #Author: Raymond #QQ: 88563128 #Date: 2021-10-10 #FileName: for_mv.sh #URL: raymond.blog.csdn.net #Description: The test script #Copyright (C): 2021 All rights reserved #********************************************************************************************* DIR=/data/test cd $DIR for SRC in *;do YYYY_MM=`echo $SRC | cut -d"-" -f1,2` DD=`echo $SRC | cut -d"-" -f3` [ -d ${YYYY_MM}/$DD ] || mkdir -p ${YYYY_MM}/$DD &> /dev/null mv $SRC/* ${YYYY_MM}/$DD done rm -rf $DIR/*-*-* [root@rocky8 bin]# bash for_mv.sh [root@rocky8 bin]# ls /data/test 2020-10 2020-11 2020-12 2021-01 2021-02 2021-03 2021-04 2021-05 2021-06 2021-07 2021-08 2021-09 2021-10
Interview question: scan a network segment: 172.31.0.0/21 (172.31.0.1-172.31.0.254), judge the online status of the host in this network segment, and print out the IP of the online host
[root@rocky8 bin]# vim for_scan_host.sh #!/bin/bash # #********************************************************************************************** #Author: Raymond #QQ: 88563128 #Date: 2021-10-10 #FileName: for_scan_host.sh #URL: raymond.blog.csdn.net #Description: The test script #Copyright (C): 2021 All rights reserved #********************************************************************************************* NET=172.31.0 for ID in {1..254};do { ping -c1 -w1 $NET.$ID &> /dev/null && echo $NET.$ID is up || echo $NET.$ID is down }& done wait [root@rocky8 bin]# bash for_scan_host.sh 172.31.0.2 is up 172.31.0.7 is up 172.31.0.6 is up 172.31.0.41 is down 172.31.0.45 is down 172.31.0.10 is down 172.31.0.20 is down 172.31.0.14 is down 172.31.0.43 is down 172.31.0.30 is down 172.31.0.17 is down 172.31.0.35 is down 172.31.0.16 is down 172.31.0.22 is down 172.31.0.19 is down 172.31.0.34 is down 172.31.0.40 is down 172.31.0.26 is down 172.31.0.15 is down 172.31.0.36 is down 172.31.0.46 is down 172.31.0.32 is down 172.31.0.49 is down 172.31.0.38 is down 172.31.0.24 is down 172.31.0.44 is down 172.31.0.37 is down 172.31.0.33 is down 172.31.0.11 is down 172.31.0.27 is down 172.31.0.3 is down 172.31.0.4 is down 172.31.0.8 is down 172.31.0.9 is down 172.31.0.13 is down 172.31.0.18 is down 172.31.0.23 is down 172.31.0.28 is down 172.31.0.29 is down 172.31.0.31 is down 172.31.0.39 is down 172.31.0.42 is down 172.31.0.48 is down 172.31.0.5 is down 172.31.0.12 is down 172.31.0.47 is down 172.31.0.1 is down 172.31.0.25 is down 172.31.0.21 is down 172.31.0.50 is down 172.31.0.51 is down 172.31.0.84 is down 172.31.0.66 is down 172.31.0.81 is down 172.31.0.62 is down 172.31.0.78 is down 172.31.0.76 is down 172.31.0.83 is down 172.31.0.72 is down 172.31.0.79 is down 172.31.0.57 is down 172.31.0.69 is down 172.31.0.63 is down 172.31.0.68 is down 172.31.0.52 is down 172.31.0.56 is down 172.31.0.53 is down 172.31.0.54 is down 172.31.0.61 is down 172.31.0.70 is down 172.31.0.71 is down 172.31.0.74 is down 172.31.0.82 is down 172.31.0.65 is down 172.31.0.75 is down 172.31.0.77 is down 172.31.0.64 is down 172.31.0.59 is down 172.31.0.58 is down 172.31.0.60 is down 172.31.0.73 is down 172.31.0.80 is down 172.31.0.55 is down 172.31.0.67 is down 172.31.0.85 is down 172.31.0.86 is down 172.31.0.143 is down 172.31.0.144 is down 172.31.0.150 is down 172.31.0.145 is down 172.31.0.133 is down 172.31.0.149 is down 172.31.0.121 is down 172.31.0.124 is down 172.31.0.131 is down 172.31.0.119 is down 172.31.0.132 is down 172.31.0.102 is down 172.31.0.128 is down 172.31.0.136 is down 172.31.0.116 is down 172.31.0.146 is down 172.31.0.114 is down 172.31.0.137 is down 172.31.0.99 is down 172.31.0.101 is down 172.31.0.111 is down 172.31.0.117 is down 172.31.0.151 is down 172.31.0.90 is down 172.31.0.110 is down 172.31.0.140 is down 172.31.0.87 is down 172.31.0.104 is down 172.31.0.97 is down 172.31.0.118 is down 172.31.0.93 is down 172.31.0.95 is down 172.31.0.103 is down 172.31.0.105 is down 172.31.0.134 is down 172.31.0.107 is down 172.31.0.109 is down 172.31.0.112 is down 172.31.0.130 is down 172.31.0.106 is down 172.31.0.88 is down 172.31.0.89 is down 172.31.0.92 is down 172.31.0.100 is down 172.31.0.123 is down 172.31.0.127 is down 172.31.0.129 is down 172.31.0.141 is down 172.31.0.148 is down 172.31.0.122 is down 172.31.0.94 is down 172.31.0.91 is down 172.31.0.108 is down 172.31.0.115 is down 172.31.0.125 is down 172.31.0.138 is down 172.31.0.139 is down 172.31.0.147 is down 172.31.0.113 is down 172.31.0.96 is down 172.31.0.135 is down 172.31.0.126 is down 172.31.0.98 is down 172.31.0.120 is down 172.31.0.142 is down 172.31.0.201 is down 172.31.0.189 is down 172.31.0.192 is down 172.31.0.199 is down 172.31.0.187 is down 172.31.0.200 is down 172.31.0.169 is down 172.31.0.196 is down 172.31.0.188 is down 172.31.0.184 is down 172.31.0.182 is down 172.31.0.166 is down 172.31.0.152 is down 172.31.0.163 is down 172.31.0.168 is down 172.31.0.179 is down 172.31.0.185 is down 172.31.0.203 is down 172.31.0.157 is down 172.31.0.194 is down 172.31.0.178 is down 172.31.0.180 is down 172.31.0.155 is down 172.31.0.171 is down 172.31.0.164 is down 172.31.0.186 is down 172.31.0.160 is down 172.31.0.162 is down 172.31.0.170 is down 172.31.0.202 is down 172.31.0.175 is down 172.31.0.177 is down 172.31.0.191 is down 172.31.0.209 is down 172.31.0.190 is down 172.31.0.204 is down 172.31.0.161 is down 172.31.0.165 is down 172.31.0.205 is down 172.31.0.208 is down 172.31.0.154 is down 172.31.0.156 is down 172.31.0.159 is down 172.31.0.167 is down 172.31.0.195 is down 172.31.0.197 is down 172.31.0.158 is down 172.31.0.176 is down 172.31.0.183 is down 172.31.0.193 is down 172.31.0.206 is down 172.31.0.207 is down 172.31.0.181 is down 172.31.0.210 is down 172.31.0.153 is down 172.31.0.172 is down 172.31.0.211 is down 172.31.0.212 is down 172.31.0.213 is down 172.31.0.214 is down 172.31.0.173 is down 172.31.0.174 is down 172.31.0.198 is down 172.31.0.215 is down 172.31.0.221 is down 172.31.0.224 is down 172.31.0.217 is down 172.31.0.220 is down 172.31.0.219 is down 172.31.0.222 is down 172.31.0.218 is down 172.31.0.223 is down 172.31.0.216 is down 172.31.0.225 is down 172.31.0.228 is down 172.31.0.230 is down 172.31.0.229 is down 172.31.0.226 is down 172.31.0.232 is down 172.31.0.233 is down 172.31.0.227 is down 172.31.0.231 is down 172.31.0.234 is down 172.31.0.240 is down 172.31.0.235 is down 172.31.0.243 is down 172.31.0.244 is down 172.31.0.236 is down 172.31.0.237 is down 172.31.0.239 is down 172.31.0.242 is down 172.31.0.241 is down 172.31.0.238 is down 172.31.0.251 is down 172.31.0.252 is down 172.31.0.253 is down 172.31.0.245 is down 172.31.0.254 is down 172.31.0.249 is down 172.31.0.250 is down 172.31.0.247 is down 172.31.0.248 is down 172.31.0.246 is down
Format 2
The double parenthesis method, that is ((...)) format, can also be used for arithmetic operations. The double parenthesis method can also enable bash Shell to realize variable operation in C language style
I=10;((I++))
for ((: for (( exp1; exp2; exp3 )); do COMMANDS; done for ((Control variable initialization;Conditional judgment expression;Modified expression of control variable)) do Circulatory body done
explain:
- Control variable initialization: executed only once when running to the loop code segment
- Correction expression of control variable: at the end of each cycle, the correction operation of control variable will be carried out first, and then the condition judgment will be made
example:
[root@rocky8 bin]# vim for_sum2.sh #!/bin/bash # #********************************************************************************************** #Author: Raymond #QQ: 88563128 #Date: 2021-10-10 #FileName: for_sum2.sh #URL: raymond.blog.csdn.net #Description: The test script #Copyright (C): 2021 All rights reserved #********************************************************************************************* for ((i=1;i<=100;i++));do let sum+=i done echo sum=$sum [root@rocky8 bin]# bash for_sum2.sh sum=5050 [root@rocky8 bin]# vim for_sum.sh #!/bin/bash # #********************************************************************************************** #Author: Raymond #QQ: 88563128 #Date: 2021-10-10 #FileName: for_sum.sh #URL: raymond.blog.csdn.net #Description: The test script #Copyright (C): 2021 All rights reserved #********************************************************************************************* N=100 for ((i=1;i<=$N;i++));do let sum+=i done echo sum=$sum [root@rocky8 bin]# bash for_sum.sh sum=5050
Example: 99 multiplication table
[root@rocky8 bin]# vim for_99_2.sh #!/bin/bash # #********************************************************************************************** #Author: Raymond #QQ: 88563128 #Date: 2021-10-10 #FileName: for_99_2.sh #URL: raymond.blog.csdn.net #Description: The test script #Copyright (C): 2021 All rights reserved #********************************************************************************************* for((i=1;i<10;i++));do for((j=1;j<=i;j++));do echo -e "${j}x$i=$((j*i))\t\c" done echo done [root@rocky8 bin]# bash for_99_2.sh 1x1=1 1x2=2 2x2=4 1x3=3 2x3=6 3x3=9 1x4=4 2x4=8 3x4=12 4x4=16 1x5=5 2x5=10 3x5=15 4x5=20 5x5=25 1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36 1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49 1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64 1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81
Example: isosceles triangle
[root@rocky8 bin]# vim for_triangle.sh #!/bin/bash # #********************************************************************************************** #Author: Raymond #QQ: 88563128 #Date: 2021-10-10 #FileName: for_triangle.sh #URL: raymond.blog.csdn.net #Description: The test script #Copyright (C): 2021 All rights reserved #********************************************************************************************* read -p "Please enter the number of rows of the triangle:" line for ((i=1;i<=line;i++));do for ((k=0;k<=line-i;k++));do echo -e ' \c' done for ((j=1;j<=2*i-1;j++));do echo -e '*\c' done echo done [root@rocky8 bin]# bash for_triangle.sh Please enter the number of rows of the triangle:10 * *** ***** ******* ********* *********** ************* *************** ***************** *******************
Example: build progress
[root@rocky8 bin]# for ((i=0;i<=100;i++));do printf "\e[4D%3d%%" $i;sleep 0.1s;done 100%
example:
[root@rocky8 bin]# for((;;));do echo for;sleep 1;done for for for for for for for
4.2.3 exercise: implement with for
1. Judge the types of all files in the / var / directory
2. Add 10 users user1-user10, and the password is 8 random characters
3. There are multiple files starting with K and S in / etc/rc.d/rc3.d directory respectively; Read each file separately. The output beginning with K is the file plus stop, and the output beginning with S is the file name plus start, such as K34filename stop S66filename start
4. Write a script to prompt for the value of positive integer n and calculate the sum of 1 + 2 +... + n
5. Calculate the sum of all integers within 100 that can be divided by 3
6. Write a script and prompt to enter the network address, such as 192.168.0.0, to judge the online status of the host in the entered network segment
7. Print 99 multiplication table
8. Create 10 HTML files in the / testdir directory. The file name format is the number N (from 1 to 10) plus 8 random letters, such as 1AbCdeFgH.html
9. Print isosceles triangles
10. The monkey picked several peaches on the first day and ate half of them immediately. Without addiction, he ate another one. The next morning, he ate half of the remaining peaches and another one. After that, I ate the remaining half and one every morning. When I wanted to eat again on the 10th morning, there was only one peach left. How much did you pick on the first day?