LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-26-2007, 12:58 PM   #1
nadavvin
Member
 
Registered: May 2006
Distribution: ubuntu
Posts: 109

Rep: Reputation: 21
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
 
Old 01-26-2007, 01:11 PM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,943
Blog Entries: 5

Rep: Reputation: 753Reputation: 753Reputation: 753Reputation: 753Reputation: 753Reputation: 753Reputation: 753
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.

Last edited by MensaWater; 01-26-2007 at 01:15 PM.
 
Old 01-26-2007, 01:24 PM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 62
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
 
Old 01-26-2007, 02:33 PM   #4
nadavvin
Member
 
Registered: May 2006
Distribution: ubuntu
Posts: 109

Original Poster
Rep: Reputation: 21
Quote:
Use the case statement:
Thanks, nice solution.
 
Old 01-26-2007, 05:38 PM   #5
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 718

Rep: Reputation: 72
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
 
Old 01-27-2007, 02:41 AM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 62
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
batch append string to the end of a determined line in text files osio Programming 6 06-30-2005 09:28 AM
Bash way to tell if String is in String tongar Programming 3 06-16-2005 06:59 AM
How to get rid of the begining and end space of a string for PERL?? cqmyg5 Slackware 2 04-28-2005 04:34 PM
Wierd String appended to end of line lel800 General 8 12-28-2004 03:55 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM


All times are GMT -5. The time now is 01:03 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration