1

So, to run my script I use a parameter like this:

./script 789

The parameter is to change the permissions of a file or a directory, and I'd like to check if every digit is between 0 and 7, so what I tried is the next thing:

 if [[ ${1:0:1} -ge 0 && ${1:0:1} -le 7 ]] && [[ ${1:1:1} -ge 0 && ${1:1:1} -le 7]] && [[ ${1:2:1} -ge 0 && ${1:2:1} -le 7]]
 then {go ahead with the code}
 else 
     echo "Error: each digit in the parameter must be between 0 and 7"
 fi

If it's true, then go ahead with the script, else show an error message, but it doesn't seem to work.

4
  • Compare $1 with the regex [0-7][0-7][0-7] Commented Mar 3, 2016 at 13:56
  • a single char cannot be a negative number, Commented Mar 3, 2016 at 13:56
  • @WilliamPursell how do I do that? Commented Mar 3, 2016 at 13:57
  • Thank you all guys, this have been so helpful! :) Commented Mar 3, 2016 at 14:54

2 Answers 2

2

You want to match the parameter with the regex [0-7][0-7][0-7], or [0-7]{3}. In bash, you can do:

[[ "$1" =~ [0-7]{3} ]] || { echo 'invalid parameter' >&2; exit 1; }

Or:

echo "$1" | grep -Eq '[0-7]{3}' || { echo err message >&2; exit 1; }
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you!! I've got another question, what does that >&2 do?
The >&2 redirects the output of echo to stderr. It is an error message, and error messages belong on stderr.
This may seem trivial, since if you run the script as a one-off command from the command line both stderr and stdout are associated with the tty, but it is good practice and will become important when your tools are used as filters (chained in a pipeline).
You can use [0-7]{3} in bash as well; there's no need for grep here.
I didn't intend to imply that {n} cannot be used in bash, but was merely demonstrating the use of grep. Thanks for pointing out the ambiguity. I'll edit.
-1

this could be another way

#!/bin/bash

#Stored input parameter 1 in a variable
perm="$1"

#Checking if inserted parameter is empty, in that case the script will show a help
if [ -z "${perm}" ];then
        echo "Usage: $0 <permission>"
        echo "I.e.: $0 777"
        exit
else
        #if the parameter is not empy, check if each digit is between 0-7
        check=`echo $perm| grep [0-7][0-7][0-7]`
        #if the result of command is empty that means that the input parameter contains at least one digit that's differs from 0-7
        if [ -z "${check}" ];then
                echo "Error: each digit in the parameter must be between 0 and 7"
        fi
fi

that's the output

[shell] ➤ ./test9.ksh 999
Error: each digit in the parameter must be between 0 and 7

[shell] ➤ ./test9.ksh 789
Error: each digit in the parameter must be between 0 and 7

[shell] ➤ ./test9.ksh 779
Error: each digit in the parameter must be between 0 and 7

[shell] ➤ ./test9.ksh 777

4 Comments

Can I get a breif explanation of this? I'm kinda new to shell script and don't know what most of those commands do
Checking that the result of grep is non-empty is not the correct way to use grep for this sort of thing. Use the exit status instead. eg, if echo string | grep -q pattern; then
I've added some comments in the script
Using ksh as the extension for a bash script is a bit misleading.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.