View previous topic :: View next topic |
Author |
Message |
alex.blackbit Advocate
Joined: 26 Jul 2005 Posts: 2397
|
Posted: Tue Nov 27, 2012 9:37 am Post subject: bash: detect last loop iteration [SOLVED] |
|
|
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 |
|
|
jormartr Apprentice
Joined: 02 Jan 2008 Posts: 174
|
Posted: Tue Nov 27, 2012 10:31 am Post subject: Re: bash: detect last loop iteration |
|
|
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 |
|
|
ppurka Advocate
Joined: 26 Dec 2004 Posts: 3256
|
Posted: Tue Nov 27, 2012 11:11 am Post subject: |
|
|
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 | Now using kde5 | e is unstable :-/ |
|
Back to top |
|
|
Akkara Bodhisattva
Joined: 28 Mar 2006 Posts: 6702 Location: &akkara
|
Posted: Tue Nov 27, 2012 11:28 am Post subject: |
|
|
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
|
_________________ Many think that Dilbert is a comic. Unfortunately it is a documentary. |
|
Back to top |
|
|
ppurka Advocate
Joined: 26 Dec 2004 Posts: 3256
|
Posted: Tue Nov 27, 2012 2:53 pm Post subject: |
|
|
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 | Now using kde5 | e is unstable :-/ |
|
Back to top |
|
|
alex.blackbit Advocate
Joined: 26 Jul 2005 Posts: 2397
|
Posted: Thu Nov 29, 2012 2:53 pm Post subject: |
|
|
Thanks for all the answers.
Akkaras solution is the most elegant I think, I'll use that. |
|
Back to top |
|
|
Jimmy Jazz Guru
Joined: 04 Oct 2004 Posts: 327 Location: Strasbourg
|
Posted: Thu Nov 29, 2012 11:40 pm Post subject: Re: bash: detect last loop iteration [SOLVED] |
|
|
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 |
|
|
|