Passing arguments with " symbols


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Passing arguments with " symbols
# 1  
Old 09-03-2008
Question Passing arguments with " symbols

I have an executable that processes under the basic premise of:
Code:
program file_in file_out "parameters a b c"

However, we often create statements like the following:
Code:
program file1 + file2 + file3 file_out "parameters a b"

or even:
Code:
program file1 + file2 + file3 file_out "parameters a b" "parameters a d"

Please note that the paramaters can have variable text, there can be more than one set of parameters, but they must be inside double-quotes.

My problem is that I need to modify the file permissions on the file_out. When I thought that the first example above was the only way to run, that was easy since I could do a:

Code:
FILE_OUT="$2"
chmod g+w $FILE_OUT

But, my FILE_OUT could be at $2 or in the above examples $6 or in lots of other places.

I was thinking of outputting each $variable to a file, grep to exclude the " lines, and then the last line would be my output. However, the following code is not maintaining the " symbols; thus I cannot grep.

Code:
NUM_VARS="$#"
count=1
while [ $count -le "$NUM_VARS" ]
   do
   echo "$count" "$#" "$*"
   echo "$1" >>$temp_file
   shift
   count=$((count+1))
done

program file1 + file2 + file3 fileout "parm1 a b" "parm2 b d"
My output $temp_file looks like:
Code:
file1
+
file2
+
file3
fileout
parm1 a b
parm2 b d

Thoughts anyone?
# 2  
Old 09-03-2008
Switch files and parmeters

Switch files and parms on the command line since what you have is a non-standard implementation. According to the standard parameters must precede files that are provided on the command line implying that the output file would be the last one on the command line.
# 3  
Old 09-03-2008
Can't switch the order

The order shown is the order the line must be in. I am trying to front-end an existing executable, and that executable is requiring the input to be laid out in that manner - I wish I could change the order of the input parameters.
But, I must follow the:
Code:
program file_in file_out "parameters a b c"

# 4  
Old 09-03-2008
Test for file existence

How about testing for the existence of a file and shifting the command line parameters conditionally??
The input files should all exist (barring typos or errors) but the output file doesn't as it will be created after processing through the executable. Is that a correct assumption??
I modified your while loop based on that premise...

Code:
NUM_VARS="$#"
count=1
while [ $count -le "$NUM_VARS" ]
do
   if [ ! -f $1 ]; then
      OFILE=$1
   else
      shift
   fi
   count=$((count+1))
done
echo Output File is $OFILE

# 5  
Old 09-03-2008
If that's the only file which is created during the execution of this program, maybe you can twiddle your umask
# 6  
Old 09-03-2008
You can grep the parameters for a space, if you find a space the previous parameter is the output file, something like:

Code:
#!/bin/sh

for i in "$@"; do
  if echo "$i" | grep ' ' > /dev/null 2>&1; then
    echo file_out = "${out_file}"
  fi
  out_file="$i"
done

Regards
# 7  
Old 09-04-2008
Using echo | grep is really kind of excessive when you have case

Code:
case $i in *' '*) echo file_out="$out_file";; esac

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to avoid "Too many arguments" error, when passing a long String literal as input to a command?

Hi, I am using awk here. Inside an awk script, I have a variable which contains a very long XML data in string format (500kb). I want to pass this data (as argument) to curl command using system function. But getting Too many arguments error due to length of string data(payloadBlock). I... (4 Replies)
Discussion started by: cool.aquarian
4 Replies

2. Shell Programming and Scripting

Passing multiple arguments

Hi, I know with getopts you can pass arguments from the command line ./script -ab -c apple But it doesn't support 2 or more arguments for ONE option. Is there any other way to do this? Thanks (2 Replies)
Discussion started by: testa500
2 Replies

3. Shell Programming and Scripting

Passing arguments--Error

Hi, i have a file.txt with data Bangalore Chennai Hyd filename of the script is: new.sh result=`cat file.txt | grep $1` if then echo pass else echo fail fi i am executing the file in the cmd line as "sh new.sh Bangalore" o/p is pass if i give "sh new.sh delhi" o/p is... (6 Replies)
Discussion started by: harsha85
6 Replies

4. Shell Programming and Scripting

Reading a string and passing passing arguments to a while loop

I have an for loop that reads the following file cat param.cfg val1:env1:opt1 val2:env2:opt2 val3:env3:opt3 val4:env4:opt4 . . The for loop extracts the each line of the file so that at any one point, the value of i is val1:env1:opt1 etc... I would like to extract each... (19 Replies)
Discussion started by: goddevil
19 Replies

5. Programming

Passing arguments to shellcode

Is there any way I could pass arguments to shellcode. My goal is to store a program in a image file, and have another program read and run the code with arguments in memory. Currently I can store a program in a image file, then read it back to the hard-drive run it normally then delete it when... (5 Replies)
Discussion started by: image28
5 Replies

6. Shell Programming and Scripting

Perl: How to Print symbols like " and ;

Hi, How do I print a line with symbols in a file? Exp: If I want to print line: Hi "Lisa;John" Command: print FILE "Hi "Lisa;John""; - will give me error Bareword found where operator expected... Can someone advise how can I print any line consiting symbols like example above. Thanks... (3 Replies)
Discussion started by: SSGKT
3 Replies

7. UNIX for Dummies Questions & Answers

Passing arguments

I need to pass arguments to a shell script.My batch is calling some java program. ################# x=$1 y=$2 java -classpath program ################### if first parameter and second parameter is null then java -classpath program if first parameter is not null and second parameter is... (3 Replies)
Discussion started by: mnjx
3 Replies

8. Shell Programming and Scripting

passing arguments

Hi I have a script to which I pass multiple arguments, for example lets say the script name is "abc". I run the script like ./abc def /file <directory location> In the above "def" is the first argument and "/file" is the second argument. I expect <directory location> that is passed after... (4 Replies)
Discussion started by: zmfcat1
4 Replies

9. Shell Programming and Scripting

Passing Arguments-Help

Hi, I have a script which adds the user credentials to an ldap server. Im passing the variables as below.. /path/my_script $uname $pwd $environ ${deposit} If i enter some special characters like ';' in $pwd, script returns an error which is set to display if the user enters... (5 Replies)
Discussion started by: Tuxidow
5 Replies

10. UNIX for Dummies Questions & Answers

passing arguments

I'm trying to pass a filename, or all the files in the current directory to the ls command with a script. Unsuccessful so far, here are a few of my attempts: #!/bin/ksh read fname #if (( $# > 0 )); then $fname | ls -l #fi this produces a long listing of all the files in my current... (4 Replies)
Discussion started by: jpprial
4 Replies
Login or Register to Ask a Question