Computing.Net > Forums > Unix > Unix Script file read problem

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Unix Script file read problem

Reply to Message Icon

Name: EdG
Date: February 21, 2009 at 09:31:22 Pacific
OS: SCO UNIX R5
Subcategory: General
Comment:

I have filenames on this system that start with a # that will not copy. Seems the # is the problem. Script below put the bad name to a file and used sed to replace the #. Ok, but the read executes twice (EOF I assume) and I lose the value.
Asking for a suggestion on how I can value $FN updated the result of the first read of $NAME.

FN=#FN00001
export FN
echo $FN Input FN

# Output bad filename to work file
echo $FN > work

# Use sed to substitute the offending # in the work file
sed 's/#/X/g' work > work2

#read the work file to get Good filename
cat work2 | while read NAME
do
echo $NAME 3
NF=$NAME
echo $NF 3
done

echo $NAME 4
echo $NF 4
exit



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: February 21, 2009 at 10:27:33 Pacific
Reply:

No, it's not an EOF issue. The problem is the # sign is a special character to the unix shell (It's the comment sign). To take away it's special meaning you must excape it:

#!/bin/ksh

FN=\#FN00001
echo "$FN" # should display #FN00001

If you want to use the # sign directly with the unix copy command, this will work:

cp \#FN00001 newfile


0

Response Number 2
Name: EdG
Date: February 21, 2009 at 10:57:26 Pacific
Reply:

Thanks for the quick response. Problem is the trouble filenames are passed as a parameter from another script ( FN=$4 ) and don't always contain the #. I like the idea of escaping ( / )when the # is there and this works. Anyway I can test to see if position 1 of the filename contains a #? Then I can set a variable and change the copy regular or copy escape based on the variable.


0

Response Number 3
Name: nails
Date: February 21, 2009 at 12:00:17 Pacific
Reply:

I have an example for you. Assume that shell script parent.ss calls child.ss and passes one command line argument:

#!/bin/ksh

# parent.ss
F1=\#F00001

./child.ss "$F1"
# end parent.ss

In child.ss, check whether the first argument starts with a # sign:

#!/bin/ksh

if [[ $# -eq 1 ]]
then
   if  echo "$1"| egrep "^\#" > /dev/null
   then
      echo "$1 starts with a # sign"
   fi
fi
# end child.ss

BTW, grep and it's cousins, i.e. egrep, aren't always portable using regular expressions.

0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More






Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Unix Script file read problem

C shell unix script file www.computing.net/answers/unix/c-shell-unix-script-file/4444.html

help redoing unix script student db www.computing.net/answers/unix/help-redoing-unix-script-student-db/5151.html

Shell script to read a file and pro www.computing.net/answers/unix/shell-script-to-read-a-file-and-pro/4304.html