LinuxQuestions.org
Help answer threads with 0 replies.
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-26-2017, 11:35 AM   #1
atjurhs
Member
 
Registered: Aug 2012
Posts: 220

Rep: Reputation: Disabled
passing an argument/option into a script


hi guys,

i've written a bash script that has multiple lines

within the code, two of the lines that i have to change a text string by hand each time before running it,in this example i'll use
Quote:
foo
i would like to pass that text string into the bash script as an argument/option into the script when i execute the script.

i execute the script by

Code:
bash batch_run_script.bash
where
Quote:
batch_run_script.bash
is an executable. here's a few lines of the code. and it all runs perfectly.

Code:
do_stuff_above_this_line \
;
for f in input*.txt ; do mv "$f" "foo_$f" ; done \
;
cat foo*.txt > all_foo_output.txt \
;
do_more_stuff_below_this_line
what i'd like to be able to do is

Code:
bash batch_run_script.bash - foo
so the next time i can run

Code:
bash batch_run_script.bash - Boo
without having to open up the script and change foo to Boo by hand, saving it and then run the script using Boo

thanks for what ever help you can give

Todd

Last edited by atjurhs; 10-26-2017 at 11:36 AM.
 
Old 10-26-2017, 11:42 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,612
Blog Entries: 3

Rep: Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859
Those are called positional parameters:

http://wiki.bash-hackers.org/scripting/posparams

http://www.tldp.org/LDP/abs/html/abs-guide.html#EX17
 
Old 10-26-2017, 11:44 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,612
Blog Entries: 3

Rep: Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859
By the way, if you make the script executable with chmod and point to the interpreter in the first line, then you can execute it with just the name alone.

Code:
#!/bin/bash

do_stuff_above_this_line \
;
for f in input*.txt ; do mv "$f" "foo_$f" ; done \
;
cat foo*.txt > all_foo_output.txt \
;
do_more_stuff_below_this_line
and

Code:
./batch_run_script.bash

Last edited by Turbocapitalist; 10-26-2017 at 11:45 AM.
 
Old 10-26-2017, 01:01 PM   #4
atjurhs
Member
 
Registered: Aug 2012
Posts: 220

Original Poster
Rep: Reputation: Disabled
hi Turbo,

as to including the #!/bin/bash and making it an exe, but not calling the bash command, i've already set it up that way. just out of habit for a line command in a terminal i like to give both the command and an argument for the command, just out of habit.

i looked at the webpages you sited and now i know there proper name of "positional parameters" and i think i already do that in line 5 with the $f

what neither of those webpages show is how to pass into the script the positional parameter. would that look like

Quote:
bash batch_run_script.bash - $8 Boo
where 8 is the 8th occurrence of a string in line 5 (of course that didn't work and gives an error)

can you tell i'm a hack

Last edited by atjurhs; 10-26-2017 at 01:11 PM.
 
Old 10-26-2017, 01:04 PM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,612
Blog Entries: 3

Rep: Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859
They are used inside the script to reference options that were passed as run time arguments. See this example from one of the links above: http://wiki.bash-hackers.org/scripti...ams#using_them
 
Old 10-26-2017, 01:28 PM   #6
atjurhs
Member
 
Registered: Aug 2012
Posts: 220

Original Poster
Rep: Reputation: Disabled
Turbo, admittedly i'm a hack and only know 1/2 of what i'm doing.

is the webpage telling me something like this?

Quote:
bash batch_run_script.bash "positional_param_: Boo"
Quote:
do_stuff_above_this_line \
;
positional_param_ = $9 \
;
for f in input*.txt ; do mv "$f" "positional_param_$f" ; done \
;
positional_param_ = $2 \
;
cat positional_param*.txt > all_positional_param_output.txt \
;
do_more_stuff_below_this_line

Last edited by atjurhs; 10-26-2017 at 01:33 PM.
 
Old 10-26-2017, 01:37 PM   #7
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,612
Blog Entries: 3

Rep: Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859
It's more like this:

Code:
#!/bin/bash

echo "Your first option was $1";
echo "Your second option was $2";
shift;
shift;

while [ "$1" ]
do
    echo "Another options was $1";
    shift;
done

exit 0;
Then run it with various numbers of options.

Code:
./script.bash one
./script.bash one two
./script.bash one two three
./script.bash one two three four five six
./script.bash
 
Old 10-26-2017, 01:56 PM   #8
atjurhs
Member
 
Registered: Aug 2012
Posts: 220

Original Poster
Rep: Reputation: Disabled
i don't get it

thanks anyways

i'll keep doing it the way i'm doing it

Last edited by atjurhs; 10-26-2017 at 01:58 PM.
 
Old 10-27-2017, 12:05 AM   #9
nodir
Member
 
Registered: May 2016
Posts: 222

Rep: Reputation: Disabled
I think what you want is this:
Code:
#!/bin/bash

newname="$1"

for f in ./input*.txt ; do
  mv "$f" "$newname__$f"  
done
If you add "foo" as the positional parameter, "$newname" will be foo (same for Boo, or whatever you add).



-
You could also do
Code:
for f in ./input*.txt ; do
  mv "$f" "$1__$f"  
done
to me the solution at the top is more clear though.

-
Ups: You will want to add a test if positional parameter 1 is given and exit if it isn't;
at the top of the script. No need to run it at all, if it is used wrong.
Something like this (it's been a while, better check if the solution is valid. shellcheck.net for example)
Code:
if [[ "$#" -ne 1 ]]; then
  printf "no parameter given !\n"
  exit 1
fi

Last edited by nodir; 10-27-2017 at 12:25 AM.
 
Old 10-27-2017, 11:03 AM   #10
atjurhs
Member
 
Registered: Aug 2012
Posts: 220

Original Poster
Rep: Reputation: Disabled
nodir,

aaah, got it the $1 refers to where in the command line you give the positional parameter
 
Old 10-27-2017, 08:30 PM   #11
nodir
Member
 
Registered: May 2016
Posts: 222

Rep: Reputation: Disabled
exactly. so:
Code:
scriptname foo bar baz
foo is "$1", bar is "$2", baz is "$3", etc.
the script itself is $0
The number of arguments given is $# (in the example above, it would be 3).

I agree that it first is a bit confusing (a) the positional parameter given, b) the $1, $2, etc and c) assign those to a variable in the script itself var_name=$1).
Well: for me it sure was too. Still is now and then.

Fooling around with it in a testscript, using "echo", like Turbocapitalist did above, might make it more clear. Perhaps try that for a bit.

---
To me this seems very helpful to avoid common errors you find all over the internet, including how-to's:
http://mywiki.wooledge.org/BashPitfalls
(hard to get rid of stuff you learned in the beginning, imho, better start right away with good practice/style)

Last edited by nodir; 10-27-2017 at 08:33 PM.
 
  


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
Passing string argument to script barakolshe Linux - Newbie 1 07-03-2017 06:11 PM
passing multiple argument to expect script sumeet inani Linux - Newbie 11 05-06-2014 08:12 AM
Syntax for passing argument in shell script shridhar22 Programming 11 08-30-2012 04:42 PM
[SOLVED] (Something like...) passing an argument to a MATLAB script Mike_V Programming 1 12-21-2009 04:04 PM
shell script, Passing string as argument? antis Programming 4 09-07-2007 01:03 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:50 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
Open Source Consulting | Domain Registration