99 Bottles of Beer/Shell: Difference between revisions

moving code from main task-page to sub-page
(moving code from main task-page to sub-page)
 
(moving code from main task-page to sub-page)
Line 3:
[[99 Bottles of Beer]] done in any of the Shell-languages.
__toc__
 
=={{header|UNIX Shell}}==
{{works with|Bourne Shell}}
<lang bash>#!/bin/sh
 
i=99 s=s
 
while [ $i -gt 0 ]; do
echo "$i bottle$s of beer on the wall"
echo "$i bottle$s of beer
Take one down, pass it around"
# POSIX allows for $(( i - 1 )) but some older Unices didn't have that
i=`expr $i - 1`
[ $i -eq 1 ] && s= || s=s
echo "$i bottle$s of beer on the wall
"
done</lang>
 
{{works with|Bash}}
{{works with|ksh93}}
{{works with|zsh}}
<lang bash>bottles() {
beer=$1
[ $((beer)) -gt 0 ] && echo -n $beer || echo -n "No more"
echo -n " bottle"
[ $((beer)) -ne 1 ] && echo -n "s"
echo -n " of beer"
}
 
for ((i=99;i>=0;i--)); do
((remaining=i))
echo "$(bottles $remaining) on the wall"
echo "$(bottles $remaining)"
if [ $((remaining)) -eq 0 ]; then
echo "Go to the store and buy some more"
((remaining+=99))
else
echo "Take one down, pass it around"
((remaining--))
fi
echo "$(bottles $remaining) on the wall"
echo
done</lang>
 
==={{header|C Shell}}===
See [[99 Bottles of Beer/Shell]]
<lang csh>@ i=99
set s=s
while ($i > 0)
echo "$i bottle$s of beer on the wall"
echo "$i bottle$s of beer"
echo "Take one down, pass it around"
@ i = $i - 1
if ($i == 1) then
set s=
else
set s=s
endif
echo "$i bottle$s of beer on the wall"
echo ""
end</lang>
Anonymous user