2

The bash script does many things. However, at beginning it should start if the input passed to the script is a directory path. For example, right way of telling directory path is "/var/lib/test", and incorrect way is "/var/%%^?/test".

where test is a directory and not a file.

The directory path can contain characters. [A-Za-z0-9/_-]

My script is as below -

#!/bin/bash
dirPath="/var/lib/test"
if [[ ! ${dirPath} =~ "^/[A-Za-z0-9/_-]+$" ]]; then
    echo "Success"
else
    echo "Failure"
fi

Here, dirPath can have any path provided from another script.

please let me know the error here.

  • I don't exactly understand the question. So the above script returns Success. Is that expected? Can you give some examples of when it should return Failure and Success? – Sparhawk Sep 28 '18 at 11:45
  • Is the issue the characters in the path? /var/%%^?/test is a valid pathname on Unix. Would it be enough to test whether the given path denotes an existing directory? – Kusalananda Sep 28 '18 at 11:45
  • The script should test if the given directory path is valid or not. Valid directory path includes characters - [A-Za-z0-9_-/] – user7290726 Sep 28 '18 at 11:50
  • 1
    @Goro Inside [...] in a regular expression, there are no delimiters. Even backslashes would be taken as literal backslashes. – Kusalananda Sep 28 '18 at 12:23
  • 1
    @Goro, Bash's =~ doesn't take slashes to delimit the regex – ilkkachu Sep 28 '18 at 12:24
4
if [[ ! ${dirPath} =~ "^/[A-Za-z0-9/_-]+$" ]]; then
    echo "Success"

This will test $dirPath against the literal string ^/[A-Za-z0-9/_-]+$, and invert the result. In other words, it will print Success when $dirPath is anything other than that literal string. This is because quoted characters in a regex lose their special meaning in current versions of Bash, and of course the ! works to invert the test.

If you want to test that variable against the actual regex, it's easiest to put the regex in a variable first, and leave it unquoted (using a variable may not be strictly necessary, but avoids some parsing issues with complex regexes):

dirPath="/var/lib/test"
re='^/[A-Za-z0-9/_-]+$'
if [[ ${dirPath} =~ $re ]]; then
    echo "'$dirPath' starts with a slash, and is composed of alphanumerics, hyphens, underscores and slashes only"
else
    echo "'$dirPath' is of a forbidden format"
fi

Of course, if you want to test if that path is an existing directory, then you'd use -d:

if [[ -d $dirPath ]]; then
    echo "'$dirPath' exists and is a directory
fi
4

Remove the quotes from the regex:

[[ ! "${dirPath}" =~ ^/[A-Za-z0-9/_-]+$ ]]

Quoting the regex forces string matching since Bash 3.2.

Compare this question on Stackoverflow.

Note: while this fixes your script, it does not properly check if the path is a correct directory path. Refer to @Kusalanandas answer how to do a proper check.

  • Someone out there is right now waiting for new Q or A to immediate downvote. – pLumo Sep 28 '18 at 11:59
  • 2
    what's going on? 3 downvotes to a valid answer that provides information from an answer from SO that has 102 upvotes. Still nothing in the comments. – pLumo Sep 28 '18 at 12:02
  • Also, four upvotes... – Kusalananda Sep 28 '18 at 13:40
2

This answer focuses on the first part of the question which (currently) reads:

However, at beginning it should start if the input passed to the script is a directory path.

The rest of the question is an attempt at doing this by matching a particular regular expression against a pathname to try to figure out whether it's a valid directory pathname.

A script should not generally impose an arbitrary naming scheme on the user's directories or files without reason. The pathname /var/%%^?/test is perfectly valid on a system where there exists a directory called %%^? under /var and where this directory contains a subdirectory called test.

What your script is really concerned about is whether it's given a pathname to an existing directory. Whatever the directory path is is less important.

This may be done with

#!/bin/bash

dirPath=$1

if [ ! -d "$dirPath" ]; then
    printf '"%s" is not a valid directory path\n' "$dirPath" >&2
    exit 1
fi

After this if-statement, you know that "$dirPath" is the path to an existing directory. The pathname to the directory is (in the above code) taken from the first command line argument.

  • 1
    @RoVo Your initial comment makes some sense though, so I will clarify. Thanks! – Kusalananda Sep 28 '18 at 12:24
  • thanks :) you answered the question correctly but the title of the Q is very misleading. – pLumo Sep 28 '18 at 13:15
0

to check if its file or dir you don't need regex - use:

if [ -d "${testpath}" ] ; then
    echo "$testpath is a directory";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.