LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 05-18-2017, 01:38 PM   #1
VolumetricSteve
Member
 
Registered: Mar 2011
Posts: 90

Rep: Reputation: 7
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.
 
Old 05-18-2017, 03:13 PM   #2
redfox2807
Member
 
Registered: Jul 2012
Distribution: Debian testing/stable, Gentoo, CentOS 7, Sailfish OS, Android
Posts: 167

Rep: Reputation: 31
Try using the single quotes, not the double ones.
 
Old 05-18-2017, 03:27 PM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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 ⚡ ~ ⚡>

Last edited by BW-userx; 05-18-2017 at 04:56 PM.
 
Old 05-19-2017, 09:40 AM   #4
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874
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
 
1 members found this post helpful.
Old 06-15-2017, 07:07 AM   #5
sweepnine
LQ Newbie
 
Registered: Jun 2017
Posts: 16

Rep: Reputation: Disabled
Quote:
Originally Posted by VolumetricSteve View Post
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
 
Old 06-17-2017, 01:50 AM   #6
OstermanA
Member
 
Registered: Dec 2006
Location: Seattle, WA
Distribution: CentOS 8
Posts: 99

Rep: Reputation: 20
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.

Last edited by OstermanA; 06-17-2017 at 01:52 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Scripting – Code Structure - Defining Multiple Points Of Entry In Bash Script carlr Programming 10 08-25-2014 02:38 AM
Which file in bash source code (tarball) contain a print output function Runicer Programming 2 03-20-2012 08:47 AM
[SOLVED] Help fixing bash/awk code to print letters based on counter Perseus Programming 37 10-28-2011 05:56 PM
Bash problem : -bash: [: /bin/bash: unary operator expected J.A.X Linux - Software 1 09-22-2011 05:52 AM
Bash: Print usage statement & exit; otherwise continue using Bash shorthand operators stefanlasiewski Programming 9 02-07-2006 05:20 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration