i want a shell script which takes as arguments a set of numbers and the array and sorts them in ascending or descending order.Also i should make the appropriate checks to ensure that the arguments given will be numbers (after 1st argument). output:
./script1.sh incr 4 5 6 0
0
4
5
6
./script2.sh dec 4 5 6 0
6
5
4
0
My try:
#!/bin/bash
k="$1"
shift
declare -i x
x=($#)
if [ $x == 0 ]
then
echo "give dec or incr and numbers"
exit 1
fi
arr=($*)
if [ $k == "dec" ]
then
for i in ${arr[@]}
do
echo $i
done > file1sorted
sort -nr file1sorted
rm file1sorted
fi
if [ $k == "incr" ]
then
for i in ${arr[@]}
do
echo $i
done > file1sorted
sort -n file1sorted
rm file1sorted
fi
It runs well but checking if parameters are numbers doesn't work.
my output:
324
231
2
1
a
I want it to give me an error if I put something that is not a number