数组声明的shellscript不工作在计划cron工作? [英] array declaration of shellscript doesnot work in scheduled cron job?

查看:185
本文介绍了数组声明的shellscript不工作在计划cron工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个名为script.sh的shell脚本,它读取数组元素并将其打印在终端上。脚本如下:

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

它在终端执行时会产生期望的输出,即'Hello World'如果我在crontab作业中安排相同的脚本,每分钟自动执行并将输出存储在另一个文件中,则作业失败并提供

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:语法错误:(意外错误。

crontab作业每分钟执行一次脚本并将输出存储在另一个文件中如下:

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.

推荐答案

在脚本的第一行之前添加:

Add this before the first line of your script:

#!/bin/bash

这篇关于数组声明的shellscript不工作在计划cron工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