LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How can I check if there slash in the end of string in bash if? (https://www.linuxquestions.org/questions/programming-9/how-can-i-check-if-there-slash-in-the-end-of-string-in-bash-if-523060/)

nadavvin 01-26-2007 12:58 PM

How can I check if there slash in the end of string in bash if?
 
Hello

I want to create a small bash script which copy file or directory to a new place.

The script create the sub directories of the new place if they don't exist (mkdir -p).

If there is something like that I will happy to here and to save time.

How do I recognize if a given new place is a directory or file or directory with file?

dirname give the directory but if its input is directory it bring only the parent directory path:
Code:

$ dirname /a/b/s
/a/b
[nadav@myhost ~]$ dirname /a/b/
/a

How do I check if there "/" in the end of a string?

Nadav

MensaWater 01-26-2007 01:11 PM

In Regular Expressions (a/k/a regexp) $ means end of line.

e.g. grep a$ myfile
would find all lines that ended with "a".

Since / is a special character you may need to escape it with \.

e.g. grep \/$ myfile

However you might want to use the "find" command:

e.g. find mydir -type d
Would find all subdirectories of the directory, mydir. Using this find syntax you would ONLY be working on directories and they'd all have the same format in output.

P.S. Of course $ also has meaning to the shell as the start of variable names or numbers. You need to be sure you're aware of how it is being treated in the context you're using it in.

e.g. grep $VAR$ myfile
Would give you very odd results. Using ${VAR}$ on the other hand would clearly distinguish that you meant $VAR as the variable and were looking for whatever it was at the end of the line.

matthewg42 01-26-2007 01:24 PM

Quote:

How do I check if there "/" in the end of a string?

Nadav
Use the case statement:
Code:

#!/bin/bash

str="this/is/my/string"
case "$str" in
*/)
    echo "has slash"
    ;;
*)
    echo "doesn't have a slash"
    ;;
esac


nadavvin 01-26-2007 02:33 PM

Quote:

Use the case statement:
Thanks, nice solution.

makyo 01-26-2007 05:38 PM

Hi.

The real question appears to be:
Quote:

How do I recognize if a given new place is a directory or file or directory with file?
The fact that a string ends in a slash does not mean that the string refers to a directory.

If that is a fair assessment, then I would recommend testing the string to see if it does refer to a directory or not. Using test or [ with the predicate -d is of value here. Of course, you may wish to do more detailed testing.

For example:
Code:

#!/bin/sh

# @(#) s1      Demonstrate test for directory.

# Clear debris.

rm -rf d1 t1 j1

isdir()
{
        local ITEM="$1"
        if [ -d "$ITEM" ]
        then
                return 1
        else
                return 0
        fi
}

echo
echo " Current items:"
touch t1
mkdir d1
ls -ld d1 t1 j1

echo
echo " Testing both items:"

FILE=./t1
if  isdir "$FILE"
then
        echo " Item $FILE is NOT a directory."
else
        echo " Item $FILE is a directory."
fi

FILE=./d1
if  isdir "$FILE"
then
        echo " Item $FILE is NOT a directory."
else
        echo " Item $FILE is a directory."
fi

FILE=./j1
if  isdir "$FILE"
then
        echo " Item $FILE is NOT a directory."
else
        echo " Item $FILE is a directory."
fi

Which results in:
Code:

% ./s1
ls: j1: No such file or directory

 Current items:
drwxr-xr-x  2 makyo makyo 48 Jan 26 17:35 d1
-rw-r--r--  1 makyo makyo  0 Jan 26 17:35 t1

 Testing both items:
 Item ./t1 is NOT a directory.
 Item ./d1 is a directory.
 Item ./j1 is NOT a directory.

Best wishes ... cheers, makyo

matthewg42 01-27-2007 02:41 AM

Note that -d will not return true if a path is a symbolic link to a directory. If you want to test for this, you should recursively follow symlinks using readlink.


All times are GMT -5. The time now is 09:15 PM.