Remove Command-Line Option from String


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove Command-Line Option from String
# 1  
Old 07-07-2010
MySQL Remove Command-Line Option from String

I want to add a "-r <remote_host>" option to my ksh script, causing the script to run a script of the same name on the specified remote host. The remote invocation should itself include all the command-line options of the original invocation, less the -r option.

For example, this invocation:

# the_script -a -b -r the_remote_host -x x_option_value -y arg1 arg2

would execute this:

ssh the_remote_host the_script -a -b -x x_option_value -y arg1 arg2

My problem is removing the "-r <remote_host>" text from $* when bulding the remote invocation. I've tried sed, but I'm not getting it.
# 2  
Old 07-07-2010
Tools Don't laugh... strange, but appears to work for you.

Code:
C:\cygwin\tmp>echo the_script -a -b -r the_remote_host -x x_option_value -y arg1 arg2 | sed 's/-/~-/g' | tr "~" "\n" | egrep -v "\-r" | tr -d "\n"
the_script -a -b -x x_option_value -y arg1 arg2
C:\cygwin\tmp>

Note, interesting that:
Code:
C:\cygwin\tmp>echo the_script -a -b -r the_remote_host -x x_option_value -y arg1 arg2 | sed 's/-/~-/g' | tr "~" "\n"
the_script
-a
-b
-r the_remote_host
-x x_option_value
-y arg1 arg2

breaks your script out so each option is on its own line. perhaps useful?

Last edited by joeyg; 07-07-2010 at 05:50 PM.. Reason: added interesting fact
# 3  
Old 07-07-2010
That doesn't work for me:

Code:
$ echo the_script -a -b -r the_remote_host -x x_option_value -y arg1 arg2 | sed 's/-/~-/g' | tr "~" "\n" | egrep -v "\-" | tr -d "\n"
the_script $

I can't see how you got the output you did from that command. Where's the logic to check for "-r ..." and remove that part of the string?
# 4  
Old 07-07-2010
Code:
cat the_script
ssh ${@:4:1} ${@:1:2} ${@:5}

and you call it exactly like this
Code:
./the_script -a -b -r the_remote_host -x x_option_value -y arg1 arg2

# 5  
Old 07-07-2010
my previous cut/paste missed something

Matt,
try my previous post now... was missing a character somehow
should have been:


Code:
C:\cygwin\tmp>echo the_script -a -b -r the_remote_host -x x_option_value -y arg1 arg2 | sed 's/-/~-/g' | tr "~" "\n" | egrep -v "\-r" | tr -d "\n"
the_script -a -b -x x_option_value -y arg1 arg2
C:\cygwin\tmp>

And this won't care the order of your switches.
# 6  
Old 07-07-2010
Quote:
${@:4:1} ${@:1:2} ${@:5}
That looks like it's designed exclusively for the example I posted, but I'm looking for a more generic solution. The "-r <remote_host>" text might be anywhere in $@.

---------- Post updated at 09:03 PM ---------- Previous update was at 08:55 PM ----------

Quote:
sed 's/-/~-/g' | tr "~" "\n" | egrep -v "\-r" | tr -d "\n"
That works for the example I posted, but it seems to assume that the text I need to remove is bounded by minus signs. For example, if "-r ..." is the last option, and is followed by arguments, that code doesn't pass the arguments through:

Code:
# echo the_script -a -b -r the_remote_host arg1 arg2 | sed 's/-/~-/g' | tr "~" "\n" | egrep -v "\-r" | tr -d "\n"                     
the_script -a -b

The approach of splitting into multiple lines and using "grep -v" is interesting, though.
# 7  
Old 07-07-2010
Code:
options=$(sed 's/\(.*\) *-r [^ ]*\(.*\)/\1\2/' <<<"$@")
remHost=$(sed 's/.* *-r \([^ ]*\).*/\1/' <<<"$@")
ssh $remHost $options

This User Gave Thanks to daPeach For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl command line option '-n','-p' and multiple files: can it know a file name of a printed line?

I am looking for help in processing of those options: '-n' or '-p' I understand what they do and how to use them. But, I would like to use them with more than one file (and without any shell-loop; loading the 'perl' once.) I did try it and -n works on 2 files. Question is: - is it possible to... (6 Replies)
Discussion started by: alex_5161
6 Replies

2. UNIX for Dummies Questions & Answers

Command line / script option to filter a data set by values of one column

Hi all! I have a data set in this tab separated format : Label, Value1, Value2 An instance is "data.txt" : 0 1 1 -1 2 3 0 2 2 I would like to parse this data set and generate two files, one that has only data with the label 0 and the other with label -1, so my outputs should be, for... (1 Reply)
Discussion started by: gnat01
1 Replies

3. Programming

Reading command line arguments and setting up values if option not provided

I have a C++ program. I read command line arguments, but if the value is not supplied, I default or make a calculation. Let's say I set it to a default value. I can code this in several ways. Here I show three ways. What would be the best way for maintaining this code? The program will get very... (2 Replies)
Discussion started by: kristinu
2 Replies

4. Shell Programming and Scripting

Remove last string from last line in a file

Hi, I have a file like : I want to remove last string in last line (here total string is "05550"~). And last line end with ~ character. Output should be : Please help me Thanks in advance (3 Replies)
Discussion started by: mnmonu
3 Replies

5. Shell Programming and Scripting

Remove last string from each line

I am trying to write a script that will allow me to recursively look at my directories, and output all filenames to a txt document. I am almost finished, however I am hitting one last snag. Here is my script so far: find . | grep .jpg | awk -F"/" '{print $NF}' > output.txtThis will give me an... (7 Replies)
Discussion started by: Davinator
7 Replies

6. Homework & Coursework Questions

trouble understanding file option and command line arguments

Hi, I am creating a program with the C language that simulates the WC command in Unix. My program needs to count lines, bytes and words. I have not added the code to count bytes and words yet. I am having trouble understanding what the file option/flag '-' does. I can not visualize how it moves... (1 Reply)
Discussion started by: heywoodfloyd
1 Replies

7. Shell Programming and Scripting

Remove line based on string and put new line with parameter

Hi Folks, I am new to ksh, i have informatica parameter file that i need to update everyday with shell script. i need your help updating this file with new parameters. sample data $$TABLE1_DATE=04-27-2011 $$TABLE2_DATE=04-23-2011 $$TABLE3_DATE=03-19-2011 .......Highligned... (4 Replies)
Discussion started by: victor369
4 Replies

8. UNIX for Dummies Questions & Answers

What is a menu or command line option driven script?

i'm confused what this means. i was asked to design a menu or command line option driven script that reads out of a DB and displays info such as read_data.pl -u <user> -e <event> which would print commands run by <user>with the <event> in the db. any suggestions? i've been using... (2 Replies)
Discussion started by: kpddong
2 Replies

9. Programming

command line option: optarg

Hi I'm learning how to add in programm another option by command line. For example in the "my_prog" i want to add a " -k " option and then write a number. I mean: my_prog -k 50 and the i should use the number 50. I'm reading about getopt_long and optarg, and what i have done in the code... (0 Replies)
Discussion started by: Dedalus
0 Replies
Login or Register to Ask a Question