•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Shell Scripting section within the Software Development category of DaniWeb, a massive community of 403,029 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,868 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Shell Scripting advertiser: Programming Forums
Views: 755 | Replies: 10 | Solved
![]() |
•
•
Join Date: Oct 2007
Posts: 267
Reputation:
Rep Power: 1
Solved Threads: 26
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
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
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
Having trouble passing cert exams? Check out How To Pass Any Computer Certification Test!
------------------------------------------------------------------------
Having trouble passing cert exams? Check out How To Pass Any Computer Certification Test!
•
•
Join Date: Nov 2007
Posts: 107
Reputation:
Rep Power: 1
Solved Threads: 0
•
•
•
•
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
•
•
Join Date: Nov 2007
Posts: 107
Reputation:
Rep Power: 1
Solved Threads: 0
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"
......
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"
......
•
•
Join Date: Oct 2007
Posts: 267
Reputation:
Rep Power: 1
Solved Threads: 26
Hey THere,
Your initial shot was pretty close. Just change:
to
and that should do it
Best wishes,
Mike
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
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
Having trouble passing cert exams? Check out How To Pass Any Computer Certification Test!
------------------------------------------------------------------------
Having trouble passing cert exams? Check out How To Pass Any Computer Certification Test!
•
•
Join Date: Nov 2007
Posts: 107
Reputation:
Rep Power: 1
Solved Threads: 0
•
•
•
•
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/'
•
•
Join Date: Oct 2007
Posts: 267
Reputation:
Rep Power: 1
Solved Threads: 26
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
I know this is closed, but wanted to follow up since I missed you last time
SorryTo 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
Last edited by eggi : Jul 1st, 2008 at 12:09 am.
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
Having trouble passing cert exams? Check out How To Pass Any Computer Certification Test!
------------------------------------------------------------------------
Having trouble passing cert exams? Check out How To Pass Any Computer Certification Test!
![]() |
•
•
•
•
•
•
•
•
DaniWeb Shell Scripting Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Slow Network Access Not Always Due to Scheduled Tasks Check (Windows tips 'n' tweaks)
- USB Ports not recognizing periferals (Peripherals)
- Please Check The Errors'..Dont know what else to do (C++)
- Check items before installing for Spyware? (Viruses, Spyware and other Nasties)
- Another hijack this log to check (Viruses, Spyware and other Nasties)
- Spyware check (Windows Users Lounge)
- Check for Disk Errors in Windows XP (Windows tips 'n' tweaks)
- IE6 - dialogue boxes , check boxes and radio buttons work very slowly after hijacking (Viruses, Spyware and other Nasties)
- Ureaka!! I found it! Please check this log (Web Browsers)
- flash check it out (Geeks' Lounge)
Other Threads in the Shell Scripting Forum
- Previous Thread: unix little quiz
- Next Thread: please take a look at my question



Linear Mode