我设置了4个shell变量
remote_account="raccname"
remote_machine="server"
First_Name="firstname"
Last_Name="lastname"
当我在我的脚本中调用remote_account
或remote_machine
时,它们会得到很好的使用
echo "What directory shall you choose?"
read dir
scp -r ~/csc60/$dir $remote_account@$remote_machine:directoryA
exit;;
但是当我把其他两个称为
时echo "What directory shall you choose?"
read dir
scp $remote_account@$remote_machine:tars/$Last_Name_$First_Name_$dir.tar.z ~/tars
exit;;
它从tars/$dir.tar.z
抓取tars文件,完全跳过$Last_Name_$First_Name_
当我在其中抛出echo $Last_Name
时仍将其显示为“姓氏”
在变量之间是否有一些使用“_”的规则,或者我只是遗漏了一些明显的东西?
答案 0 :(得分:5)
_
是变量名称的有效字符,因此您必须限定哪个部分是变量。
scp "$remote_account@$remote_machine:tars/${Last_Name}_${First_Name}_$dir.tar.z" ~/tars
答案 1 :(得分:0)
您需要使用$ {}来界定变量值,因为您声明$ Last_Name_ $ First_Name_ $ dir被视为一个变量和shell put值。在您的情况下,您没有为它定义值。它将被解释为“”。
使用${LAST_NAME}_${FIRST_NAME}