Remove arguments


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove arguments
# 1  
Old 03-05-2009
Remove arguments

Hi all,
First of all, this is my first post in this forum, so it's nice to know you. I hope not to brake any 'rule' Smilie

I'd like to know, if it's possible to remove a parameter from command line, regardless of its position? I've found a solution using shift, if it's the first argument. But, I don't know in which position the argument comes (if it comes). Any solution?

Thanks a lot.

Albert.
# 2  
Old 03-05-2009
Assuming I understand -
let's say your arguments are 1 2 3 4
and you want to "lose" 2 (which in this case happens to be $2).
So the args become 1 3 4
Code:
# do not include the value you want to skip -- $2
set - $1 $3 $4
# show the new arguments
echo $*

# 3  
Old 03-05-2009
Quote:
Originally Posted by jim mcnamara
Assuming I understand -
let's say your arguments are 1 2 3 4
and you want to "lose" 2 (which in this case happens to be $2).
So the args become 1 3 4
Code:
# do not include the value you want to skip -- $2
set - $1 $3 $4
# show the new arguments
echo $*

Thanks jim, that could be a good solution. I tried something else that it doesn't work, using strings.
I think it could be easier to understand if I show you my code:
Code:
ARGS=$@

confirmed=false
for order in $ARGS; do
    if [ $order == "-confirmed" ]; then
        confirmed=true
    fi
    [....]
done

When I know "-confirmed" is an argument I have to remove it from ARGS.
I tried cut in this way:
Code:
ARGS=`echo $ARGS | cut -d $order -f-´

but the delimiter must be a character. I think using sed or awk should work, but I don't know how. I'm quite new in bash programming.

Thanks a lot.

Albert
# 4  
Old 03-05-2009
I got it:
Code:
ARGS=$@

confirmed=false
for order in $ARGS; do
    if [ $order == "-confirmed" ]; then
        confirmed=true
         ARGS=`echo "$ARGS" | awk 'BEGIN { FS="-confirmed" } ; { print $1$2 }'`
         continue
    fi
    [....]
done


But I have one more question. In awk command why it doesn't work if I replace
Code:
{ FS = "-confirmed" }

for
Code:
{ FS = "$order" }

It is supposed $order contains "-confirmed", isn't it?

Albert.

# 5  
Old 03-05-2009
Quote:
Originally Posted by AlbertGM
...
But I have one more question. In awk command why it doesn't work if I replace
Code:
{ FS = "-confirmed" }

for
Code:
{ FS = "$order" }

It is supposed $order contains "-confirmed", isn't it?

Albert.


Because awk has a special way of passing shell variables into it:

Code:
awk -v sep="$order"  'BEGIN { FS=sep }{ print $1 $2 }'

# 6  
Old 03-05-2009
ok you have to delete -confirmed so:
Code:
set - $( echo "$*" | sed 's/-confirmed//' )

Run this - you do not have to test for -confirmed if you do not need to
# 7  
Old 03-06-2009
Quote:
Originally Posted by rubin
Because awk has a special way of passing shell variables into it:

Code:
awk -v sep="$order"  'BEGIN { FS=sep }{ print $1 $2 }'

Quote:
Originally Posted by jim mcnamara
ok you have to delete -confirmed so:
Code:
set - $( echo "$*" | sed 's/-confirmed//' )

Run this - you do not have to test for -confirmed if you do not need to
Thanks a lot.

Albert.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash to remove find and remove specific extension

The bash below executes and does find all the .bam files in each R_2019 folder. However set -x shows that the .bam extension only gets removed from one .bam file in each folder (appears to be the last in each). Why is it not removing the extension from each (this is $SAMPLE)? Thank you :). set... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

remove brackets and put it in a column and remove repeated entry

Hi all, I want to remove the remove bracket sign ( ) and put in the separate column I also want to remove the repeated entry like in first row in below input (PA156) is repeated ESR1 (PA156) leflunomide (PA450192) (PA156) leflunomide (PA450192) CHST3 (PA26503) docetaxel... (2 Replies)
Discussion started by: manigrover
2 Replies

3. Shell Programming and Scripting

Too many arguments

hi I have this code a="a b c" set -- $a if ; then echo empty fi why is it line 3 reports "test: [: too many arguments"? :wall: Thanks! (4 Replies)
Discussion started by: h0ujun
4 Replies

4. UNIX for Dummies Questions & Answers

Getting some arguments but not others

I have a tcsh script that I want to call using the following expressions, with the --sw argument being optional: /home/chrisd/tatsh/trunk/hstmy/bin/tcsh/printTable.tcsh $msg /home/chrisd/tatsh/trunk/hstmy/bin/tcsh/printTable.tcsh --sw=15 $msg I want to store $msg in a variable called strLst... (0 Replies)
Discussion started by: kristinu
0 Replies

5. UNIX for Dummies Questions & Answers

How to take arguments?

Hey everybody. How do you write a program that will produce output based on its arguments? For example, how would you write one that will add 1 to an integer argument so it would look like this: $add 1 78 79 $ I only know how to write programs to take user input with the read function,... (2 Replies)
Discussion started by: unclepickle1
2 Replies

6. Shell Programming and Scripting

grep with two arguments to arguments to surch for

Hello, is it possible to give grep two documents to surche for? like grep "test" /home/one.txt AND /home/two.txt ? thanks (1 Reply)
Discussion started by: Cybertron
1 Replies

7. Shell Programming and Scripting

Too many arguments?

I can't find anything wrong with this line of code, it works when there is one file in the directory but more than one i get a "too many arguements2 error if ; then am i missing something? (3 Replies)
Discussion started by: Alendrin
3 Replies

8. Shell Programming and Scripting

Remove the first two words from shell arguments

Hi I need to delete first two words from the arguments my script receives .. no matter how many arguments are passed . Thanks Praveen (5 Replies)
Discussion started by: praveenbvarrier
5 Replies

9. UNIX for Dummies Questions & Answers

smitty, remove user, remove directory as well..

hi, i am on aix. i used smitty to remove a user.. but then found that its directory still exists.... so i have to remove the directory manually... am i doing it the right way? (2 Replies)
Discussion started by: yls177
2 Replies

10. Shell Programming and Scripting

several arguments together

Hello guys, you may help me on this : I want to search thru many files if a sentence or a string exists and printout the lines where this is present by calling a script like : loc "arg1 arg2 arg3 arg4" where arg1 arg2 arg3 arg4 would be in fact the sentence separated with blanks... In... (8 Replies)
Discussion started by: homefp
8 Replies
Login or Register to Ask a Question