Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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

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 #FN00001If you want to use the # sign directly with the unix copy command, this will work:
cp \#FN00001 newfile

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.

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.

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |