LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-18-2012, 08:54 PM   #16
Trd300
Member
 
Registered: Feb 2012
Posts: 89

Original Poster
Rep: Reputation: Disabled

I used (without functions first just to understand)
Code:
#!/bin/bash

input=""

while getopts :lnh opt
do
    case $opt in
        l)
            command1 input      # this first step returns output called "output1". It is used as the input for command2.
            continue
            ;;
        n)
            output1 = input
            continue
            ;;
         h)
            echo <usage, bla bla...>
            exit 1
            ;;
         *)
            echo "error: -$opt is not a valid option"
            exit 1
            ;;
    esac

command2 output1 > output2
command3 output2 > final_output

done
Problems:
- cannot find input when using option -l or -n. How do you link the input variable to the query file?
- when using a wrong option like -y for e.g. it returns:
Code:
error: -? is not a valid option
How can I mention "y" instead of "?" ?

Last edited by Trd300; 10-19-2012 at 12:43 AM.
 
Old 10-19-2012, 01:00 AM   #17
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,350

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
see the link I gave you (session: an option with argument):
Code:
#echo "error: -$opt is not a valid option"
echo "error: -$OPTARG is not a valid option"
 
1 members found this post helpful.
Old 10-19-2012, 01:29 AM   #18
Trd300
Member
 
Registered: Feb 2012
Posts: 89

Original Poster
Rep: Reputation: Disabled
I didn't understand the verbose/silent mode, but it was because I made a typo when I tried $OPTARG earlier. Thanks for that !

I fixed the problem with -l, by removing the "continue", but not the problem with -n (still doesn't find the file)...
Code:
#!/bin/bash

input=""

while getopts :lnh opt
do
    case $opt in
        l)
            command1 $input      # this first step returns output called "output1". It is used as the input for command2.
            ;;
        n)
            output1 = $input
            ;;
         h)
            echo <usage, bla bla...>
            exit 1
            ;;
         *)
            echo "error: -$opt is not a valid option"
            exit 1
            ;;
    esac

command2 output1 > output2
command3 output2 > final_output

done

Last edited by Trd300; 10-19-2012 at 02:25 AM.
 
Old 10-19-2012, 02:29 AM   #19
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,506

Rep: Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890
You do realise that the following line will never work:
Code:
output1 = $input
As assigning to any variable in bash requires no white space around the equals sign.

Also, for all calls to the while loop, input is always blank, so I am not sure what is supposed to happen when calling the variable $input?
 
Old 10-19-2012, 03:43 AM   #20
Trd300
Member
 
Registered: Feb 2012
Posts: 89

Original Poster
Rep: Reputation: Disabled
Whoops... You are right, I forgot about the space !

How could you define "the last argument of the command line" to select the file?

Last edited by Trd300; 10-19-2012 at 06:39 AM.
 
Old 10-19-2012, 10:45 AM   #21
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,506

Rep: Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890
All arguments are still stored in $@ or $*, including switches. Once you shift past your switches the file is what should be left
 
Old 10-19-2012, 09:01 PM   #22
Trd300
Member
 
Registered: Feb 2012
Posts: 89

Original Poster
Rep: Reputation: Disabled
All good thanks !
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
gawk works from command line but not from cron fantasygoat Linux - Server 3 10-25-2011 01:18 PM
howto create GUI-graphical wrapper for command line program SaintDanBert Linux - Desktop 4 01-30-2011 08:24 PM
Program on Command Line Argument and Pointers thelink123 Programming 2 09-01-2009 09:05 AM
[SOLVED] specifying fields for printing in gawk from command line David the H. Programming 8 08-04-2009 03:32 PM
Redirecting output to a command-line argument of another command madiyaan Linux - Newbie 1 02-19-2005 04:35 PM


All times are GMT -5. The time now is 08:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration