10

How can I write this with an AIX script correctly? My requirement is to write this command in test.txt:

clock=$(prtconf -s | awk '{print $4,$5}')

I tried this command:

print 'clock=$(prtconf -s | awk '{print $4,$5}')' > test.txt

Output written in test.txt gives me:

clock=$(prtconf -s | awk {print ,})

If I use " " quotes:

print "clock=$(prtconf -s | awk '{print $4,$5}')"

It gives me directly to:

clock=3612 MHz

How can I solve this?

kjones
  • 107
batil
  • 311
  • Do you mean your want to use clock=$(prtconf -s | awk '{print $4,$5}') expression in awk statement? – cuonglm Jun 10 '14 at 09:59

4 Answers4

6

The simple way to do it is to use single quotes '…' around the string. Single quotes delimit a literal string, so you can put it anything between them except a single quote '. To insert a single quote in the string, use the four-character sequence '\'' (single quote, backslash, single quote, single quote).

Technically, there's no way to put a single quote inside a single-quoted literal. However consecutive literals are just as good as a single literal. 'foo'\''bar' is parsed as

  1. the single-quoted literal foo
  2. the backslash-quoted literal character '
  3. the single-quoted literal bar

This effectively means that '\'' is a way to escape a single quote in a single-quoted literal.

Note that the ksh print command performs backslash expansion. Add the -r option to avoid this. It won't hurt you because there happens to be no backslash in the string you want to print, but it's better to use -r, in case a backslash is added during maintenance.

print -r -- 'clock=$(prtconf -s | awk '\''{print $4,$5}'\'')' > test.txt

Alternatively, you can use the POSIX method to print a string literally with a newline at the end:

printf '%s\n' 'clock=$(prtconf -s | awk '\''{print $4,$5}'\'')' > test.txt
5

You need to either keep using single quotes, but then print out the ones you need in the output "separately", or use double quotes and escape the dollar signs.

For the second option:

print "clock=\$(prtconf -s | awk '{print \$4,\$5}')" > test.txt

For the first:

print 'clock=$(prtconf -s | awk '\''{print $4,$5}'\'')' > test.txt

(That's 'text' then escaped single quote \' then 'other text'.)

For the sake of completeness, note that print expands backslash-character escape sequences (this doesn't matter in your case because the string you want to print doesn't contain any backslash). To avoid this, use print -r.

Mat
  • 52,586
  • Unless I've misunderstood the question, OP wants the command to be printed in the text file, not the result (if it's the result, their second attempt is just missing a pair of quotes). – Mat Jun 10 '14 at 10:12
  • Oh,sorry, my misreading, remove comment. – cuonglm Jun 10 '14 at 10:14
1

2 solution is there as @Mat told.

1] Using single quote ' : this single quote removes special meaning of escape characters,$ , different commands (basically all bash constructs & unix commands). print command will take everything as plain characters and writes this to file test.txt

2] Using double quotes ": Here we need to explicitly escape the bash constructs. \$ removes the special meaning of $, i.e. value of variable used after $.

1

Why not just use a here document?

cat <<'END' > test.txt
clock=$(prtconf -s | awk '{print $4,$5}')
END

Note the quotes around END in <<'END': they mean that the text in the following lines (until the END marker) will be used literally. With just <<END, dollar expansion (i.e. $(command) or $variable) would be performed.

Joseph R.
  • 39,549