# help with shell script for school

Sorry I am very new and this and do not know what I am doing from I am sure the -f should be a different command but since is really my first shell script I am writing just as the professor has requested.

The error I am getting is ./acctchk.sh: line 7: [-f: command not found

Here is the full script copied from the instructions. (any suggestions are welcome I am working in a VM with Fedora 26)

#!/bin/sh
#  acctchk.sh
#  This script compares the current /etc/passwd file against an archived copy called /root/passed.ar.  #  It produces a list of added or deleted accounts.  It creates an archived file from /etc/passwd
#  called /root/passwd.ar if none exists.
cd  /root
if [ -f  /root/passwd.ar ]
then
echo “Today is date”       # print today’s date
sort </etc/passwd > ./tmp1     # sort the contents of the /etc/passwd file into tmp1
sort </root/passwd.ar> ./tmp2  # sort the contents of the /root/passwd.ar file into tmp2
comm -23 ./tmp1 ./tmp2    # output any lines that are in tmp1 but not in tmp2 (added accounts)
echo “*** Accounts deleted:”
comm -13 ./tmp1 ./tmp2  # output any lines that are in tmp2 but not in tmp1 (deleted accounts)
echo “”
rm –f ./tmp1 ./tmp2  # remove temporary files
else
echo “** Creating archived /etc/passwd file ** “
cp /etc/passwd  /root/passwd.ar  #  create the archive if it doesn’t already exist
fi

edit retag close merge delete

Sort by » oldest newest most voted

In the error message you quoted you have

./acctchk.sh: line 7: [-f: command not found


I notice a missing space between [ and -f. That space is essential as it separates the command [ from its first argument -f. The [ command, by the way, is the same as the test command.

In your script, however, I see

if [ -f  /root/passwd.ar ]


This could also be written

if test -f /root/passwd.ar


It it will do exactly the same thing.

which looks correct.

more

Minors changes, '; then' other 'sh to bash' I share you a great guide about conditionals (if statements).

  #!/bin/bash
#  acctchk.sh
#  This script compares the current /etc/passwd file against an archived copy called /root/passed.ar.  #  It produces a list of added or deleted accounts.  It creates an archived file from /etc/passwd
#  called /root/passwd.ar if none exists.
cd  /root
if [ -f  /root/passwd.ar ]; then
echo “Today is date”       # print today’s date
sort </etc/passwd > ./tmp1     # sort the contents of the /etc/passwd file into tmp1
sort </root/passwd.ar> ./tmp2  # sort the contents of the /root/passwd.ar file into tmp2
comm -23 ./tmp1 ./tmp2    # output any lines that are in tmp1 but not in tmp2 (added accounts)
echo “*** Accounts deleted:”
comm -13 ./tmp1 ./tmp2  # output any lines that are in tmp2 but not in tmp1 (deleted accounts)
echo “”
rm –f ./tmp1 ./tmp2  # remove temporary files
else
echo “** Creating archived /etc/passwd file ** “
cp /etc/passwd  /root/passwd.ar  #  create the archive if it doesn’t already exist
fi

more

It's common for scripts to use sh if they're going to be used by anybody but the author, as it's the only shell you can be sure that everybody has. Yes, almost all Linux distros use bash, but using sh makes it as portable as possible. Presumably, that's what the poster's instructor wanted the class to use.

( 2017-10-11 02:15:59 -0600 )edit

@sideburns But if the boy uses 'sh' or 'bash', in Fedora doesn't matter, because is symbolic link to "bash"

file -h /bin/sh


But other distributions changes and not everything is interpreted correctly. Likewise this with "sh" or "bash" should run without problem, because it is a simple script. You have a more advanced script running in other distributions; you will see the differences between "bash" and "sh" ...

( 2017-10-11 02:54:39 -0600 )edit

And what happens if somebody's using a distro that doesn't use bash? The whole point of using sh is for maximum portability, and that's my point. Just because sh is a symlink to bash in Fedora doesn't mean that the whole world does it that way, so it's best to avoid anything that might be distro-specific.

( 2017-10-11 03:16:33 -0600 )edit

All modern distro uses bash... but using "sh" the thing change; a simple example; Ubuntu its sh is a symbolic link to 'dash'

Busybox, which is usually run during the Linux system boot time as part of initramfs. It uses the ash shell implementation.

BSDs, and in general any non-Linux systems. OpenBSD uses pdksh, a descendant of the Korn shell. FreeBSD's sh is a descendant of the original UNIX Bourne shell. Solaris has its own sh which for a long time was not POSIX-compliant; a free implementation is available from the Heirloom project.

( 2017-10-11 03:36:51 -0600 )edit
1

In that guide you see

if [ “$stringvar” == “tux” ]; then  This is a bash-ism and not acceptable by dash or posix sh. Proper is if [ “$stringvar” = “tux” ]; then


Also

if [ $foo -ge 3 -a$foo -lt 10 ]; then


is better written as

if [ $foo -ge 3 ] && [$foo -lt 10 ]; then


as the former is not posix.

Check out the checkbashisms command provided by the devscripts-checkbashisms package.

( 2017-10-11 03:57:24 -0600 )edit

Welcome to ask.fedora! It's rather hard to follow your script, because all of the lines are run together. Please get rid of the oversized bold at the beginning, and surround all of your script with Code tags. (The button for that is the one with the little ones and zeros.) However, one major error stands out. Your first line should look like this:

#!/bin/sh


instead of what you have. As it is, I'd be surprised if it runs at all, even if you've remembered to make it executable by changing the permissions. And, while you're at it, put a # at the beginning of every line that's intended to be a comment, to make sure that the shell doesn't try to execute them. Feel free to come back and ask more questions as needed, either about this script or about other Fedora related issues that you may have. Unlike some Linux forums, we don't mind helping out students.

more

the copy and paste must have eliminated the # in the beginning, hopefully it is easier to read not. This is straight from the instructions.

( 2017-10-10 22:33:07 -0600 )edit

Thanks all the space worked, I cant believe I missed it. I appreciate the extra eyes.

The lesion is using shells, I will have to try the bash one after I get done the assignment just to see if I can get the same results.

more

Thanks all the space worked, I cant believe I missed it. I appreciate the extra eyes.

The lesion is using shells, I will have to try the bash one after I get done the assignment just to see if I can get the sam results.

more