In: Computer Science
Unix
Create a script that will declare an array and assign four values from the command line.
1. Use these four values - Paul, Ringo, George, John, - in that order
2. Display the content of the array, displaying the values in this format and this order
The first array value is "John"
The second array value is "Paul"
The third array value is "George"
The fourth array value is "Ringo"
Code:
Output:
Raw code:
# taking command line argument to a list
if [ "$#" -eq 0 ]; then
array=( defaultarg1 defaultarg2 )
else
array=( "$@" )
fi
# changing the order of elements
array=( ${array[3]} ${array[0]} ${array[2]} ${array[1]} )
# taking another array
strings=( "first" "second" "third" "fourth")
# lenght of array
len=${#array[@]}
# printing all elements
for (( i=0; i<$len; i++ ));
do
echo "The ${strings[$i]} array value is \"${array[$i]}\"" ; done
NOTE:
If You Have Any Doubts Feel Free To Comment In The
Comment Section.
Do Vote (LIKE).