LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-09-2009, 04:32 PM   #1
Mike_V
Member
 
Registered: Apr 2009
Location: Boston MA
Distribution: CentOS 6.2 x86_64 GNU/Linux
Posts: 59

Rep: Reputation: 19
define a variable outside of a *.sh script...


Hi there,

I have a question about a sh script and hope one of you can help.


Say I have a file called "script.sh" it looks like this:


Code:
#!/bin/sh

for file in first_file; do


cp $file SAVED/$file

done

Now I want to change this script to something like:


Code:
#!/bin/sh

for file in BLANK; do


cp $file SAVED/$file

done

and then run

script.sh -file first_file

And let the script fill "first_file" in the BLANK space.

(OR maybe it can be done much easier: how can I let the script know that I want it to fill in the word "first_file" everywhere in the script were it says "file")


Two things:
a) I have no idea if this is at all possible in sh
b) of course my real script is much more complex, but getting an option like I describe above would make my work a lot easier.
c) my real script should really stay in sh (so I'm bound to sh)

Is this question clear? (I could imagine not...)
Please share any thoughts and ideas,

Thanks,

Mike
 
Old 09-09-2009, 04:42 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You can just pass the name of the file as argument, then use positional parameters:
Code:
#!/bin/sh
for file in $1
do
  cp $file SAVED/$file
done
If you have a list of files passed as a list of arguments separated by spaces:
Code:
#!/bin/sh
for file in $*
or
Code:
#!/bin/sh
for file in $@
See the Advanced Bash Scripting Guide for a complete explanation about positional parameters and the difference between $* and $@. If you really want to use options like -f or --file, you have to go through getopt or getopts.
 
Old 09-09-2009, 09:59 PM   #3
Mike_V
Member
 
Registered: Apr 2009
Location: Boston MA
Distribution: CentOS 6.2 x86_64 GNU/Linux
Posts: 59

Original Poster
Rep: Reputation: 19
colucix:
thanks for the suggestions. I almost understand what you're saying.

It's just that I'm afraid I want to ask you if you can spell out what you mean with "You can just pass the name of the file as argument".

What is that? (I'm a relative newbie, I did look at the Advanced Bash Scripting Guide, but it didn't help me right away).

So what I did now is I made a script called script.sh which looks like:

Code:
#!/bin/sh
for file in $1
do
  cp $file SAVED/$file
done
What do I type in the command line so that it will know that for each $1 it will read the file "first_file.txt"

Thanks again!

Last edited by Mike_V; 09-09-2009 at 10:30 PM.
 
Old 09-09-2009, 10:05 PM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555
What colucix means by "pass the name of the file as an argument", is like so (assuming your script is named script and the file is called filename:

Code:
shell$ ./script filename
So, you've passed filename to the script, as the first argument. Basically, anything following the name of the script, is an argument or an option. Each one will be allotted to a successive shell variable (the positional parameters), so:

Code:
shell$ ./script hello goodbye rotten
passes 3 arguments to the script, which will be available within the script as $1, $2, and $3, while $0 is reserved for the script itself.

Sasha
 
Old 09-09-2009, 10:22 PM   #5
Mike_V
Member
 
Registered: Apr 2009
Location: Boston MA
Distribution: CentOS 6.2 x86_64 GNU/Linux
Posts: 59

Original Poster
Rep: Reputation: 19
Cool... honestly, it's like magic to me!
It works (of course).

One more question, if you don't mind:

Is there a way to "read out" what arguments I've passed?
 
Old 09-09-2009, 10:26 PM   #6
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555
You mean, display the arguments on screen?

Well, colucix' second example is the way I usually do:

Code:
for stuff in $*; do
echo $stuff
done
That will loop through the space-separated arguments you gave on the command-line. Is that what you want?

It is pretty cool this bash/cli stuff. The term I like that describes the myriad linux command-line & bash tools is, "Elegantly Simple" though there are certainly no shortage of "Elegantly Ridiculously Complicated" tools too

Sasha

PS- Nope, I don't mind. That's why we're all hanging around here-- to help each other out
 
Old 09-09-2009, 10:50 PM   #7
Mike_V
Member
 
Registered: Apr 2009
Location: Boston MA
Distribution: CentOS 6.2 x86_64 GNU/Linux
Posts: 59

Original Poster
Rep: Reputation: 19
thanks again Sasha.

I don't have it super clear, here.

This is what I do:

Code:
TEMP$ ls
first_file.txt  SAVED  script.sh
TEMP$ cat script.sh
#!/bin/sh
for file in $1
do
  cp $file SAVED/$file
done
TEMP$ sh ./script.sh first_file.txt
TEMP$ sh script.sh
TEMP$ ls -l SAVED/
-rw-r--r-- 1 kdijk cnl 5 Sep  9 23:41 first_file.txt
TEMP$
Now, it is keeping in mind that I have passed "first_file.txt" to "script.sh". I was wondering what I should type if I want to retrieve what I've passed through to the script?

(It seems that that last piece of text did not do that... should it?)
 
Old 09-09-2009, 10:56 PM   #8
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555
OK, your script appears to be working, but I don't understand your question

If you're asking, "How can I retrieve the argument I gave the script, after the script is done executing?" then the answer is, "I don't believe you can."

Once the script is run and done, its variables and things like that are gone into outer-space. With each execution, you can pass new stuff to it, and that stuff will only effect the environment of the running script itself. Once it's finished, that's it.

Did this answer the question? If not, please rephrase

Sasha

PS - I shouldn't say there's NO WAY to get that info, but I'm fairly certain there's not; if there IS a way, I don't know it.. I'm far from expert with bash either; just learning as I go

Last edited by GrapefruiTgirl; 09-09-2009 at 11:00 PM.
 
Old 09-09-2009, 11:01 PM   #9
Mike_V
Member
 
Registered: Apr 2009
Location: Boston MA
Distribution: CentOS 6.2 x86_64 GNU/Linux
Posts: 59

Original Poster
Rep: Reputation: 19
Clear as a bell!! Thanks! Have a good night.

(I could have know this if I would have tried to run the script with those arguments twice... I thought I could...)

Last edited by Mike_V; 09-09-2009 at 11:03 PM.
 
Old 09-09-2009, 11:01 PM   #10
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555
You're most welcome -- you too!

Sasha
 
Old 09-10-2009, 06:51 AM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by GrapefruiTgirl View Post
You mean, display the arguments on screen?

Well, colucix' second example is the way I usually do:

Code:
for stuff in $*; do
echo $stuff
done
That will work just fine until you have any spaces in the file names, for example if
Code:
c@CW8:/tmp/tmp$ /bin/ls -l
total 4
-rw-r--r-- 1 c c   0 2009-09-10 17:15 a file name with spaces in it
-rwxr--r-- 1 c c 112 2009-09-10 17:19 my.sh
c@CW8:/tmp/tmp$ cat my.sh
#!/bin/bash
for stuff in $*; do
    echo 'using $*' $stuff
done

for stuff in "$@"; do
    echo 'using $@' $stuff
done

c@CW8:/tmp/tmp$ ./my.sh *
using $* a
using $* file
using $* name
using $* with
using $* spaces
using $* in
using $* it
using $* my.sh
using $@ a file name with spaces in it
using $@ my.sh
 
Old 09-10-2009, 07:24 AM   #12
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555
Quote:
Originally Posted by catkin View Post
That will work just fine until you have any spaces in the file names, for example if
Code:
c@CW8:/tmp/tmp$ /bin/ls -l
total 4
-rw-r--r-- 1 c c   0 2009-09-10 17:15 a file name with spaces in it
-rwxr--r-- 1 c c 112 2009-09-10 17:19 my.sh
c@CW8:/tmp/tmp$ cat my.sh
#!/bin/bash
for stuff in $*; do
    echo 'using $*' $stuff
done

for stuff in "$@"; do
    echo 'using $@' $stuff
done

c@CW8:/tmp/tmp$ ./my.sh *
using $* a
using $* file
using $* name
using $* with
using $* spaces
using $* in
using $* it
using $* my.sh
using $@ a file name with spaces in it
using $@ my.sh
Right! In which case (off the top of my head) there are two solutions (or a combination of two things):

1-- use quotes around the "stuff"
2-- use some delimiter between the filenames, and parse the input.

@ catkin -- what would you do in this case?:

Code:
filename1="this one has spaces"
filename2="thisonedoesnot"

shell$ ./script this one has spaces thisonedoesnot
without actually running this example, I think $@ will not work. However, the following does work:
Code:
filename1="this one has spaces"
filename2="thisonedoesnot"

shell$ ./script "this one has spaces" "thisonedoesnot"
So, it's annoying having to quote all the arguments -- what to do?

Sasha
 
  


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
How to define a variable In a while loop? fatsheep Programming 16 07-30-2014 01:12 PM
Define global variable maxmil Linux - Newbie 4 03-07-2006 10:36 AM
How can I define a global variable? kloss Linux - General 4 02-21-2006 04:30 AM
how am i define an environment variable? yenonn Slackware 2 12-19-2003 07:44 AM
how to define a global variable Anniebaby Programming 1 11-09-2003 10:43 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:01 AM.

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