More on variables, more array

yes, its a brain cracker for me🙂

to day i try again to finish the exercise to gain a bit more knowledge about bash scripting. this script is still dirty and if you have any other idea how to make it better than i will be happy to try it. I try to use conditional and looping together to get better result for the goal, but i think i failed a bit, especially at the back (b) job. But i hope i got more idea about how to handle array variable at bash scripting which i think a bit tricky😉.

this is the script:

#!/bin/bash
#
# web-browser.sh
# This script simply used wget and links to
# browser the user given address.
# The last 10 URLs entered by the user are stored in an array,
# from which the user can restore the URL by using the back functionality.
#
# note: remove # at the beginning links -dump ${ADDR[0]} command
# so the script can do the job as it should.

declare -a ADDR
declare -i a
declare -i i

a=0

while true; do

echo “This script will act as a simple text web browser,”
echo “press q to quit, b for back,”
echo -n “or please give the web address: ”
read choice

case $choice in
q)
clear
break
;;
z)
a=$a-1
if [ $a -eq 0 ]; then
echo “There is no back any more…!!!”
echo “The last record is ${ADDR[$a]}”
echo “Array $a is ${ADDR[0]}”
#links -dump ${ADDR[0]}
a=0
elif [ $a -gt 0 ]; then
echo “Array $a is ${ADDR[$a]}”
#links -dump ${ADDR[$a]}
else
clear
echo “There is no record available…!!!”
a=0
fi
;;
*)
# array will only hold 10 arrays
# if we got more, then the array will recycle
# remember array index start from 0

if [ $a -gt 9 ]; then
#links -dump $choice
ADDR[$a]=$choice
echo “Array $a is ${ADDR[$a]}”
for ((i=0;i<=9;i++)); do
ADDR[$i]=${ADDR[$i+1]}
done
a=$a-1
else
#links -dump $choice
ADDR[$a]=$choice
echo “Array $a is ${ADDR[$a]}”
fi
a=$a+1
;;
esac
echo
done

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: