The parameter passed in the shell script contains a parameter with a space

Problem Description

Call and pass parameters to other shell scripts. The parameters passed have spaces, and the called shell script only takes the first word of this parameter.

code show as below

# 传参脚本 test_pre.sh
[wqf@b1i10 test]$ cat test_pre.sh
binFilePath="/apps/test"
logpath="/apps/test"
std_time="2023-01-31 18:00:00"
sh $binFilePath/test.sh $std_time > $logpath/test_$(date +%Y%m%d%H%M).log 2>&1 & 

# 调用脚本 test.sh
[wqf@b1i10 test]$ cat test.sh
std_time=$1
echo $std_time

The test results are as follows, you can see that only the first word of the parameter is taken

[wqf@b1i10 test]$ cat test_202302061655.log
2023-01-31

Cause Analysis

The parameter transfer of the script uses positional parameters, and the default is a space to separate the parameters at each position, so only the first half of std_time "2023-01-31" is taken.


solution

Method 1: When passing parameters , quotes will be used, and all the content in the quotes will be used as a parameter. If there is no variable in the parameter, single quotes and double quotes are fine; if there are variables, use double quotes.

code show as below

# 传参脚本 test_pre.sh
[wqf@b1i10 test]$ cat test_pre.sh
binFilePath="/apps/test"
logpath="/apps/test"
std_time="2023-01-31 18:00:00"
sh $binFilePath/test.sh "$std_time" > $logpath/test_$(date +%Y%m%d%H%M).log 2>&1 & 

# 调用脚本 test.sh
[wqf@b1i10 test]$ cat test.sh
std_time=$1
echo $std_time

Test Results

[wqf@b1i10 test]$ cat test_202302061716.log
2023-01-31 18:00:00

Method 2: Use $* or $@ to pass the parameter , and all passed parameters will be displayed in a single string. The difference is that $* will treat all included positional parameters as a whole, while $@ will treat all positional parameters as separate fields (equivalent to a list). They are used the same in this example.

code show as below

# 传参脚本 test_pre.sh
[wqf@b1i10 test]$ cat test_pre.sh
binFilePath="/apps/test"
logpath="/apps/test"
std_time="2023-01-31 18:00:00"
sh $binFilePath/test.sh $std_time > $logpath/test_$(date +%Y%m%d%H%M).log 2>&1 & 

# 调用脚本 test.sh
[wqf@b1i10 test]$ cat test.sh
#取第一个参数到的所有内容(直到最后)
std_time=${
    
    @:1}
echo $std_time

# 调用脚本 test.sh
[wqf@b1i10 test]$ cat test.sh
#取第一个参数到的所有内容(直到最后)
std_time=${
    
    *:1}
echo $std_time

The test results of the above codes are all the same and will not be displayed one by one.

Note : Assuming that the parameter to be passed contains multiple spaces, it needs to indicate how many spaces it contains in order to use it correctly. For example, if it is the second parameter, the parameter contains two spaces and can be written as: ${@:2:3}


expand

One of the parameters passed to the function contains spaces

A parameter passed to the function contains spaces. In addition to the above two methods, there is also a third method: directly use the corresponding variable in the function and call the script. There is no way to pass variables in this way.

Suppose the code of the function script is as follows

[wqf@b1i10 ~]$cat test_1.sh
test_param(){
    
    
std_time=$1
echo "std_time:" $std_time
}

std_time="2023-01-31 18:00:00"
echo "std_time:" $std_time
test_param $std_time

Test Results

[wqf@b1i10 ~]$ sh test_1.sh
std_time: 2023-01-31 18:00:00
std_time: 2023-01-31

The code for method three is as follows:

[wqf@b1i10 ~]$cat test_1.sh
test_param(){
    
    
std_time=$std_time
echo "std_time:" $std_time
}

std_time="2023-01-31 18:00:00"
echo "std_time:" $std_time
test_param 

Test results using method three:

[wqf@b1i10 ~]$ sh test_1.sh
std_time: 2023-01-31 18:00:00
std_time: 2023-01-31 18:00:00

The code to use Method 1 is as follows:

[wqf@b1i10 ~]$cat test_1.sh
test_param(){
    
    
std_time=$1
echo "std_time:" $std_time
}

std_time="2023-01-31 18:00:00"
echo "std_time:" $std_time
test_param "$std_time"

The code for using Method 2 is as follows:

[wqf@b1i10 ~]$cat test_1.sh
test_param(){
    
    
std_time=${
    
    @:1}
echo "std_time:" $std_time
}

std_time="2023-01-31 18:00:00"
echo "std_time:" $std_time
test_param "$std_time"


[wqf@b1i10 ~]$cat test_1.sh
test_param(){
    
    
std_time=${
    
    *:1}
echo "std_time:" $std_time
}

std_time="2023-01-31 18:00:00"
echo "std_time:" $std_time
test_param "$std_time"

The above test results are consistent and will not be displayed one by one.

Guess you like

Origin blog.csdn.net/sodaloveer/article/details/128904265