4

I've been trying to get this function to work without returning errors, but so far I'm unable to figure out what the problems is. I'm using $(report_home_space) to insert the contents of the functions on a small bit of hmtl but keep getting the error: report_home_space: command not found on line 30.

 report_home_space () {
          cat <<- _EOF_
                  <H2>Home Space Utilization</H2>
                  <PRE>$(du -sh /home/*)</PRE>
                  _EOF_
  }

I'm new to shell scripting, but I can't not find anything wrong with the syntax of the function, and the spelling seems correct. Thanks in advance.

Full script is:

#!/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_uptime () {
        cat <<- _EOF_
                <H2>system Uptime</H2>
                <PRE>$(uptime)</PRE>
                _EOF_
}

report_disk_space () {
        cat <<- _EOF_
                <H2>Disk Space Ulitilizatoin</H2>
                <PRE>$(df -h)</PRE>
                _EOF_
}

report_home_space () {
        cat <<- _EOF_
                <H2>Home Space Utilization</H2>
                <PRE>$(du -sh /home/*)</PRE>
                _EOF_
}

cat << _EOF_
<HTML>
        <HEAD>
                <TITLE>$TITLE</TITLE>
        <BODY>
                <H1>$TITLE</H1>
                <P>$TIMESTAMP</P>
                $(report_uptime)
                $(report_disk_space)
                $(report_home_space)
        </BODY>
<HTML>
_EOF_
4
  • 1
    A few notes: (1) your whitespace is very unusual (ie. spaces after the function name between the parens); may not be invalid, but certainly not common idiom. (2) Using <<- is risky, as it's easy for tabs to be converted to spaces during copy/paste; safer to use << and not indent the closing delimiter, ugly as that is. Commented Jun 9, 2015 at 3:37
  • Also, it would be more efficient to leave out the cat invocations (which all involve a fork()/exec() cycle, after all). Consider using printf '%s\n' "<H2>System Uptime</H2>" "<PRE>$(uptime)</PRE>" or the like. Commented Jun 9, 2015 at 3:38
  • I was just typing up a printf recommendation but you beat me to it. :) Commented Jun 9, 2015 at 3:39
  • @CharlesDuffy all the suggestions are dully noted. I'm following examples as I see them in the book I'm currently learning from, but I do see the benefit of printf over what I'm currently using. Thanks. Commented Jun 9, 2015 at 5:43

2 Answers 2

4

BTW, your script works fine. Did you by any chance type it up in a Windows environment before uploading to a UNIX env? Try running:

dos2unix script.sh

What this does is change line endings from from Windows to unix format. i.e. it strips \r (CR) from line endings to change them from \r\n (CR+LF) to \n (LF).

Also, on a HTML note, you're missing a closing tag for "< HEAD >" after your title tags.

enter image description here

Sign up to request clarification or add additional context in comments.

6 Comments

Great suggestion, though I'm not sure it would result in the error that the OP mentioned. From what I can see of the script, the text report_home_space never actually appears at the end of the line, so the only places where rogue \r characters could foul things up would be within the function (which would still run), or within the HTML (where it doesn't matter). Of course, I haven't actually tested. :-)
It would also be worth noting what options you use in vim to get an explicit view of what's inside the file. A nod to set list in traditional vi might be good as well, in case vim is unavailable on the OP's system.
@rurouni88-I'm working on my Ubuntu machine, the script was typed (vim) and ran there. Thanks for the <HEAD/>sup :) I did miss that.
@ghoti- liked the "set list" command. I usually run cat -A on the script to get this view.
It turns out that the culprit was a tab character located at the end of my second function _EOF_^I$. All working now. Thanks!
|
0

You can also do "od -c filename" or "grep pattern filename | od -c" to see if there are any hidden characters in there.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.