LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 05-02-2012, 04:53 AM   #1
gregmcc
Member
 
Registered: Mar 2007
Distribution: opensuse, ubuntu, debian
Posts: 43

Rep: Reputation: 1
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?
 
Old 05-02-2012, 05:14 AM   #2
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
Not easier but you could do for posparm in "$@" (in which case the shift would be pointless -- but you could do it anyway!).
 
Old 05-02-2012, 05:42 AM   #3
gregmcc
Member
 
Registered: Mar 2007
Distribution: opensuse, ubuntu, debian
Posts: 43

Original Poster
Rep: Reputation: 1
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
 
Old 05-02-2012, 05:48 AM   #4
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
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 "$*".
 
Old 05-02-2012, 06:01 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,018

Rep: Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199
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.
 
Old 05-02-2012, 06:05 AM   #6
gregmcc
Member
 
Registered: Mar 2007
Distribution: opensuse, ubuntu, debian
Posts: 43

Original Poster
Rep: Reputation: 1
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.
 
Old 05-02-2012, 06:53 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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
 
  


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 loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
[SOLVED] Bash: loop through parameters with shift Ramurd Programming 3 03-01-2012 05:49 AM
shift+End and Shift+Home key combo doesn't work right santana Linux - Hardware 6 04-23-2010 11:35 AM
How to loop a script? rollo Linux - General 3 12-21-2006 05:39 AM
Gateway shift Script neos Linux - Networking 1 03-10-2005 04:11 AM

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

All times are GMT -5. The time now is 05:32 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