Home  >  Article  >  Operation and Maintenance  >  Shell exception caused by a space

Shell exception caused by a space

大家讲道理
大家讲道理Original
2017-05-28 10:30:132335browse

Recently, I used shell to write a script replace.sh to back up and replace the f server program. The original intention was to save manpower and repetitive mechanical work. During the script writing process, due to negligence, an extra space was entered, and an unexpected situation occurred.

1. First take a look at the problematic replace.sh

List 1: replace.sh


 1 #!/bin/sh 2 file="mv.sh"     #日志清理脚本 3 bak="mv" 4 replace="scp" 5 hostPath="/data/kuyin_new/musicsearch/bin/logs" 6 #hostList=(172.16.72.50 172.16.72.56 172.16.72.58 172.16.72.76 172.16.72.78 172.16.72.96 172.16.72.98) 7 hostList=(172.16.72.50) 8 for host in ${hostList[*]} 9 do10     bakCmd= "ssh $host $bak ${hostPath}/$file ${hostPath}/${file}.bak"    #先备份目标机器上的脚本,这里=之后多打了一个空格11     echo $bakCmd       #这里先打印一下bakCmd命令,看一下12     #bash -c "$bakCmd"13     #replaceCmd="$replace $file ${host}:${hostPath}"   #拷贝至目标机器14     #finalCmd="${bakCmd} ; ${replaceCmd}"15     #echo $replaceCmd16     #echo $finalCmd17     #bash -c "$finalCmd"18 done


Execute the script and the results are as follows:

After entering an extra space, bash replace.sh runs the script and an unexpected situation occurs: ssh 172.16 .72.50 mv /data/kuyin_new/musicsearch/bin/logs/mv.sh /data/kuyin_new/musicsearch/bin/logs/mv.sh.bak(ssh $host $bak ${hostPath}/ $file ${hostPath}/${file}.bakResult after variablereplacement) The command was actually executed! My original intention is to use echo to check whether the command combination is correct. This command will not be executed. So why does this happen?

In order to find out the problem, you can run the script through bash -x replace.sh. The "-x" option causes the shell to display each command line it actually executes during the execution of the script, and display a "+" sign at the beginning of the line. What is displayed after the "+" sign is the content of the command line after variable substitution, which is helpful for analyzing what command is actually executed. The "-x" option is simple and convenient to use, and can easily handle most shelldebugging tasks, and should be regarded as the preferred debugging method. Execute bash -x replace.sh, the results are as follows:

 

It can be seen that line 10 in replace.sh is split into 2 commands (a total of 2 commands in the yellow box and red box), so ssh $host $bak ${hostPath}/$file ${hostPath}/${file}.bak will be executed. Next, remove the extra spaces on line 10 and see the effect.

2. Normal replace.sh

List 2: replace.sh


 1 #!/bin/sh 2 file="mv.sh"     #日志清理脚本 3 bak="mv" 4 replace="scp" 5 hostPath="/data/kuyin_new/musicsearch/bin/logs" 6 #hostList=(172.16.72.50 172.16.72.56 172.16.72.58 172.16.72.76 172.16.72.78 172.16.72.96 172.16.72.98) 7 hostList=(172.16.72.50) 8 for host in ${hostList[*]} 9 do10     bakCmd="ssh $host $bak ${hostPath}/$file ${hostPath}/${file}.bak"    ##先备份目标机器上的脚本,去除了多余的空格,此时第10行就是一个命令11     echo $bakCmd12     #bash -c "$bakCmd"13     #replaceCmd="$replace $file ${host}:${hostPath}"   #拷贝至目标机器14     #finalCmd="${bakCmd} ; ${replaceCmd}"15     #echo $replaceCmd16     #echo $finalCmd17     #bash -c "$finalCmd"18 done


The running result is as follows:

Therefore, pay special attention to spaces when writing shell scripts.

 

The above is the detailed content of Shell exception caused by a space. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn