Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a question. I have a script, a kind of long script written in bash aprox. 370 lines. That has several functions and in those functions the user has to enter information which is then stored in files. ( This is suppose to represent a MySQL database, with the functions INSERT, UPDATE, SELECT, SELECT where x=y.) I created this myself in bash, now the only thing that rests me is that I need to be able to pass arguments on the command line to the script, that will do the same as the script does. I know that bash has positional parameters such as

$1
$2
$3
$*
$@
$0 ( refers to the name of the script)

etc. I know how I can use these parameters in a simple if function. This is not enough for my script. I basicly need to do the same thing that the script does, but then from the command line. I have been struggling with this for a couple of days now and I cannot think of a way to get it to work. Maybe someone here can help me with this?

If you want to have the script. That can be possible, but I don't think I can paste it in here...

EDIT: Link to script, http://pastebin.com/Hd5VsDv2

Note, I am a beginner in bash scripting.

EDIT: This is in reply to Answer 1. As I said I hope I can just replace the if [ "$1" = "one" ] ; then echo "found one" to if [ "$1" = "one" ] ; then echo SELECT where SELECT is the function I previously had in my script(above)

http://pastebin.com/VFMkBL6g LINK to testing script

share|improve this question
1  
I'm completely lost. What are you trying to accomplish? What do you mean "I need to do the same thing that the script does."? Can you give an example of what you want rather than give examples of what you don't want? –  TheCompWiz Jun 6 '11 at 16:42
    
Is your current script interactive? Does it ask the user something and the user types something? –  Nifle Jun 6 '11 at 16:46
    
If the script does not contain any sensitive information (for example usernames/passwords) you can post it on pastebin and put the url in your question: pastebin.com –  Nifle Jun 6 '11 at 16:50
    
Yes, my current script is interactive. It presents the user with a menu, then asks what the user wants to do, by pressing the number 1-4 the script will load a function. something like this. echo "Hello enter choice" echo "1. select" echo "2. insert" read choice case "$choice" in "1") SELECT ;; "2") INSERT ;; esac then in those functions the user gets asked to enter data, this data then gets put into a file with a command something like echo $read >> $file I pasted a link to my code in my question. The text is in dutch though, shouldnt matter I think. –  bryan Jun 6 '11 at 16:57

2 Answers 2

The script is interactive and reads it's data with several read statements.

After a quick look my judgement is that it would be easier to write a new script than adjust the current script to accept command lines. (With liberal use of cut/paste it shouldn't be that hard).

A few tips to get you started.

  1. Write a simple script that reads three or four variables from the command line, saves the variables in local variables A B C D and echo them out again.
  2. In your script create a function that if A=one echos "found one" if A=two echos "found two" if A=three echos "found two" (case works well for this)
  3. Create another few function that does the same thing as no2 above but for B, C & D
  4. Replace the echo statments in no2 with a call to your newly created functions
  5. If you get this to work you can now start replacing your place-holder functions and values with real stuff from your current script.

Lastly; don't take to many steps at once and when you get stuck ask a question over on http://stackoverflow.com/. Be sure to include the minimum amount of code that can be run and reproduces the error.

Good luck

share|improve this answer
    
It took me a week orso to get this far, I have all the functionalitys that they asked me, except for the command line thing. I had hoped I wouldn't have to rewrite this whole script :(... I only have a couple of days left... –  bryan Jun 6 '11 at 17:37
    
@bryan - An ugly way to do it would be to assign a lot of variables when the script starts. Then put a if-statment around each read variable_name. If the variable doesn't have a value you call the read variable_name line, if it does have a value you just proceed with the script. –  Nifle Jun 6 '11 at 17:51
    
I do not understand your last comment. Do you mean to just put a if statement around each read variable_name that checks if there is a input from the command line? –  bryan Jun 6 '11 at 17:54
    
@bryan - Yes that is what I mean. –  Nifle Jun 6 '11 at 17:55
    
@Nifle, hmm alright, and how might I do that?, sorry if I am bugging you but I am a real beginner with bash and I don't quite understand some things –  bryan Jun 6 '11 at 18:15

In bash there is built-in getopts to parse positional parameters. You can also use older external command getopt. Look at Using getopts to read the options/arguments passed to a script

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.