A little test of the operation and maintenance shell script (10): the shift built-in command reads multiple command line parameters


A little test of operation and maintenance shell script (1)

A little test of operation and maintenance shell script (2)

A quick test of operation and maintenance Shell script (3)::$(cd $(dirname $0); pwd) command detailed explanation

A little test of operation and maintenance Shell script (4): multi-layer nesting if...elif...elif...else fi_Snail Yang Ge's Blog-CSDN Blog

Cenos7 installation train program animation

A little test of the operation and maintenance shell script (5): until loop

A little test of operation and maintenance Shell script (6): Function recognition in Shell

A little test of operation and maintenance Shell script (7): Call a function in another script file from a function file

A little trial of operation and maintenance shell script (8): case mode ignores the case mode of command line parameters demonstration

Operation and maintenance Shell script master test (9): redirection operator ">" and double redirection ">>"

 A little trial of operation and maintenance shell script (10)

Operation and maintenance shell test (11): for loop reads multiple command line parameters 


1: Read positional parameters

Using shift [n], you can read multiple positional parameters; it is more convenient to use case for a single command line parameter; but multiple command line parameters are not reasonable. Use the command shift command to get the command line parameters one by one in the variable, shift It is a built-in command in Bash. This command is used to shift the passed parameter variable to the left. Its syntax: shift [n]; n is a non-negative integer less than or equal to "$#". If 0, the positional argument will not be changed; if not specified, it will be set to 1 by default. If n is greater than $#, the positional parameters will not change. If n is greater than $# or less than 0, the status code of this status will be greater than 0, otherwise it will be 0;


[root@www standandinout]# cat shiftreadparam.sh 
#!/bin/bash -
#================================ ================================================== =================================
#
#
# FILE: shiftreadparam.sh
# USAGE: ./shiftreadparam.sh
# DESCRIPTION: Read positional parameters: Use shift [n] to read multiple positional parameters; it is more convenient to use case for a single command line parameter; but multiple command line parameters are not reasonable, use the command shift command to read one after another in the variable Get the command line parameters directly.
# shift is a built-in command in Bash. This command is used to shift the passed parameter variable to the left. Its syntax: shift [n]; n is a non-negative integer less than or equal to "$#". If 0, the positional parameter will not be changed;
# If not specified, it will be set to 1 by default. If n is greater than $#, the positional parameters will not change.
# If n is greater than $# or less than 0, the status code of this status will be greater than 0, otherwise it will be 0; # # #
OPTIONS
:                
-------
# REQUIREMENTS: ---------

