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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
Due to network maintenance being performed by our provider, LQ will be down starting at 05:01 AM UTC. The exact duration of the downtime isn't currently known. We apologize for the inconvenience.
|
 |
01-26-2007, 12:58 PM
|
#1
|
|
Member
Registered: May 2006
Distribution: ubuntu
Posts: 109
Rep:
|
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
|
|
|
|
01-26-2007, 01:11 PM
|
#2
|
|
Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,644
|
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.
|
|
|
|
01-26-2007, 01:24 PM
|
#3
|
|
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530
Rep:
|
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
|
|
|
|
01-26-2007, 02:33 PM
|
#4
|
|
Member
Registered: May 2006
Distribution: ubuntu
Posts: 109
Original Poster
Rep:
|
|
|
|
|
01-26-2007, 05:38 PM
|
#5
|
|
Member
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 713
Rep:
|
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
|
|
|
|
01-27-2007, 02:41 AM
|
#6
|
|
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530
Rep:
|
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 10:49 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|