简体   繁体   English

变量不解释内部循环 shell 脚本

[英]Variable not interprate inside loop shell script

I tried to made a save inside a SSH connection from files inside a folder with a script, I don't understand why but我尝试使用脚本从文件夹内的文件在 SSH 连接中进行保存,我不明白为什么但是

  expect \"ssh>\"
    send \"for ENTRY in "pathtomyfolder"/*; do cp /DOMP/appli/bin/$(basename $ENTRY) /pathtofolder/$(basename $ENTRY).somedate; done \r\"

But it seems that my variable isn't interprate inside my script, it's empty.但似乎我的变量没有在我的脚本中解释,它是空的。 If I launched the loop from the SSH connection it works fine it's juste inside the script that it doesn't work.如果我从 SSH 连接启动循环,它工作正常,只是在脚本内部它不起作用。 If you could help me thank you.如果你能帮助我谢谢你。

Edit: Thank for you replies sorry for my lack of informations, I've made a minimal reproductible example:编辑:感谢您的回复,抱歉我缺乏信息,我做了一个最小的可复制示例:

#!/bin/sh

HORODATAGE=`date "+%Y%m%d%H%M%S"`
for entry in "folder"/*
do
  cp "\$entry" "\$entry.$HORODATAGE"
done

I've tried to escape my variable but it doesn't work too...我试图逃避我的变量,但它也不起作用......

By escaping the variables, you basically told the shell "please do not expand it".通过 escaping 变量,你基本上告诉 shell “请不要扩展它”。 Hence, you should have written it as因此,你应该把它写成

#!/bin/sh
HORODATAGE=`date "+%Y%m%d%H%M%S"`
for entry in "folder"/*
do
  cp "$entry" "$entry.$HORODATAGE"
done

BTW, I would write the assignment of the date as顺便说一句,我会把日期的分配写成

HORODATAGE=$(date "+%Y%m%d%H%M%S")

but of course, your approach is not wrong either.但当然,你的做法也没有错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM