array declaration of shellscript doesnot work in scheduled cron job?

寵の児 提交于 2020-01-06 12:29:27

问题


I have created a shell script named "script.sh" which reads an arrays elements and prints it on terminal. The script is as follows:

 arr=("hello" "world")
for i in ${arr[@]}
do
echo  $i;
done

It gives expected output i.e 'Hello World' on executing it in terminal, but if I schedule the same script in crontab jobs to get executed automatically every minute and store the output in another file,the job fails and gives

/home/vikash/script.sh: 1: Syntax error: "(" unexpected error.

The crontab job to execute the script every minute and store the output in another file is as follows:

* * * * * $HOME/script.sh >> $HOME/output.log 2>&1

How to use array in this scenario?? please help.


回答1:


Add this before the first line of your script:

#!/bin/bash



回答2:


if this question is still valid, just run your script with

bash script.sh

I also tried it with sh somehow it is not working with sh but with bash it suns smooth.



来源:https://stackoverflow.com/questions/15209607/array-declaration-of-shellscript-doesnot-work-in-scheduled-cron-job

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!