Shell Scripting awk Problem

I'm limited in my skills related to *nix and I'm even more limited in my skills related to shell scripting and the use of awk. I have what should be a relatively simple shell script that looks in the /etc/fstab to see if a particular partition exists on the Linux system. The intention of this part of the script is to determine whether a /tmp partition exists on the system. If such a partition does exist, then no output would be returned. If such a partition does not exist, then three lines of text would be echoed to the screen and the script would terminate right there. So, I need to test both a positive (so that the lines of text get echoed to the screen, and a negative (so that nothing gets returned to the screen). In order to test a positive, I have my command looking for a /temp entry in /etc/fstab instead of a /tmp entry (since the system I'm testing does have a /tmp partition). Below is my code. I get no errors when I run it but I'm never able to get a positive, so I know my code is not working. Can someone tell me what is wrong with my code. Please, I do not won't suggestions about other ways to try to get the same information. I just want to know what to correct in my code to get it to work the way I need it to work.

#!/bin/bash
/bin/awk if ( grep "[[:space:]]/temp[[:space:]]" /etc/fstab )= "" then
    echo "The RHEL security benchmarks require that there be a /tmp file partition."
    echo "Since this system doesn't have one, you will need to rebuild the system,"
    echo "creating a /tmp partition.
    break
fi

What is the 'awk' for? That doesn't look like awk code. (awk is its own programming language.)

You do not need to check if grep's output is blank to see if it found anything. It returns a success or failure code like any other utility. You can plug it into if directly, and invert with ! if needed.

'break' only makes sense in a loop. If you want to exit, 'exit' is what you want, 'exit 1' to exit with error particularly.

Your code looks for /temp, but your warning demands /tmp. Assuming /tmp is what you really want.

You need a return, or ; , between your if statement and the then.

Warning messages should go to standard error.

if ! grep -q "/tmp" /etc/fstab
then
    echo "The RHEL security benchmarks require that there be a /tmp file partition." >&2
    echo "Since this system doesn't have one, you will need to rebuild the system," >&2
    echo "creating a /tmp partition." >&2
    exit 1
fi

Thanks a bunch, Corona688. I also found one other problem (which you wouldn't have known about because I didn't think to tell you). I created that script on a Windows box using Notepad and the ftp'ed it onto the Linux box. I didn't know that Windows inserts an M (its carriage return character) at the end of each line and the shell can't interpret that M. I had to run the dos2unix utility against the file. Then it ran properly. Thanks again.

1 Like