简体   繁体   中英

Command works from command line but not from shell script

I am not much familiar with shell script. I want to modify a script but it's giving me error "No such file or directory" after I change it.

But that command works over the command prompt.

Here is the line which causes problem. (Even not sure how below command spawns a process.)

_T_COMMAND_=1 "valgrind ---tool=memcheck --trace-children=yes command"

same thing works if I run command as

$valgrind ---tool=memcheck --trace-children=yes command

Any idea?

Don't put the command in double quotes.

_T_COMMAND_=1 valgrind ---tool=memcheck --trace-children=yes command

The general syntax is simply

[var=value ...] cmd [args]

which will set the environment variable var to value for the duration of cmd . You can set several variables in this way.

Alternatively, set the variable and export it; then it will remain set for the remainder of the current shell's lifetime, and be exposed to subprocesses (that's what the export does).

_T_COMMAND_=1
export _T_COMMAND_
valgrind ---tool=memcheck --trace-children=yes command

Similarly, valgrind processes its options, then runs the specified command (with any options) as a subprocess.

A single command in double quotes is harmless, because the shell will strip the quotes before the kernel sees the argument. A string with spaces in double quotes will be preserved as a single argument, while without quotes, it becomes multiple arguments. Behold:

bash$ perl -le 'print "<<$_>>" for @ARGV' "foo bar" baz quux
<<foo bar>>
<<baz>>
<<quux>>

or just as well, add harmless but no doubt rather confusing double quotes around everything which isn't already quoted:

bash$ "perl" "-le" 'print "<<$_>>" for @ARGV' "yowza"
<<yowza>>

The shell parses this into

<<perl>>
<<-le>>
<<print "<<$_>>" for @ARGV>>
<<yowza>>

and removes the (outer) quotes in the process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM