User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Nov 2007
Posts: 107
Reputation: k2k is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
k2k k2k is offline Offline
Junior Poster

check for $1?

  #1  
Jun 27th, 2008
hi,
does anyone know how to check if certain character is contained in the argument?

for example: > foo /home/myname

how should I check if the argument contains a "/" character?

thanks
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jan 2007
Location: Ireland
Posts: 1,581
Reputation: DimaYasny will become famous soon enough DimaYasny will become famous soon enough 
Rep Power: 6
Solved Threads: 66
Featured Poster
DimaYasny DimaYasny is offline Offline
Posting Virtuoso

Re: check for $1?

  #2  
Jun 27th, 2008
just use "basename"
Real stupidity always beats Artificial Intelligence. (Terry Pratchett)

BA BizMg, MCSE, DCSE, Linux+, Network+
Reply With Quote  
Join Date: Oct 2007
Posts: 267
Reputation: eggi is on a distinguished road 
Rep Power: 1
Solved Threads: 26
eggi eggi is offline Offline
Posting Whiz in Training

Re: check for $1?

  #3  
Jun 27th, 2008
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
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!
Reply With Quote  
Join Date: Nov 2007
Posts: 107
Reputation: k2k is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
k2k k2k is offline Offline
Junior Poster

Re: check for $1?

  #4  
Jun 28th, 2008
Originally Posted by eggi View Post
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
Reply With Quote  
Join Date: Jan 2007
Location: Ireland
Posts: 1,581
Reputation: DimaYasny will become famous soon enough DimaYasny will become famous soon enough 
Rep Power: 6
Solved Threads: 66
Featured Poster
DimaYasny DimaYasny is offline Offline
Posting Virtuoso

Re: check for $1?

  #5  
Jun 28th, 2008
first idea is to use "cut" with '/' as delimeter
Real stupidity always beats Artificial Intelligence. (Terry Pratchett)

BA BizMg, MCSE, DCSE, Linux+, Network+
Reply With Quote  
Join Date: Nov 2007
Posts: 107
Reputation: k2k is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
k2k k2k is offline Offline
Junior Poster

Re: check for $1?

  #6  
Jun 28th, 2008
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"

......
Reply With Quote  
Join Date: Oct 2007
Posts: 267
Reputation: eggi is on a distinguished road 
Rep Power: 1
Solved Threads: 26
eggi eggi is offline Offline
Posting Whiz in Training

Re: check for $1?

  #7  
Jun 28th, 2008
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
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!
Reply With Quote  
Join Date: Nov 2007
Posts: 107
Reputation: k2k is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
k2k k2k is offline Offline
Junior Poster

Re: check for $1?

  #8  
Jun 28th, 2008
Originally Posted by eggi View Post
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??
Reply With Quote  
Join Date: Nov 2007
Posts: 107
Reputation: k2k is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
k2k k2k is offline Offline
Junior Poster

Re: check for $1?

  #9  
Jun 29th, 2008
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.....
Reply With Quote  
Join Date: Oct 2007
Posts: 267
Reputation: eggi is on a distinguished road 
Rep Power: 1
Solved Threads: 26
eggi eggi is offline Offline
Posting Whiz in Training

Re: check for $1?

  #10  
Jul 1st, 2008
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
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!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Shell Scripting Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Shell Scripting Forum

All times are GMT -4. The time now is 10:44 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC