LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   getting bash code to print from within bash (https://www.linuxquestions.org/questions/linux-software-2/getting-bash-code-to-print-from-within-bash-4175606225/)

VolumetricSteve 05-18-2017 01:38 PM

getting bash code to print from within bash
 
I'm trying to use bash instead of flash cards for my linux+. I have some testing materials I'm trying to write into a bash script that will ask you a question from the test. It works on almost every question, except a few that explicitly use bash commands within the question, for example:

Code:

What word will complete an if statement in bash such as the following: if [ -x "$file" ]; then echo
$file _____
(Please provide the missing word only)

If I isolate this, I can get it to print with echo:

Code:

#!/bin/bash
echo "What word will complete an if statement in bash such as the following: if [ -x "$file" ]; then echo"
echo "$file _____"
echo "(Please provide the missing word only)"

I copied and pasted those same lines into the case-statement within my testing script, and those same lines don't work anymore!

Code:

./LX0-104_studyguide.sh: line 38: syntax error near unexpected token `then'
./LX0-104_studyguide.sh: line 38: `echo "What word will complete an if statement in bash such as the following: if [-x "$file"]; then echo"'

It looks like it's trying to execute the code within the quotes, but what I don't get is the inconsistent behavior. Why do the same lines work on their own but not within a case-statement?

More importantly, how can I get bash to blindly print code without trying to interpret it?

Thank you.

redfox2807 05-18-2017 03:13 PM

Try using the single quotes, not the double ones.

BW-userx 05-18-2017 03:27 PM

it is showing you what it wrong
Code:

if [ -x "$file" ]; then echo" <--- incomplete echo statement.
it is trying to execute your if statement then it hits that incomplete echo command that puts a stop to everything.

Plus the quote looks like it is hitting the echo command too.

Not exactly knowing what code you want printed out but these examples should get you an idea of how to, then go from there.
Code:


echo " some text "

if [[ -x "$file" ]] ; then
{
echo
echo "$file _____"
echo "(Please provide the missing word only)"
}
fi

Quote:

More importantly, how can I get bash to blindly print code without trying to interpret it?
just add quote marks on both ends of code.
Code:

echo "mv -v /usr/file /usr/file3"
inside of your script.
example:
Code:

userx%slackwhere ⚡ ~ ⚡> file="pppp"                                                         
userx%slackwhere ⚡ ~ ⚡> echo "mv -v $file myfile"                                           
mv -v pppp myfile
userx%slackwhere ⚡ ~ ⚡> echo "mv -v \$file myfile"                                           
mv -v $file myfile
userx%slackwhere ⚡ ~

look closely at the differences. what using an escape does \

example 2
Code:

userx%slackwhere ⚡ ~ ⚡> echo "if [[ -x \$file ]] ; then
]> {
]> echo "\"using echo to print code"\"                                                       
]> echo " \$file____"
]> }
]> fi"
#results all of that gets echo'ed out to the screen like this.
if [[ -x $file ]] ; then
{
echo "using echo to print code"
echo  $file____
}
fi
userx%slackwhere ⚡ ~ ⚡>


Shadow_7 05-19-2017 09:40 AM

The bash interpreter strips the "(double quote)'s so you need to escape them or bypass interpretation with '(single quote)'s.

$ echo the thing
the thing

$ echo "the thing"
the thing

$ echo "\"the thing\""
"the thing"

$ echo '"the thing"'
"the thing"

This applies for other "special characters" as well

$ echo $(( 2 + 2 ))
4
$ echo "$(( 2 + 2 ))"
4
$ echo "\$(( 2 + 2 ))"
$(( 2 + 2 ))
$ echo '$(( 2 + 2 ))'
$(( 2 + 2 ))

HTH

sweepnine 06-15-2017 07:07 AM

Quote:

Originally Posted by VolumetricSteve (Post 5712475)
More importantly, how can I get bash to blindly print code without trying to interpret it?

You can use the "here document":
Code:

cat <<EOF
if [[ -a /tilt ]]; then ohno; fi
ls /
echo bla
EOF


OstermanA 06-17-2017 01:50 AM

A super powerful but under used tool is putting a $ at the start of single quotes:
Code:

$ echo 'the\nthing'
the\nthing
$ echo $'the\nthing'
the
thing
$ echo $'the\n$thing'
the
$thing

$'' will parse escaped characters, but everything else is treated as a literal. In many cases, it's the only good solution to some text mangling that needs doing.


All times are GMT -5. The time now is 09:15 PM.