Shell script that check the argument passed to it and prints error if test condition is not met

I want to make a script that check for the argument passed to it and generates an error in case any character/string argument passed to it.

I am using below code, but its not working. can anyone help.

#!/bin/bash
if [ $@ -ne [0-9] ]; then
     echo 'An integer argument is passed to the script hence going to exit'
     exit 1
fi

Have also tried using :

#!/bin/bash
for i in $@
do
      if [ $i -ne [0-9] ]
      echo 'An integer argument is passed to the script hence going to exit'
      exit 1
     fi
done

in the first example

[[ $@ =~ [^0-9 ] ]]  # (double "[[") means if any character is not a digit or blank

In your second example you forgot "then"

1 Like

Its working... but i have some doubts, the script that i have used is :

#!/bin/bash
for i in $@
do
if [[ $i = [!0-9] ]]; then
     echo 'Encountered an string hence going to exit'
      exit 0
fi
done

could you explain me the use of "[[" in brief??

Also why can't we use operator -eq instead of = in below line

if [[ $i = [!0-9] ]]; then

-eq is for arithmetics only, the operators are converted to digits when using that.
I don't think your script is working with only = , you should use =~ .
And for [[ , read the manuals, e.g. man bash in the command line. There you have all information of "[[", "-eq" and many other things.