Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question
    
commandlinefu.com –  djangofan Jun 25 '13 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? –  Scott Jun 25 '13 at 23:01
1  
question also on stackoverflow –  glenn jackman Jun 25 '13 at 23:35
    
Why the cd $HOME ? You already have an -f $HOME/mine.tar in the command. –  Hennes Dec 23 '13 at 14:43

2 Answers 2

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.

share|improve this answer

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

share|improve this answer
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 '13 at 14:45
    
I mention that in the case of bash. –  mailer Dec 24 '13 at 2:23

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.