Single quotes and variables inside a bourne shell

1

I have this command that I'm trying to execute in the bourne shell for Solaris 9.

find ${DATADIR} -name "check_*" -type f -exec sh -c '$0 | ${PARSEDATA}' {} \; >> ${TMP_1}

My problem is, the variable ${PARSEDATA} won't work properly, I have tried using double quotes around it, then the problem is $0 is the name of the script executing this code, not the file located using the find command.

Any suggestions to solve this, probably easy problem, is greatly helpful.

Thanks in advance.

Update: $0 is the name of the script located using the find command, the variable for ${PARSEDATA} is a filter, located somewhere on the system that modifies the output of each located script. However, the problem is, I can't seem to get both $0 and ${PARSEDATA} to expand properly, either $0 expands fine using single quotes, or using double qoutes ${PARSEDATA} expands fine, but then $0 becomes the name of the orginal script that runs this find-command.

user32178

Posted 2011-04-12T09:16:40.093

Reputation: 231

It would help if you explain what you expect this command to achieve, especially the $0 and PARSEDATA stuff. – jlliagre – 2011-04-12T09:23:31.007

@jlliagre, Clear enough? – user32178 – 2011-04-12T09:28:56.593

Answers

0

I'm not sure of what you are trying to do? But if you want to look for scripts named check_* and then run it and pipe it through a program ${PARSE_DATA}, well I guess the file located by find is not $0 but {}

Example

find /var/ftp/mp3 -name "*.mp3" -type f -exec chmod 644 {} \; (Wikipedia)

So I think you can use your double quotes :

find ${DATADIR} -name "check_*" -type f -exec sh -c '{} | ${PARSEDATA}' \; >> ${TMP_1}

I've tried successfully :

PG='sed s/Blah/Bleh/'
find . -name "check_*" -type f -exec sh -c "{} | $PG" \;

> Bleh Blah

M'vy

Posted 2011-04-12T09:16:40.093

Reputation: 3 540

Have you checked so sh is simply not a symlink towards bash and that you're not using a GNU find? – user32178 – 2011-04-12T09:39:31.080

Hummmm in fact, it's a symlink to /bin/dash... what is the point of calling it sh then. Damn it. But seems to be GNU find by the way. – M'vy – 2011-04-12T09:54:52.817

Oh, just by curiosity : what does my example do on your pure sh? – M'vy – 2011-04-12T10:16:56.737

It's because there must be a /bin/sh binary that is compatible with Bourne shell. – user1686 – 2011-04-12T13:11:29.797

0

Keep the single quotes, the issue is very likely you haven't exported the PARSEDATA variable.

export PARSEDATA
find ${DATADIR} -name "check_*" -type f -exec sh -c '$0 | ${PARSEDATA}' {} \; >> ${TMP_1}

jlliagre

Posted 2011-04-12T09:16:40.093

Reputation: 12 469

0

If you want $0 to not be expanded by the shell (using single quotes) and ${PARSEDATA} be expanded, then why not just use different quotes for each:

find ${DATADIR} -name "check_*" -type f -exec sh -c '$0 | '"${PARSEDATA}" {} \; >> ${TMP_1}

As long as you keep the quoted parts joined, the shell will just expand ${PARSEDATA} and pass the whole command as a single token to find.

Jaap Eldering

Posted 2011-04-12T09:16:40.093

Reputation: 7 596