由 cron 启动的 bash 脚本中的“错误变量名称”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26251272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:41:48  来源:igfitidea点击:

"bad variable name" in bash script started by cron

bashcron

提问by steros

Works just fine when running it manually on the shell but when I setup a cronjob to run it on reboot I get "bad variable name".

在 shell 上手动运行它时工作得很好,但是当我设置一个 cronjob 以在重新启动时运行它时,我得到“错误的变量名称”。

#! /bin/sh
# /etc/init.d/duplicityCleanUp
export PASSPHRASE=foo
duplicity remove-older-than 30D --force --gio smb://remote/archiv/
duplicity remove-all-but-n-full 1 --force --gio smb://remote/archiv/
unset PASSPHRASE

回答by Vorsprung

There is a space between the #!and the /bin/sh. I don't think this is the reported problem but it needs fixing

#!和之间有一个空格/bin/sh。我不认为这是报告的问题,但需要修复

I guess that you are using a version of Unix or Linux where /bin/shis not bash so the export syntax is wrong.

我猜您使用的/bin/sh不是 bash的 Unix 或 Linux 版本,因此导出语法是错误的。

Alter your script to say

改变你的脚本说

PASSPHRASE=foo
export PASSPHRASE

See this answer UNIX export command

请参阅此答案UNIX 导出命令

回答by Muthuveerappan

They way you export or set your variables are incompatible with your shell. When executing the script - try and use different shell.

您导出或设置变量的方式与您的 shell 不兼容。执行脚本时 - 尝试使用不同的 shell。

sh yourscript.sh 
bash yourscript.sh 
ksh yourscript.sh
csh yourscript.sh
zsh yourscript.sh 

Mostly bash will work for you.

大多数情况下 bash 会为你工作。