Afficher un message
Vieux 01/11/2006, 21h26   #20
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: double "triangle" loop for $* ?

On 2006-11-01, Loki Harfagr wrote:
....
> Then, while at replacing 'yes' and 'act -n' to simulate 'seq'
> why not directly simulate 'seq' ?
> here follows a possible form, I guess Chris F.A. Johnson and/or
> youreslf will correct the possible holes in it :-)
>
> $ cat seq_in_shell.sh
> #!/bin/bash
> ###
> ###
> ### In the "horrible tools without tools" collection.
> ###
> ###
> ### Admitting you miss the 'seq' utility and you don't
> ### have access to the source and/or a C compiler.
> ### Well, also admitting you have some spare time
> ### and you are kin to the March Hare here's a
> ### horrible still effective bash way to fill your dreams.
> ###
> ###
> ###
> _min=1
> _max=$1
> (( $# > 1 )) && _max=$2 && _min=$1


Why use non-standard syntax?

> _count=$_min
> (( _max=${_max} + 1 ))
> (( _max - $_min )) || exit
> while true
> do
> printf ${_count}"\n"
> (( _count=${_count} + 1 ))
> (( ${_max} - ${_count} )) || exit
> done


seq()
{
inc=1
min=1
case $# in
1) max=$1 ;;
2) min=$1; max=$2; ;;
3) min=$1; inc=$2; max=$3 ;;
*) echo "Too few arguments" >&2; return 1 ;;
esac

[ $inc -eq 0 ] && return 1

while :
do
[ $inc -gt 0 ] && [ $min -gt $max ] && break
[ $inc -lt 0 ] && [ $min -lt $max ] && break
printf "%d\n" "$min"
min=$(( $min + $inc ))
done
}

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
  Réponse avec citation
 
Page generated in 0,06250 seconds with 9 queries