# BUGS: ------
# NOTES: -------- 
# AUTHOR: ---------YangGe (TOM) , [email protected]
# ORGANIZATION:
# CREATED: 2023-8-24 09:11:20    
# REVISION: --------
#
#
#
#
#
#========== ================================================== ================================================== ======
flag=0
# If the number of command line parameters is not 0, continue the while loop, otherwise exit the loop
while [ $# -ne 0 ]
do
  
 # Print the value of special variable $1 and special variable $# The value
 echo "Current Parameter: $1, Remaining $#."

# The flag variable is incremented by 1 operation
 flag=`expr $flag + 1`
 # Pass the value of $1 to a function or other operation
 # Pass $1 to some bash function or do whatever
 # Shift the positional parameter to the left by one
 shift
 # If the variable flag If it is greater than 5, break out of the loop
 if [ $flag -gt 5 ]
 then
    break
 fi
done

echo "================================================ ====================The end of the first while loop======================== ========================================="

# The condition for exiting the while loop is changed to the value of $1 not being empty;

while [ -n "$1"
do
   
 # Print the value of special variable $1 and the value of special variable $#
 echo "Current Parameter: $1, Remaining $#."

 # Pass $1
 to some bash function or do whatever

 # Shift the positional parameter left by one
 shift

done
echo "================================================ =====================End of the second while loop======================== =========================================="


Two: Script Test 


[root@www standandinout]# ./shiftreadparam.sh one two three four
Current Parameter: one, Remaining 4.
Current Parameter: two, Remaining 3.
Current Parameter: three, Remaining 2.
Current Parameter: four, Remaining 1.
====================================================================第一个while循环结束=================================================================
====================================================================第二个while循环结束=================================================================
[root@www standandinout]# ./shiftreadparam.sh one two three four five
Current Parameter: one, Remaining 5.
Current Parameter: two, Remaining 4.
Current Parameter: three, Remaining 3.
Current Parameter: four, Remaining 2.
Current Parameter: five, Remaining 1.
====================================================================第一个while循环结束=================================================================
====================================================================第二个while循环结束=================================================================
[root@www standandinout]# ./shiftreadparam.sh one two three four five six
Current Parameter: one, Remaining 6.
Current Parameter: two, Remaining 5.
Current Parameter: three, Remaining 4.
Current Parameter: four, Remaining 3.
Current Parameter: five, Remaining 2.
Current Parameter: six, Remaining 1.
================================================== ==================The end of the first while loop========================== =======================================
=========== ================================================== =======End of the second while loop====================================== ============================
[root@www standandinout]# ./shiftreadparam.sh one two three four five six seven eight
Current Parameter : one, Remaining 8.
Current Parameter: two, Remaining 7.
Current Parameter: three, Remaining 6.
Current Parameter: four, Remaining 5.
Current Parameter: five, Remaining 4.
Current Parameter: six, Remaining 3.
====================================================================第一个while循环结束=================================================================
Current Parameter: seven, Remaining 2.
Current Parameter: eight, Remaining 1.
====================================================================第二个while循环结束=================================================================
[root@www standandinout]# ./shiftreadparam.sh one two three four five six seven eight ten 
Current Parameter: one, Remaining 9.
Current Parameter: two, Remaining 8.
Current Parameter: three, Remaining 7.
Current Parameter: four, Remaining 6.
Current Parameter: five, Remaining 5.
Current Parameter: six, Remaining 4.
================================================== ==================The end of the first while loop========================== =======================================
Current Parameter: seven, Remaining 3.
Current Parameter: eight, Remaining 2.
Current Parameter: ten, Remaining 1.
===================================== ===============================End of the second while loop============== ================================================== ==


Lead to questions: 

       For example, if we change the shift command in the script to 5 and pass a series of parameters, if the passed parameters cannot be divisible by 5; if we pass 9 command line parameters, after the first execution of $1, the value of $1 becomes six, The value of $# becomes 2; after the first shift operation, no more shifts will be performed, and the value of $1 is not empty, and $# is not 0, so iteration continues indefinitely. ,To solve this problem, you can use the exit status of ,shift. Check whether the shift command is executed by adding $? after the shift command to see if the value is 0. If the value of $? is non-zero, then terminate the loop

Three: shift [5] combined with $? to exit the loop


[root@www standandinout]# cat shift5readparam.sh 
#!/bin/bash -
#================================ ================================================== =================================
#
#
# FILE: shiftreadparam.sh
# USAGE: ./shiftreadparam.sh
# DESCRIPTION: Read positional parameters: Use shift [n] to read multiple positional parameters; it is more convenient to use case for a single command line parameter; but multiple command line parameters are not reasonable, use the command shift command to read one after another in the variable Get the command line parameters directly.
# shift is a built-in command in Bash. This command is used to shift the passed parameter variable to the left. Its syntax: shift [n]; n is a non-negative integer less than or equal to "$#". If 0, the positional parameter will not be changed;
# If not specified, it will be set to 1 by default. If n is greater than $#, the positional parameters will not change.
# If n is greater than $# or less than 0, the status code of this status will be greater than 0, otherwise it will be 0; # # #
OPTIONS
:                
-------
#        REQUIREMENTS: ---------

#        BUGS: ------
#        NOTES: -------- 
#        AUTHOR: ---------YangGe (TOM) ,[email protected]
#    ORGANIZATION:
#        CREATED: 2023-8-24 09:11:20    
#        REVISION: --------
#
#
#
#
#
#====================================================================================================================

# If the number of command line parameters is not 0, continue the while loop, otherwise exit the while loop
[ $# -ne 0 ]
do
  
 # Print the value of special variable $1 and the value of special variable $#
 echo "Current Parameter: $1, Remaining $#."
 
 # Pass the value of $1 to a function or other operation
 # Pass $1 to some bash function or do whatever
 # Shift the positional parameter to the left by one
 shift 5
 # If the return value of the previous command is not 0, execute if statement
 if [ $? -ne 0 ]
 then
   # Terminate the execution of the loop
   break
 
 fi
 
done

echo "================================================ ====================The end of the first while loop======================== ========================================="

echo "==========================================Loop terminated=== ================================================== ==============================================="
 

Four: Test script command execution 


[root@www standandinout]# ./shift5readparam.sh one two three four five six senven
Current Parameter: one, Remaining 7.
Current Parameter: six, Remaining 2.
=============== ================================================== ===End of first while loop========================================== ========================
========================== ===============Loop terminated================================ ================================================== ===================
 

Guess you like

Origin blog.csdn.net/u014635374/article/details/132899401