2

While executing the below script, I'm getting an error:

Syntax error at line 1 : `(' is not expected.

sudo su - user1 -c "cd $HOME ; tar -cvf $HOME/mine.tar `cat /tmp/$LOGNAME/List`  "

where List file contains the name of some other files.

Please help me out with some solution.

Regards, Sumit

4
  • commandlinefu.com
    – djangofan
    Jun 25, 2013 at 22:57
  • Standard debugging technique: break down the problem until you find the smallest example that causes it. What happens if you say sudo su - user1 -c date? How about sudo su - user1 -c sh and then type cd $HOME ; tar -cvf $HOME/mine.tar `cat /tmp/$LOGNAME/List` into the shell? Jun 25, 2013 at 23:01
  • 1
    question also on stackoverflow Jun 25, 2013 at 23:35
  • Why the cd $HOME ? You already have an -f $HOME/mine.tar in the command.
    – Hennes
    Dec 23, 2013 at 14:43

2 Answers 2

0

Your shell is expanding things too early.

Does $LOGNAME have a ( in it? Or the contents of /tmp/$LOGNAME/List?

Bash will expand $HOME and $LOGNAME before they get passed to su - when su then tries to pass them off to sh or bash, they have improper tokens in them.

0

Please make that single line commands to multiple line and execute, like write this to run.sh

#!/usr/bin/env bash
cd $HOME
tar -cvf $HOME/mine.tar
cat /tmp/$LOGNAME/List

Give execution permission (chmod +x run.sh) and run that script as sh path to the file(run.sh)

e.g: sh /home/run.sh

2
  • 2
    Two minor points: 1) No need to make it executable or add the shebang line if you run it with sh scriptfile. 2) sh might not be bash, so in this example sh run.sh and /home/run.sh might run under different shells. This matters for more complex scripts which may try to use bash-only features.
    – Hennes
    Dec 23, 2013 at 14:45
  • I mention that in the case of bash.
    – mailer
    Dec 24, 2013 at 2:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .