bash functions returns "command not found"

1.2k views Asked by At

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_
2

There are 2 answers

6
rurouni88 On BEST ANSWER

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

0
djvh On

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