Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm trying to do something with a cron wrapper and my cron looks something like

* * * * * root /usr/bin/wrapper cd /tmp/ && ls 

Then wrapper looks like

#!/bin/bash 

"$@"

When I run it I can't get the semi colon or && to work as I'm hoping.

[root@domain.net ~/test]# pwd
/root/test
[root@domain.net ~/test]# ls
test.sh
[root@domain.net ~/test]# ./test.sh cd /tmp && ls
test.sh

So in that case it goes into tmp then the weapper script exits.. and it runs ls in my cwd.

If I add '' around the command I get

[root@domain.net ~/test]# ./test.sh 'cd /tmp && ls'
./test.sh: line 3: cd /tmp && ls: No such file or directory

I'm guessing with the "$@" it is trying to add in the single quote to the command being run

I support I can strip out the single quotes before the "$@" but I was hoping not to do that.. any tips or is this even solvable in bash?

share|improve this question
add comment

1 Answer 1

up vote 7 down vote accepted

/usr/bin/wrapper cd /tmp/ && ls gets parsed as (/usr/bin/wrapper cd /tmp/) && ls. Even if you do manage to pass the arguments to wrapper properly, it doesn't understand them; && is a shell construct. I'm not sure exactly what you were aiming for with this wrapper script, but I suspect you can just replace it with bash -c, which takes a string and tells bash to evaluate it as though you'd typed it directly into the shell:

* * * * * root bash -c "cd /tmp/ && ls"
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.