LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   For loop script - shift (https://www.linuxquestions.org/questions/linux-newbie-8/for-loop-script-shift-942885/)

gregmcc 05-02-2012 05:53 AM

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?

catkin 05-02-2012 06:14 AM

Not easier but you could do for posparm in "$@" (in which case the shift would be pointless -- but you could do it anyway!).

gregmcc 05-02-2012 06:42 AM

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


catkin 05-02-2012 06:48 AM

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 "$*".

grail 05-02-2012 07:01 AM

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.

gregmcc 05-02-2012 07:05 AM

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


David the H. 05-02-2012 07:53 AM

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


All times are GMT -5. The time now is 12:59 PM.