1

I've been writing a script with #!/bin/bash shebang for a school project but I found out few hours ago that the shebang has to be #!/bin/sh. Could you advise me how to format the second if-condition so it would be compatible for every shell script?

if [[ -z $date ]]; then
   echo "No date argument"
   exit 1
elif [[ $date =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]]; then
   return
1
  • 2
    Actually, it is best to write if [ -z "$date" ]: if $date is really empty, without double quotes it will fail. And there is no need of double brackets in this comparison. Commented Mar 24, 2018 at 21:21

1 Answer 1

3

With GNU grep.

Replace

[[ $date =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]]

with

echo "$date" | grep -q -E '^[0-9]{4}-[0-9]{2}-[0-9]{2}$'

or use

echo "$date" | egrep '^[0-9]{4}-[0-9]{2}-[0-9]{2}$' >/dev/null
Sign up to request clarification or add additional context in comments.

Comments

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.