More on variables, array

Tugas latihan dari belajar bash kali ini mengenai variable, terutama sekali tentang array. Array merupakan salah satu jenis variable yang istimewa karena dapat di “isi” dengan kapasitas yang tidak terbatas (teoritis).

Setelah beberapa hari membaca dan melatih bagaimana variable di pergunakan dan dioperasikan dalam script bash, maka ini adalah salah satu tugas latihannya:

+ Write a script that does the following:

  • Display the name of the script being executed.
  • Display the first, third and tenth argument given to the script.
  • Display the total number of arguments passed to the script.
  • If there were more than three positional parameters, use shift to move all the values 3 places to the left.
  • Print all the values of the remaining arguments.
  • Print the number of arguments.

Test with zero, one, three and over ten arguments.

Saya buat seperti ini, my script is:

#!/bin/bash
#
# display_it.sh
# This script will display and manipulate argumen wich given to script…
# This script use array variable to store the argument.

declare -a ARGM

echo “This script you are using is: $0” | sed ‘s/.\///’
echo “You passed $# argument(s).”
ARGM=($@) #($@) equal to ($1 $2 $3 … $($#))


echo “Your argument is: ${ARGM[*]}”
echo “The 1st argument is : ${ARGM[0]}”
echo “The 3rd argument is : ${ARGM[2]}”
echo “The 10th argumen is : ${ARGM[9]}”

# If there were more than three positional parameters,
# use shift to move all the values 3 places to the left.

if [ $# -gt 3 ]; then
shift 3
ARGM=($@)
echo “The argument left after shifted by 3 is: ${ARGM[*]}”

# Print the number of arguments.
# Thanks to ANTON CHANG <ant_on_2083[at]yahoo.com> to show me some trick…

declare -i i
declare -i n
i=0
n=$#
echo -n “The number of argument is: ”
while [ $i -lt $n ]; do
echo -n “$i ”
shift
i=$i+1
done
echo
fi

Saya yakin script ini perlu dievaluasi lagi supaya lebih baik…

Ok, let us try how it work…

test with one argument :

ari@alpha5:~/doc/bash$ ./display_it.sh one
This script you are using is: display_it.sh
You passed 1 argument(s).
Your argument is: one
The 1st argument is : one
The 3rd argument is :
The 10th argumen is :
ari@alpha5:~/doc/bash$

with three arguments :

ari@alpha5:~/doc/bash$ ./display_it.sh one two three
This script you are using is: display_it.sh
You passed 3 argument(s).
Your argument is: one two three
The 1st argument is : one
The 3rd argument is : three
The 10th argumen is :
ari@alpha5:~/doc/bash$

with ten arguments :

ari@alpha5:~/doc/bash$ ./display_it.sh one two three four five six seven eight nine ten
This script you are using is: display_it.sh
You passed 10 argument(s).
Your argument is: one two three four five six seven eight nine ten
The 1st argument is : one
The 3rd argument is : three
The 10th argumen is : ten
The argument left after shifted by 3 is: four five six seven eight nine ten
The number of argument is: 0 1 2 3 4 5 6
ari@alpha5:~/doc/bash$

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: