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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
05-02-2012, 04:53 AM
|
#1
|
Member
Registered: Mar 2007
Distribution: opensuse, ubuntu, debian
Posts: 43
Rep:
|
For loop script - shift
Got a exercise from a course manual I'm trying to do.
The first part is to use a until loop to iterate through the positional parameters on the command line using the shift command. I've for this working fine.
Code:
#!/bin/bash
echo "Start Backup"
NAME="geeko"
NOW="$(date '+%Y%m%d-%H%M')"
echo "$NOW"
until test "$1" = ""
do
echo -e "\nBackup ""$1"" to /backup"
rsync -av --no-whole-file "$1" /backup
shift
done
The next part of the exercise is to do the above but use a for loop instead of the until loop.
I'm thinking of doing a endless for loop and then when a condition is true break out of the loop? Is there a easier way to do this?
|
|
|
05-02-2012, 05:14 AM
|
#2
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Not easier but you could do for posparm in "$@" (in which case the shift would be pointless -- but you could do it anyway!).
|
|
|
05-02-2012, 05:42 AM
|
#3
|
Member
Registered: Mar 2007
Distribution: opensuse, ubuntu, debian
Posts: 43
Original Poster
Rep:
|
The manual says nothing about posparm so I don't think we are supposed to know about that - is there an easier way?
I was thinking of something like this - it doesnt work yet cause I'm probably way off but you get the idea. Its just supposed to echo the command line parameters.
Code:
#!/bin/bash
echo "Start Backup"
NAME="geeko"
NOW="$(date '+%Y%m%d-%H%M')"
echo "$NOW"
for i in *
do
echo $0
shift
done
|
|
|
05-02-2012, 05:48 AM
|
#4
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
posparm is just a variable name (short for "positional parameter"), equivalent to the i in your for loop.
In your for loop, * will expand to the names of all the files in the current directory except for the ones beginning with .
You probably meant $* but it needs to be quoted in case any of the individual posparms include word-splitting characters such as space. "$@" is more robust than "$*".
|
|
|
05-02-2012, 06:01 AM
|
#5
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,018
|
Personally I am a bit wary of the example you are using as it seems to want to backup what is passed to the script and yet what is being passed is never verified (danger will robinson).
If I ignore my concerns, you can default your for loop like so:
Code:
for i
do
echo "$i"
done
I thought perhaps better to echo the parameters that the name of the script for each parameter passed in.
|
|
|
05-02-2012, 06:05 AM
|
#6
|
Member
Registered: Mar 2007
Distribution: opensuse, ubuntu, debian
Posts: 43
Original Poster
Rep:
|
Thanks a lot - that did the trick. Got this working.
Code:
for i in "$@"
do
echo $1
shift
done
Grail - awesome - this also works. Its even easier. Its basic scripting so they are just going through for/while loops etc - no error checking yet
Code:
for i
do
echo $i
done
Last edited by gregmcc; 05-02-2012 at 06:07 AM.
|
|
|
05-02-2012, 06:53 AM
|
#7
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
for iterates through the list of words* supplied to it. If no list is given, it defaults to the " $@" positional parameters.
*What's considered a word depends on things like quoting and the IFS setting, and is read after all variable, command, and globbing expansion is complete. In particular be aware of the don't read lines with for gotcha.
Edit: also read these links for more on how the shell defines words/arguments. It's very important to understand the concept correctly:
http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes
Last edited by David the H.; 05-02-2012 at 06:56 AM.
Reason: as stated
|
|
|
All times are GMT -5. The time now is 05:32 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|