Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ksh comparing strings using if:fi statement

Status
Not open for further replies.

southbeach

Programmer
Jan 22, 2008
879
0
0
US
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
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
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! ;-)


--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top