Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hopefully this is not a silly question. I am just repeating an example from the book <The Linux Command Line> Fourth Internet Edition by William Shotts. Taking the example on page 423, I always come across this problem. Can anyone help to figure it out?
Code:
#!/bin/bash
#Program to output a system information page
TITLE="System Information Report For $HOSTNAME"
CURRENT_TIME="$(date +"%x %r %Z")"
TIMESTAMP="Generated $CURRENT_TIME, by $USER"
report_home_space (){
if [[ "$(id -u)" -eq 0 ]]; then
cat <<- _EOF_
<H2>Home Space Utilization (All Users)</H2>
<PRE>$(du -sh /home/*)</PRE>
_EOF_
else
cat <<- _EOF_
<H2>Home Space Utilization ($USER)</H2>
<PRE>$(du -sh $HOME)</PRE>
_EOF_
fi
return
}
cat << _EOF_
<HTML>
<HEAD>
<TITLE>$TITLE</TITLE>
</HEAD>
<BODY>
<H1>$TITLE</H1>
<P>$TIMESTAMP</P>
$(report_home_space)
</BODY>
</HTML>
_EOF_
The problem is gone, if I shorten the above code as below:
Code:
#!/bin/bash
#Program to output a system information page
TITLE="System Information Report For $HOSTNAME"
CURRENT_TIME="$(date +"%x %r %Z")"
TIMESTAMP="Generated $CURRENT_TIME, by $USER"
report_home_space (){
echo "Function report_home_space executed."
return
}
cat << _EOF_
<HTML>
<HEAD>
<TITLE>$TITLE</TITLE>
</HEAD>
<BODY>
<H1>$TITLE</H1>
<P>$TIMESTAMP</P>
$(report_home_space)
</BODY>
</HTML>
_EOF_
To me, the only difference between the two pieces of codes is: there are two extra pairs of "_EOF_" in the first piece. But I might miss something obvious. Thank you!
I see that is true...did not know that. From man bash -- Here Documents section:
Code:
If the redirection operator is <<-, then all leading tab characters are stripped from input lines and the line containing delimiter. This allows here-documents within shell scripts to be indented in a natural fashion.
...so that should allow the not-left-flush ending _EOF_ to work IF the leading whitespace is tab(s), but apparently not if it is spaces...
That said, I'm pretty sure that there should be no spaces between the << or <<- and the word (in this case the word is _EOF_, but it can be any string). Try removing the space there.
(confirmed...setting the leading spaces to tabs still threw the error. Removing the space after the << and <<- allowed the script to run.)
I see that is true...did not know that. From man bash -- Here Documents section:
Code:
If the redirection operator is <<-, then all leading tab characters are stripped from input lines and the line containing delimiter. This allows here-documents within shell scripts to be indented in a natural fashion.
...so that should allow the not-left-flush ending _EOF_ to work IF the leading whitespace is tab(s), but apparently not if it is spaces...
That said, I'm pretty sure that there should be no spaces between the << or <<- and the word (in this case the word is _EOF_, but it can be any string). Try removing the space there.
(confirmed...setting the leading spaces to tabs still threw the error. Removing the space after the << and <<- allowed the script to run.)
Thank you! You are also right. After I changed spaces as tabs, everything works.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.