×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

ksh comparing strings using if:fi statement

ksh comparing strings using if:fi statement

ksh comparing strings using if:fi statement

(OP)
I have this simple script

CODE

#!/bin/ksh
MyString=/dir/dir/dir/34/54/39/98-1.pdf
x=${#MyString}
echo "x: ${x}"
y=1
#while (( y <= ${#MyString} ))
while (( x > 0 ))
do
  xchar=$(expr substr "$MyString" $x 1)
  if (( "${xchar}" == "/" ));
  then
          echo "fname set to: ${fname}"
          exit
  fi
  fname="${xchar}${fname}"
  let x=x-1
done 
I need to extract the file name off a given directory tree or path. I am having hell of a hard time comparing two simple strings as attempted above.

I have tried various approaches using awk and other syntax as I've found online but nothing seems to work.

I have resolved to parse through the string from end of string back until I find the first "/" thus presumably having successfully extracted the file name into a string "$fname"

That being said, I am running into an "arithmetic syntax error" on line 10 that reads

CODE

line 10:  "f" == "/" : arithmetic syntax error 

Any assistance with this will be truly appreciated!

Thanks,

--
SouthBeach
http://www.fpgroups.com
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.

RE: ksh comparing strings using if:fi statement

(OP)
Never mind, solved it!

CODE

#!/bin/ksh
MyString=/dir/dir/dir/34/54/39/98-1.pdf
x=${#MyString}
echo "x: ${x}"
y=1
#while (( y <= ${#MyString} ))
while (( x > 0 ))
do
  xchar=$(expr substr "$MyString" $x 1)
  if [ ${xchar} == "/" ];
  then
          echo "fname set to: ${fname}"
          exit
  fi
  fname="${xchar}${fname}"
  let x=x-1
done 

Notice change within if:fi expression! winky smile

--
SouthBeach
http://www.fpgroups.com
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close