The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
passing strings as arguments iago UNIX for Dummies Questions & Answers 1 08-22-2007 07:04 AM
Passing and using arguments in Scripts. David.Vilmain Shell Programming and Scripting 1 11-13-2006 02:32 PM
Passing arguments to a script Kevin Pryke Shell Programming and Scripting 3 06-14-2002 06:06 AM
Passing arguments to an alias pmcg UNIX for Dummies Questions & Answers 1 10-23-2001 08:15 AM
passing arguments jpprial UNIX for Dummies Questions & Answers 4 04-03-2001 08:13 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 09-03-2008
joeyg's Avatar
Moderator
 

Join Date: Dec 2007
Location: Home of world champion Boston Celtics
Posts: 983
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?
Reply With Quote
Forum Sponsor
  #2  
Old 09-03-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 568
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.
Reply With Quote
  #3  
Old 09-03-2008
joeyg's Avatar
Moderator
 

Join Date: Dec 2007
Location: Home of world champion Boston Celtics
Posts: 983
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"
Reply With Quote
  #4  
Old 09-03-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 568
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
Reply With Quote
  #5  
Old 09-03-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
If that's the only file which is created during the execution of this program, maybe you can twiddle your umask
Reply With Quote
  #6  
Old 09-03-2008
Moderator
 

Join Date: Feb 2007
Posts: 2,327
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
Reply With Quote
  #7  
Old 09-03-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Using echo | grep is really kind of excessive when you have case

Code:
case $i in *' '*) echo file_out="$out_file";; esac
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
recognize parameters with spaces

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 01:36 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0