Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For loop to find out if directory exists in unix

I want to use a script that checks whether a list of directories exists or not and at the same time it should print some custom message that I am sending.

For example:

I have a script that validates if directory exists or not:

**check.sh**

for i in $*
   if [ -d "$i" ]; then
      echo Found <msg-i> directory.
   else
       echo <msg-i> directory not found.

Now I want to call this script like this:

./check.sh $DIR1 msg1 $Dir2 msg2 $Dir3 msg3

So if DIR1 doesn't exist then I want to display message as "msg1 directory not found", similarly for DIR2 I want to show "msg2 directory not found". Here msg1 and msg2 are something I want to pass as string. How to achieve this? I am using bash shell.

like image 230
chaitanya Avatar asked May 24 '13 12:05

chaitanya


People also ask

How do I check if a directory exists in path?

The Test-Path Cmdlet $Folder = 'C:\Windows' "Test to see if folder [$Folder] exists" if (Test-Path -Path $Folder) { "Path exists!" } else { "Path doesn't exist." } This is similar to the -d $filepath operator for IF statements in Bash. True is returned if $filepath exists, otherwise False is returned.

How do you check if a directory exists or not in bash?

Checking If a Directory Exists In a Bash Shell Script-h "/path/to/dir" ] && echo "Directory /path/to/dir exists." || echo "Error: Directory /path/to/dir exists but point to $(readlink -f /path/to/dir)." The cmd2 is executed if, and only if, cmd1 returns a non-zero exit status.

How do I find a directory in Linux?

The ls command is used to list files or directories in Linux and other Unix-based operating systems. Just like you navigate in your File explorer or Finder with a GUI, the ls command allows you to list all files or directories in the current directory by default, and further interact with them via the command line.


2 Answers

Try this:

while [ -n "$1" ]
do
    dir="$1"
    msg="$2"
    if [ -d "$dir" ]; then
        echo "$msg dir FOUND"
    else
        echo "$msg dir NOT FOUND"
    fi
    shift 2
done

shift <n> command simply shifts left positional parameters passed to the script of n positions. For example if you call a script with:

./myscript 1 2 3 4

$1 is "1" and $2 is "2"

but if you shift 2 then $1 is "3" and $2 is "4".

In this way the loop consumes 2 parameters per cycle until $1 parameter is an empty string ( -n "$1").

while condition can be written more elegantly as:

while (( $# ))

obtaining the same result.

You can also check for the second parameter (while [ -n "$2" ]) but this changes the behavior when user provides an odd number of parameters:

  • in the first case last directory will be checked but you'll have a strange message because $msg il empty
  • il the second case you'll not have strange messages, but last directory will silently not be checked

Better test parameters at the beginning:

if (( $# % 2 ))
then
    echo "Provide an even number of parameters"
    exit 1
fi
like image 117
Zac Avatar answered Oct 13 '22 13:10

Zac


Chepner Says:

The while condition can simply be (( $# )) (test if the number of positional parameters is non-zero).

Chaitanya Says:

Hi Chepner, thanks for providing alternate solution, can you please tell me how the while condition should actually look like in order to use $# , I tried different ways but it is not working for me.

Here's a quick sample:

while (( $# ))
do
   dir=$1
   msg=$2
   shift 2
   [...]
done

The while (( $# )) will be true as long as there are any command line arguments. Doing the shift twice removes arguments from the list. When no more arguments, the while loop ends.

like image 34
David W. Avatar answered Oct 13 '22 13:10

David W.