Computing.Net > Forums > Unix > Check if variable starts with /

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

Check if variable starts with /

Reply to Message Icon

Original Message
Name: Jens Nielsen
Date: April 26, 2004 at 13:23:38 Pacific
Subject: Check if variable starts with /
OS: HP Unix
CPU/Ram: ?
Comment:

Hello,

I'm trying find a way to check if a variable starts with forward slash "/".
Whoever, nothing that I have tried seems to work. For example, I want to perform the following check on my "out_dir" variable. But it fails the check

if [ $out_dir != /* ]
then
echo Error! - Invalid directory specified
echo Script will exit - please run it again
exit
fi

Any assistance would be great.

Jens



Report Offensive Message For Removal


Response Number 1
Name: Wolfbone
Date: April 26, 2004 at 14:49:02 Pacific
Reply: (edit)

if [ $(echo $out_dir | sed 's%^/%%') = $outdir ]
then
....
....

If your shell is bash (maybe also ksh?):

if [[ ${out_dir:0:1} != "/" ]]
then
....
....


Report Offensive Follow Up For Removal

Response Number 2
Name: Jens Nielsen
Date: April 27, 2004 at 06:53:32 Pacific
Reply: (edit)

The first suggestion seemed to do the trick.

Thanks!


Report Offensive Follow Up For Removal

Response Number 3
Name: WilliamRobertson
Date: May 1, 2004 at 16:30:22 Pacific
Reply: (edit)

Pretty straightforward in ksh:

b=/blah/blah

[[ $b = /* ]] && print Yep
Yep



Report Offensive Follow Up For Removal







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home








Do you have your own blog?

Yes
No
I did before
I will soon


View Results

Poll Finishes In 2 Days.
Discuss in The Lounge
Poll History




Data Recovery Software