my working shell is csh and even though if I try to run my script in plain sh, it behaves the same way. Here's a simple script:
I want $desc to be passed as an argument to another command, but csh apparently doesn't like spaces in the string and interprets the value as this:
Quote:
sh -x script.sh
+ desc='"test my changes"'
+ cmd='echo "test my changes"'
+ echo '"test' my 'changes"'
"test my changes"
This is the real problem as the subsequent command (in reality it's another script instead of an echo doesn't understand the value of $desc when it sees
Quote:
echo '"test' my 'changes"'
Any solutions how to properly escape spaces in a string variable?
What's your ultimate goal? I'm not quite understanding your need to jam everything into a string with extra quotation marks instead of just passing the strings you want in the first place... Instead of
couldn't you just run
in the first place?
Or, what exactly are you trying to pass, into what? Don't just show me code that doesn't work, I can't guess what you actually do want from it.
That still doesn't explain the purpose behind this odd goal, which is what I was wondering, because there may be more straightforward ways to do it than the design you've chosen.
In sh, you can try eval "$cmd" to force it to re-evaluate the line again after the first expansion, which will indeed interpret the literal quotes as a string parameter.
This is dangerous however, because eval accepts all valid shell syntax, including backticks and variables. Someone could feed it a tailored string to extract variables from your program or run whatever commands they wanted.
Because of this you may wish to make the string into
to prevent $COMMENT from being expanded. eval will expand it for you, but won't interpret its contents as anything other than string. I don't think this is possible in C shell, only sh.
Last edited by Corona688; 06-21-2011 at 02:36 PM..
Sorry for not being clear. I'm trying to create a wrapper script that will call either script1 or script2 depending on what option users select on command line when calling my script. One more input that my script takes and later passes to script2 is the comment line which can be a string with spaces. I'm then trying to pass that string as an argument to script2. The string has to be enclosed within double quotes for script2 to accept it properly.
I still don't see any need to put it all in one string like that, then. Just keep them separate, and run them like normal. You can do the double-quoting yourself instead of torturing the syntax.
Hello.
In csh if I declared a variable to be a set of arguments can I retrieve a particular element from that set.
My code
set files=(`ls`)
and I want to get only one file from $files. How can I do that????(It is just an abstract example):wall:
Thanks in advance :) (5 Replies)
Hello all. I am a newb obviously and a bit stumped on this, so any help gratefully accepted.
The script is extracting metadata from individual mp3 files, then (hopefully will be) sorting them into newly-created subdirectories. I have filtered out the relevant metadata and have the album names... (8 Replies)
Hi, i have this text:
X (m) 4917536.9627 4917536.9673 0.0090 -0.0046
Y (m) -815726.1383 -815726.1294 0.0061 -0.0089
Z (m) 3965857.4730 3965857.4840 0.0071 -0.0110
X (m) 4917536.9627 4917537.1411 -0.1784 0.1710
Y (m) -815726.1383 -815726.4859 0.3476 0.3489
Z (m) 3965857.4730... (2 Replies)
I am reading a number of files but then I want to put the ranges
xmin xmax ymin ymax
as arrays for each file.
Any idea how I can do this???
set j = 1
echo "Welcome $i times"
while ( $j <= $i )
echo "$j"
set fname = $fin-bst-misf.xy
echo " "$fname
... (0 Replies)
I have a simple script that sets a value and reads the value in csh:
set -x
set a = 10
echo $a
The output of the script does not show the value of a
+ set a = 10
+ echo
any help would be great. (4 Replies)
My shell is csh and it is required.
I have a file like sample.txt
------------------------
a b c
d
e
f
g h i
------------------------
I want set the file to a variable and print it out in the same format.
I have tried something like this, but not succed.
% cat ~/tmp/sample.txt
a b c
d... (8 Replies)
I am new to scripting and I needed to know if there would be an easy way to delete extra spaces in a text file. I have a file with three rows with 22 numbers each, but there is extra spaces between the numbers when it gets output by this program AFNI that I am using. What script would help delete... (2 Replies)
Hello all
i need to pass to my shell script parameter that looks like "2 3 3"
inside the script i need to use this string that looks like this "2 3 3"
but when i try to print the script im getting syntax error , this is my script :
set s = $1
echo $s (1 Reply)
I am trying to strip all leading and trailing spaces of a shell variable using either awk or sed or any other utility, however unscuccessful and need your help.
echo $SH_VAR | command_line Syntax.
The SH_VAR contains embedded spaces which needs to be preserved. I need only for the leading and... (6 Replies)