English
Ask Your Question
2

help with shell script for school

asked 2017-10-11 01:27:54 +0000

crazycat0131 gravatar image

updated 2017-10-11 03:38:22 +0000

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
  echo “*** Accounts added:”
  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 flag offensive close merge delete

5 answers

Sort by » oldest newest most voted
2

answered 2017-10-11 06:03:05 +0000

villykruse gravatar image

updated 2017-10-11 06:04:34 +0000

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.

edit flag offensive delete link more
1

answered 2017-10-11 06:52:55 +0000

updated 2017-10-11 07:06:09 +0000

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
      echo “*** Accounts added:”
      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 flag offensive delete link more

Comments

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.

sideburns ( 2017-10-11 07:15:59 +0000 )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" ...

davidva ( 2017-10-11 07:54:39 +0000 )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.

sideburns ( 2017-10-11 08:16:33 +0000 )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.

davidva ( 2017-10-11 08:36:51 +0000 )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.

villykruse ( 2017-10-11 08:57:24 +0000 )edit
1

answered 2017-10-11 02:39:34 +0000

sideburns gravatar image

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.

edit flag offensive delete link more

Comments

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

crazycat0131 ( 2017-10-11 03:33:07 +0000 )edit
0

answered 2017-10-11 20:53:29 +0000

crazycat0131 gravatar image

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.

edit flag offensive delete link more
0

answered 2017-10-11 20:51:34 +0000

crazycat0131 gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

[hide preview]

Use your votes!

  • Use the 30 daily voting points that you get!
  • Up-vote well framed questions that provide enough information to enable people provide answers.
  • Thank your helpers by up-voting their comments and answers. If a question you asked has been answered, accept the best answer by clicking on the checkbox on the left side of the answer.
  • Down-voting might cost you karma, but you should consider doing so for incorrect or clearly detrimental questions and answers.

Question Tools

Follow
1 follower

Stats

Asked: 2017-10-11 01:27:54 +0000

Seen: 55 times

Last updated: Oct 11