syntax error near unexpected token in bash using perl
Hi,
I am getting this problem while running the command from shell. If I run the command directly on command prompt, its running fine. Here, i used the back slash also to skip the special purpose of single quote, but getting the below problem. Command is: sh -c '(perl -pi -e \'print "TextContents" if $.==13\' "/usr/local/etc/stunnel/stunnel.conf")' bash: syntax error near unexpected token `"/usr/local/etc/stunnel/stunnel1.conf")'' If someone can help me, it would be great! This command is for entering a line TextContents to a file stunnel.conf file at line number 13. Regards Ambrish |
It looks like you've got a real newline break between the 2 lines there, which bash will interpret as separate cmds.
Either write it on 1 line or use '\' at the end of the 1st line, which bash will recognise as a line continuation. It's important to know that nothing except the newline char must appear after the '\' . Ps please post your distro in your profile. |
messy, why don't you just make it a proper little script?
;) |
Hi Chris,
The command I gave is on one line only. Once again I give, its sh -c '(perl -pi -e \'print "TextContents" if $.==13 \' "/usr/local/etc/stunnel/stunnel.conf")' and the error which I am getting is : bash: syntax error near unexpected token `"/usr/local/etc/stunnel/stunnel1.conf")'' So, i suppose its the filename at the end with path which is not properly in quotes. But i am not getting exactly how to change this. Also, i updated my profile but i am not sure what exactly is meaning of distro! Regards Ambrish |
Billy,
Its part of one particular module wherein much more scripts are running, so i cannot make a separate script. Its just one command which I am passing to another function. Thats why I need to do it in this way. Regards Ambrish |
can someone update. the error is just coming, i suppose, while giving the file name in double quotes. But i am not getting, how to remove this!! :(
|
Why are you using perl, & its complexity, for something that is almost trivial w/ sed?
Code:
# set insert line # 2nd, why are you using sh -c, rather than bash -c, your script is, after all, a bash script? In fact why are you using either? -- Can't you just put the perl command directly into the script? Next, why the parentheses? I don't think they are normal w/ [ba]sh -c. Finally, once again, why even use perl for this. To my way of thinking, it's swatting flies w/ a sledge hammer. |
Rick,
Your queries are quite correct but its a project which is already running and its already developed by someone else. We are in maintainence phase wherein need to do these updates. Per ur queries: Did you mean "from shell" to mean "inside a script"? ==> Yes!! 2nd, why are you using sh -c, rather than bash -c, your script is, after all, a bash script? In fact why are you using either? -- Can't you just put the perl command directly into the script? ==> Project is already developed. We need to do this small change. Next, why the parentheses? I don't think they are normal w/ [ba]sh -c. ==> I can give my command only in parentheses as the command is passed on to some other function which is running this command. Finally, once again, why even use perl for this. To my way of thinking, it's swatting flies w/ a sledge hammer. ==> Perl is already used in this project, so i have to use it!! Regards Ambrish |
I wouldn't call that a complex perl script.
also sed -i is not portable. :) |
Let me clarify: I wasn't saying that that snippet of perl was complex, but rather that perl itself is a complex language.
As for sed -i not being portable, that can be worked around w/ temp files -- which might be a safer way of doing things anyway. So you/we are stuck w/ what is there. :) General trouble shooting Q's: Is this breakage a recent occurrence? If so, can you think of anything else that changed around the same time that might be the cause? Based on your clarification (for which, thanks), you're going to have to trouble shoot in place or in a dummy test script. It might help if you would outline the scripts/programs involved & who is passing what to whom. I think you are going to have trace what has become of your values vis-a-vis quoting rules at each step of the way. I suspect you're going to be reading the bash, & maybe other, man pages more than once. It would also be helpful to know what you are allowed to change & what is off-limits. |
That's funny. When I copy and paste your original problem line
Code:
sh -c '(perl -pi -e \'print "TextContents" if $.==13\' "/usr/local/etc/stunnel/stunnel.conf")' Code:
bash: syntax error near unexpected token `"/usr/local/etc/stunnel/stunnel1.conf")'' Code:
-bash: syntax error near unexpected token `)' I did the following at the command prompt. Note which commands responded with Thursday when I asked for yesterday. (All of them either do that or fail completely, except the first one, which doesn't invoke sh at all.) Code:
wally:~/thursday$ date Code:
wally:~/thursday$ date Bingo. Your first change is from invoking sh to invoking bash. But wait. There's more. You should be aware of the way bash works with single and double quotes. The normal escape mechanism with backslash ("\") doesn't work as you'd think inside single quotes. Try these at the command line: Code:
echo '\'' No, you want double quotes around the whole thing (that is, a double quote just after the "-c " and a double quote at the end of the whole command. If you do this, you'll want to escape (precede with backslash) the dollar sign ("$") and every inner double quote, but no inner single quote. So your command will look like this: Code:
bash -c "(perl -pi -e 'print \"TextContents\" if \$.==13' \"/usr/local/etc/stunnel/stunnel.conf\")" But you can also lose the parentheses. The effect of single parentheses is to fire up a new shell, but you're doing that already. That leaves you with: Code:
bash -c "perl -pi -e 'print \"TextContents\" if \$.==13' \"/usr/local/etc/stunnel/stunnel.conf\"" Code:
bash -c "perl -pi -e 'print \"TextContents\" if \$.==13' /usr/local/etc/stunnel/stunnel.conf" Try these changes both on the command line and in your script. Hope this helps. |
Oh. And.
"Distro" is slang for "distribution". See the other posts in this thread for examples. I re-read what I wrote, and have to ask: Why is it again that you need parentheses in the -c option? Can you be more specific? |
Great analysis, wish I understood bash quoting well enough to have written that.
kambrish, I know you may be asleep, but please post back soonest. |
wjevans,
Thanx alot for the wonderful explanation. I tried it (from command line) and it worked!! Indeed, i am not an expert in shell, but your explanation has helped alot. But let me give u little explanation of how its going in code and why I am using sh -c instead bash: My code looks as below: $exec_info = ginstall_common::execute_backquoted_command( "perl -pi -e \'print \"TextContents\" if \$\.==13\' \"\/usr\/local\/etc\/stunnel\/stunnel1.conf\"", ); The command goes like this in the function execute_backquoted_command Now, if you see part of execute_backquoted_command where the above command is taken, it looks like below: verbose::verbose(3, "sh -c '(@{commands_list})' " . "> $ginstall_common::stdout_capt_fname 2> $ginstall_common::stderr_capt_fname"); So, here sh -c is coming which I cannot change as it impacts other modules too. For me only option is to pass the command to the function. Ok, if this sh -c solution doesnt work, is there any other way or any other single command by which I can insert text contents at some particular line? Kind Regards Ambrish |
Quote:
The first is this: I cannot imagine that changing the function to use bash instead of sh would harm other users, unless the software is also being used where bash is not available. Is there any way you can push through a change to the function? (I realize that what I'm asking is often foolish in a larger software environment.) The second is this: If you can't or shouldn't change the function, can you add a new one which uses bash instead of sh? As I said, lame, but ... (sigh) EDIT: I just thought of a third way. Since it appears that passing parameters works with bash but not with sh (see my example above with Wednesday/Thursday), and you're restricted to using Perl, is there any way you can write a special-purpose Perl script which performs the file modification and uses environment variables rather than command-line parameters? |
All times are GMT -5. The time now is 06:07 PM. |