Hey There,

You can use expr's colon (:) notation for string comparison, but it might be easier to read and write to do something with grep, like:

echo $1|grep "/" >/dev/null 2>&1

(use -q for Gnu grep and you can avoid the output redirection).

then check errno ($?). If it equals 0, your string has a "/" in it.

Hope that helps,

Mike

Hey There,

You can use expr's colon (:) notation for string comparison, but it might be easier to read and write to do something with grep, like:

echo $1|grep "/" >/dev/null 2>&1

(use -q for Gnu grep and you can avoid the output redirection).

then check errno ($?). If it equals 0, your string has a "/" in it.

Hope that helps,

Mike

thanks, and it definitely helps. I actually figured that out after a while. I have another question if anyone helps out, it would be so great.

say file=/home/admin/foo
and i want to delete the foo and leave file=/home/admin how do i do that?
i tried:
echo $file | tr '/' ' ' | sed 's/ *$//' | tr ' ' '/' #it deleted everything.. : (

can anyone tell how do i delete something in between? all the reference I read only tell the usage of ^ and $ Thanks

cut works if I know the exact input path="/home/admin/foo"

but if path is an argument.... then it won't know cut -f what field.

what i need is ...

path="$1"
and if $1 is a directory... say like path="/home/admin/foo"

then i need to remove the file foo from the dir and leave path="/home/admin"

......

Hey THere,

Your initial shot was pretty close. Just change:

file=/home/admin/foo
echo $file | tr '/' ' ' | sed 's/ *$//' | tr ' ' '/'

to

file=/home/admin/foo
echo $file |sed 's/^.*\/\(.*\)$/\1/'

and that should do it :)

-bash-3.2$ echo $file |sed 's/^.*\/\(.*\)$/\1/'
foo

Best wishes,

Mike

Hey THere,

Your initial shot was pretty close. Just change:

to

and that should do it :)

Best wishes,

Mike

Mike,
wow... you are genius! That is really helpful and I had never thought about using the saving to register.

However, i need the dir part of the $path but the last file.
$path=/home/admin/foo
result: $path=/home/admin

thanks for the big hint though, i tried to save the same first part into register 1 and the second part into register 2....... but it returns the whole path...(dont' know ) if possible please fix it.. if i get my result..i'll post it here too

my attempt:

echo $path | sed 's/\(^.*\/\):\(.*\)$/\1/'

#anything wrong??

hey all,
just to echo myself.... instead of banging my head for solution of the sed and tr wutever.. it could be done by just :
path=/home/admin/foo
dirname /home/admin/foo

thanks for everyone tried to help.....

Hey,

I know this is closed, but wanted to follow up since I missed you last time ;) Sorry

To get the basename, with sed, just so you have it for your stockpile, just change your expression:

echo $path | sed 's/\(^.*\/\):\(.*\)$/\1/'

to

echo $path | sed 's/^\(.*\)\/.*$/\1/'
/home/admin

Of course, you're right, basename and dirname are much simpler to use ;)

Cheers,

Mike

Hey,

I know this is closed, but wanted to follow up since I missed you last time ;) Sorry

To get the basename, with sed, just so you have it for your stockpile, just change your expression:

to

Of course, you're right, basename and dirname are much simpler to use ;)

Cheers,

Mike

just wanna thank mike for the follow up.... i was banging my head to get to sed to work until i saw the first post (thank to who posted too)... and i looked it up "basename" (i m pretty new to shell scripting).. and i think there must be something does the opposite of basename... and wooooo!!!... dirname leads the way!!!... thanks again for helping

This question has already been answered. Start a new discussion instead.