Preventing parameter expansion multiple times
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I have a code that is writing a script which is writing another script:
cat > step1.sh <<-EOF
*other commands*
cat > step2.sh <<-EOF4
if [ -f PM.log ]; then
awk '/testphrase/ f=1;r=""; next f && /testphrase2/f=0f r=(r=="")? $0: r RS $0 END print r ' PM.log > tmp1.log
checkfree=($(cat tmp1.log))
wait
sed -i '$ d' tmp$mold.log
wait
fi
EOF4
qsub step2.sh
EOF
qsub step1.sh
I am trying to prevent parameter expansion with the "$0" in line 5 of the code as well as with line 6 of the code ($(cat tmp1.log)).
I am aware that using "" before the $ would prevent the parameter expansion once:
$0
However, because this script is written by another script, I don't know how to manipulate to prevent double and triple expansions as each script is being written.
I also know that you can edit the end of file token but there are other parameters (such as mold in line 8) within the file that I DO want to expand so I can't do that either.
How can I stop these particular terms from being expanded every time?
shell scripting parameter
add a comment |Â
up vote
2
down vote
favorite
I have a code that is writing a script which is writing another script:
cat > step1.sh <<-EOF
*other commands*
cat > step2.sh <<-EOF4
if [ -f PM.log ]; then
awk '/testphrase/ f=1;r=""; next f && /testphrase2/f=0f r=(r=="")? $0: r RS $0 END print r ' PM.log > tmp1.log
checkfree=($(cat tmp1.log))
wait
sed -i '$ d' tmp$mold.log
wait
fi
EOF4
qsub step2.sh
EOF
qsub step1.sh
I am trying to prevent parameter expansion with the "$0" in line 5 of the code as well as with line 6 of the code ($(cat tmp1.log)).
I am aware that using "" before the $ would prevent the parameter expansion once:
$0
However, because this script is written by another script, I don't know how to manipulate to prevent double and triple expansions as each script is being written.
I also know that you can edit the end of file token but there are other parameters (such as mold in line 8) within the file that I DO want to expand so I can't do that either.
How can I stop these particular terms from being expanded every time?
shell scripting parameter
It looks as ifEOF4
is the ending of an internal here-document, but I can't see where that here-document starts...
â Kusalananda
Feb 10 at 8:06
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have a code that is writing a script which is writing another script:
cat > step1.sh <<-EOF
*other commands*
cat > step2.sh <<-EOF4
if [ -f PM.log ]; then
awk '/testphrase/ f=1;r=""; next f && /testphrase2/f=0f r=(r=="")? $0: r RS $0 END print r ' PM.log > tmp1.log
checkfree=($(cat tmp1.log))
wait
sed -i '$ d' tmp$mold.log
wait
fi
EOF4
qsub step2.sh
EOF
qsub step1.sh
I am trying to prevent parameter expansion with the "$0" in line 5 of the code as well as with line 6 of the code ($(cat tmp1.log)).
I am aware that using "" before the $ would prevent the parameter expansion once:
$0
However, because this script is written by another script, I don't know how to manipulate to prevent double and triple expansions as each script is being written.
I also know that you can edit the end of file token but there are other parameters (such as mold in line 8) within the file that I DO want to expand so I can't do that either.
How can I stop these particular terms from being expanded every time?
shell scripting parameter
I have a code that is writing a script which is writing another script:
cat > step1.sh <<-EOF
*other commands*
cat > step2.sh <<-EOF4
if [ -f PM.log ]; then
awk '/testphrase/ f=1;r=""; next f && /testphrase2/f=0f r=(r=="")? $0: r RS $0 END print r ' PM.log > tmp1.log
checkfree=($(cat tmp1.log))
wait
sed -i '$ d' tmp$mold.log
wait
fi
EOF4
qsub step2.sh
EOF
qsub step1.sh
I am trying to prevent parameter expansion with the "$0" in line 5 of the code as well as with line 6 of the code ($(cat tmp1.log)).
I am aware that using "" before the $ would prevent the parameter expansion once:
$0
However, because this script is written by another script, I don't know how to manipulate to prevent double and triple expansions as each script is being written.
I also know that you can edit the end of file token but there are other parameters (such as mold in line 8) within the file that I DO want to expand so I can't do that either.
How can I stop these particular terms from being expanded every time?
shell scripting parameter
shell scripting parameter
edited Feb 10 at 14:57
asked Oct 9 '17 at 16:57
A. B
224
224
It looks as ifEOF4
is the ending of an internal here-document, but I can't see where that here-document starts...
â Kusalananda
Feb 10 at 8:06
add a comment |Â
It looks as ifEOF4
is the ending of an internal here-document, but I can't see where that here-document starts...
â Kusalananda
Feb 10 at 8:06
It looks as if
EOF4
is the ending of an internal here-document, but I can't see where that here-document starts...â Kusalananda
Feb 10 at 8:06
It looks as if
EOF4
is the ending of an internal here-document, but I can't see where that here-document starts...â Kusalananda
Feb 10 at 8:06
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
1
down vote
Quoting the end of line token does this. Quote it in your first line:
cat > run1.sh <<-"EOF"
I can't do this because there are other parameters in the script that I DO want to expand. Quoting the end of line token would prevent all the expansions.
â A. B
Feb 10 at 0:45
add a comment |Â
up vote
0
down vote
Consider the following example script (all indentations are literal tabs):
#!/bin/sh
var='hello'
cat >script1.sh <<-END_SCRIPT1
#!/bin/sh
echo 'This is script1 ($var)'
printf '$0 is "%s"n' "$0"
cat >script2.sh <<END_SCRIPT2
#!/bin/sh
echo 'This is script2 ($var)'
printf '\$0 is "%s"n' "\$0"
END_SCRIPT2
END_SCRIPT1
In the outer here-document, we need to escape the $
in "$0"
as "$0"
to not have it expanded by the script writing the document.
To be able to write the inner here-document, we need to make sure that the outer here-document is writing "$0"
. The backslash needs to be escaped as \
, which turns the string into "\$0"
.
The script above writes script1.sh
as
#!/bin/sh
echo 'This is script2 (hello)'
printf '$0 is "%s"n' "$0"
END_SCRIPT2
printf '$0 is "%s"n' "$0"
Running this outputs
This is script1 (hello)
$0 is "script1.sh"
... and script2.sh
is created:
#!/bin/sh
echo 'This is script2 (hello)'
printf '$0 is "%s"n' "$0"
Notice that the inner here-document is created using <<END_SCRIPT2
, not <<-END_SCRIPT2
. Any leading tabs will be eaten up when the outer here-document is created (as evident when looking at script1.sh
).
As an aside, you don't need the two calls to wait
in your code since you don't seem to run any background processes.
Your awk
code is also faulty as the f && /testphrase2/
condition would never be true. If /testphrase/
matches, then next
is executed, forcing the code to continue with the next line of input. If /testphrase/
does not match, then /testphrase2/
won't match either.
Your could try this instead:
awk '!f && /testphrase/ f = 1; r = ""
f && /testphrase2/ f = 0
f r = (r == "" ? $0 : r RS $0)
END print r '
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Quoting the end of line token does this. Quote it in your first line:
cat > run1.sh <<-"EOF"
I can't do this because there are other parameters in the script that I DO want to expand. Quoting the end of line token would prevent all the expansions.
â A. B
Feb 10 at 0:45
add a comment |Â
up vote
1
down vote
Quoting the end of line token does this. Quote it in your first line:
cat > run1.sh <<-"EOF"
I can't do this because there are other parameters in the script that I DO want to expand. Quoting the end of line token would prevent all the expansions.
â A. B
Feb 10 at 0:45
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Quoting the end of line token does this. Quote it in your first line:
cat > run1.sh <<-"EOF"
Quoting the end of line token does this. Quote it in your first line:
cat > run1.sh <<-"EOF"
answered Oct 9 '17 at 17:06
Tomasz
8,08752560
8,08752560
I can't do this because there are other parameters in the script that I DO want to expand. Quoting the end of line token would prevent all the expansions.
â A. B
Feb 10 at 0:45
add a comment |Â
I can't do this because there are other parameters in the script that I DO want to expand. Quoting the end of line token would prevent all the expansions.
â A. B
Feb 10 at 0:45
I can't do this because there are other parameters in the script that I DO want to expand. Quoting the end of line token would prevent all the expansions.
â A. B
Feb 10 at 0:45
I can't do this because there are other parameters in the script that I DO want to expand. Quoting the end of line token would prevent all the expansions.
â A. B
Feb 10 at 0:45
add a comment |Â
up vote
0
down vote
Consider the following example script (all indentations are literal tabs):
#!/bin/sh
var='hello'
cat >script1.sh <<-END_SCRIPT1
#!/bin/sh
echo 'This is script1 ($var)'
printf '$0 is "%s"n' "$0"
cat >script2.sh <<END_SCRIPT2
#!/bin/sh
echo 'This is script2 ($var)'
printf '\$0 is "%s"n' "\$0"
END_SCRIPT2
END_SCRIPT1
In the outer here-document, we need to escape the $
in "$0"
as "$0"
to not have it expanded by the script writing the document.
To be able to write the inner here-document, we need to make sure that the outer here-document is writing "$0"
. The backslash needs to be escaped as \
, which turns the string into "\$0"
.
The script above writes script1.sh
as
#!/bin/sh
echo 'This is script2 (hello)'
printf '$0 is "%s"n' "$0"
END_SCRIPT2
printf '$0 is "%s"n' "$0"
Running this outputs
This is script1 (hello)
$0 is "script1.sh"
... and script2.sh
is created:
#!/bin/sh
echo 'This is script2 (hello)'
printf '$0 is "%s"n' "$0"
Notice that the inner here-document is created using <<END_SCRIPT2
, not <<-END_SCRIPT2
. Any leading tabs will be eaten up when the outer here-document is created (as evident when looking at script1.sh
).
As an aside, you don't need the two calls to wait
in your code since you don't seem to run any background processes.
Your awk
code is also faulty as the f && /testphrase2/
condition would never be true. If /testphrase/
matches, then next
is executed, forcing the code to continue with the next line of input. If /testphrase/
does not match, then /testphrase2/
won't match either.
Your could try this instead:
awk '!f && /testphrase/ f = 1; r = ""
f && /testphrase2/ f = 0
f r = (r == "" ? $0 : r RS $0)
END print r '
add a comment |Â
up vote
0
down vote
Consider the following example script (all indentations are literal tabs):
#!/bin/sh
var='hello'
cat >script1.sh <<-END_SCRIPT1
#!/bin/sh
echo 'This is script1 ($var)'
printf '$0 is "%s"n' "$0"
cat >script2.sh <<END_SCRIPT2
#!/bin/sh
echo 'This is script2 ($var)'
printf '\$0 is "%s"n' "\$0"
END_SCRIPT2
END_SCRIPT1
In the outer here-document, we need to escape the $
in "$0"
as "$0"
to not have it expanded by the script writing the document.
To be able to write the inner here-document, we need to make sure that the outer here-document is writing "$0"
. The backslash needs to be escaped as \
, which turns the string into "\$0"
.
The script above writes script1.sh
as
#!/bin/sh
echo 'This is script2 (hello)'
printf '$0 is "%s"n' "$0"
END_SCRIPT2
printf '$0 is "%s"n' "$0"
Running this outputs
This is script1 (hello)
$0 is "script1.sh"
... and script2.sh
is created:
#!/bin/sh
echo 'This is script2 (hello)'
printf '$0 is "%s"n' "$0"
Notice that the inner here-document is created using <<END_SCRIPT2
, not <<-END_SCRIPT2
. Any leading tabs will be eaten up when the outer here-document is created (as evident when looking at script1.sh
).
As an aside, you don't need the two calls to wait
in your code since you don't seem to run any background processes.
Your awk
code is also faulty as the f && /testphrase2/
condition would never be true. If /testphrase/
matches, then next
is executed, forcing the code to continue with the next line of input. If /testphrase/
does not match, then /testphrase2/
won't match either.
Your could try this instead:
awk '!f && /testphrase/ f = 1; r = ""
f && /testphrase2/ f = 0
f r = (r == "" ? $0 : r RS $0)
END print r '
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Consider the following example script (all indentations are literal tabs):
#!/bin/sh
var='hello'
cat >script1.sh <<-END_SCRIPT1
#!/bin/sh
echo 'This is script1 ($var)'
printf '$0 is "%s"n' "$0"
cat >script2.sh <<END_SCRIPT2
#!/bin/sh
echo 'This is script2 ($var)'
printf '\$0 is "%s"n' "\$0"
END_SCRIPT2
END_SCRIPT1
In the outer here-document, we need to escape the $
in "$0"
as "$0"
to not have it expanded by the script writing the document.
To be able to write the inner here-document, we need to make sure that the outer here-document is writing "$0"
. The backslash needs to be escaped as \
, which turns the string into "\$0"
.
The script above writes script1.sh
as
#!/bin/sh
echo 'This is script2 (hello)'
printf '$0 is "%s"n' "$0"
END_SCRIPT2
printf '$0 is "%s"n' "$0"
Running this outputs
This is script1 (hello)
$0 is "script1.sh"
... and script2.sh
is created:
#!/bin/sh
echo 'This is script2 (hello)'
printf '$0 is "%s"n' "$0"
Notice that the inner here-document is created using <<END_SCRIPT2
, not <<-END_SCRIPT2
. Any leading tabs will be eaten up when the outer here-document is created (as evident when looking at script1.sh
).
As an aside, you don't need the two calls to wait
in your code since you don't seem to run any background processes.
Your awk
code is also faulty as the f && /testphrase2/
condition would never be true. If /testphrase/
matches, then next
is executed, forcing the code to continue with the next line of input. If /testphrase/
does not match, then /testphrase2/
won't match either.
Your could try this instead:
awk '!f && /testphrase/ f = 1; r = ""
f && /testphrase2/ f = 0
f r = (r == "" ? $0 : r RS $0)
END print r '
Consider the following example script (all indentations are literal tabs):
#!/bin/sh
var='hello'
cat >script1.sh <<-END_SCRIPT1
#!/bin/sh
echo 'This is script1 ($var)'
printf '$0 is "%s"n' "$0"
cat >script2.sh <<END_SCRIPT2
#!/bin/sh
echo 'This is script2 ($var)'
printf '\$0 is "%s"n' "\$0"
END_SCRIPT2
END_SCRIPT1
In the outer here-document, we need to escape the $
in "$0"
as "$0"
to not have it expanded by the script writing the document.
To be able to write the inner here-document, we need to make sure that the outer here-document is writing "$0"
. The backslash needs to be escaped as \
, which turns the string into "\$0"
.
The script above writes script1.sh
as
#!/bin/sh
echo 'This is script2 (hello)'
printf '$0 is "%s"n' "$0"
END_SCRIPT2
printf '$0 is "%s"n' "$0"
Running this outputs
This is script1 (hello)
$0 is "script1.sh"
... and script2.sh
is created:
#!/bin/sh
echo 'This is script2 (hello)'
printf '$0 is "%s"n' "$0"
Notice that the inner here-document is created using <<END_SCRIPT2
, not <<-END_SCRIPT2
. Any leading tabs will be eaten up when the outer here-document is created (as evident when looking at script1.sh
).
As an aside, you don't need the two calls to wait
in your code since you don't seem to run any background processes.
Your awk
code is also faulty as the f && /testphrase2/
condition would never be true. If /testphrase/
matches, then next
is executed, forcing the code to continue with the next line of input. If /testphrase/
does not match, then /testphrase2/
won't match either.
Your could try this instead:
awk '!f && /testphrase/ f = 1; r = ""
f && /testphrase2/ f = 0
f r = (r == "" ? $0 : r RS $0)
END print r '
edited May 5 at 22:19
answered May 5 at 22:02
Kusalananda
105k14209326
105k14209326
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f397068%2fpreventing-parameter-expansion-multiple-times%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
It looks as if
EOF4
is the ending of an internal here-document, but I can't see where that here-document starts...â Kusalananda
Feb 10 at 8:06