Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
bash: detect last loop iteration [SOLVED]
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
alex.blackbit
Advocate
Advocate


Joined: 26 Jul 2005
Posts: 2395

PostPosted: Tue Nov 27, 2012 9:37 am    Post subject: bash: detect last loop iteration [SOLVED] Reply with quote

Hi,

let's say I want to do something stupid like this:
Code:
#!/bin/bash

IFS='/'
FILE="/usr/bin/firefox"

for level in $FILE;
do
   echo -n "${level}"
   echo -n '/'
done

echo

The output would be:
Code:
$ bash /tmp/script.sh
/usr/bin/firefox/
$

I want to get rid of the last slash.
My idea is to add an if statement to the loop to decide whether to output the slash or not, but I don't know what the condition would be.
Of course this is a constructed example.
Thanks in advance.


Last edited by alex.blackbit on Thu Nov 29, 2012 2:54 pm; edited 1 time in total
Back to top
View user's profile Send private message
jormartr
Apprentice
Apprentice


Joined: 02 Jan 2008
Posts: 174

PostPosted: Tue Nov 27, 2012 10:31 am    Post subject: Re: bash: detect last loop iteration Reply with quote

Something like this?

Code:

IFS='/'
FILE="/usr/bin/firefox"

for level in $FILE;
do
    echo -n "${level}"
    # if ... then SLASH=1 || SLASH=0
    echo -n '/'
done

[ $SLASH -eq 1 ] && FILE="$(echo "FILE" | sed 's:/$::')"
Back to top
View user's profile Send private message
ppurka
Advocate
Advocate


Joined: 26 Dec 2004
Posts: 3205

PostPosted: Tue Nov 27, 2012 11:11 am    Post subject: Reply with quote

Switch the order of the two echo's (to chop off the first /)
Code:
#!/bin/bash

IFS='/'
FILE="/usr/bin/firefox"
cnt=0

for level in $FILE;
do
   cnt=$(( $cnt + 1 ))
   if [ $cnt -eq 1 ]; then
        continue
   fi
   echo -n '/'
   echo -n "${level}"
done

echo

Alternatively, use bash arrays:
Code:
#!/bin/bash

IFS='/'
FILE="/usr/bin/firefox"

FA=( $FILE )
max_1=$((${#FA[@]}-1))
for ((i=0; i<$max_1; i++)); do
    echo -n "${FA[$i]}"
    echo -n '/'
done
echo ${FA[$max_1]}

_________________
emerge --quiet redefined | E17 vids: I, II
Back to top
View user's profile Send private message
Akkara
Administrator
Administrator


Joined: 28 Mar 2006
Posts: 5171
Location: &akkara

PostPosted: Tue Nov 27, 2012 11:28 am    Post subject: Reply with quote

My favorite way of handling these "insert in between" kinds of problems, is to insert as a prefix, but arrange for the first prefix to be empty. Like this:
Code:
#!/bin/bash

IFS='/'
FILE="/usr/bin/firefox"

slash=""
for level in $FILE;
do
   echo -n "${slash}${level}"
   slash="/"
done

echo

_________________
echo 'long long long x;' | gcc -x c -c -
Back to top
View user's profile Send private message
ppurka
Advocate
Advocate


Joined: 26 Dec 2004
Posts: 3205

PostPosted: Tue Nov 27, 2012 2:53 pm    Post subject: Reply with quote

Akkara wrote:
My favorite way of handling these "insert in between" kinds of problems, is to insert as a prefix, but arrange for the first prefix to be empty. Like this:
Code:
#!/bin/bash

IFS='/'
FILE="/usr/bin/firefox"

slash=""
for level in $FILE;
do
   echo -n "${slash}${level}"
   slash="/"
done

echo
Nice! I like this solution!
_________________
emerge --quiet redefined | E17 vids: I, II
Back to top
View user's profile Send private message
alex.blackbit
Advocate
Advocate


Joined: 26 Jul 2005
Posts: 2395

PostPosted: Thu Nov 29, 2012 2:53 pm    Post subject: Reply with quote

Thanks for all the answers.
Akkaras solution is the most elegant I think, I'll use that.
Back to top
View user's profile Send private message
Jimmy Jazz
Apprentice
Apprentice


Joined: 04 Oct 2004
Posts: 274
Location: Strasbourg

PostPosted: Thu Nov 29, 2012 11:40 pm    Post subject: Re: bash: detect last loop iteration [SOLVED] Reply with quote

alex.blackbit wrote:
Hi,

let's say I want to do something stupid like this:
Code:
#!/bin/bash

IFS='/'
FILE="/usr/bin/firefox"

for level in $FILE;
do
   echo -n "${level}"
   echo -n '/'
done

echo

The output would be:
Code:
$ bash /tmp/script.sh
/usr/bin/firefox/
$

I want to get rid of the last slash.
My idea is to add an if statement to the loop to decide whether to output the slash or not, but I don't know what the condition would be.
Of course this is a constructed example.
Thanks in advance.


For the last slash, you could write something like that,

Code:

#!/bin/bash

for pathname in /usr/bin/firefox/ /usr/bin/firefox; do

    last=${pathname: -1}

    case "$last" in
    (/) echo is a slash
        echo ${pathname::-1}
        ;;
    (*) echo is something else
        echo ${pathname}
        ;;
    esac
done

$ ./fun
is a slash
/usr/bin/firefox
is something else
/usr/bin/firefox



Also not supported by busybox ash
_________________
« La seule condition au triomphe du mal, c'est l'inaction des gens de bien » E.Burke
Code:

+----+----+----+
|    |::::|    |
|    |::::|    |
+----+----+----+

motto: WeLCRO
WritE Less Code, Repeat Often
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum