brand new user!.. Lost on BASH script writing

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:
    I have just gotten into writing bash scripts for a class, part of the assignment is to read and be able to tell what a script does by looking at it, unfortunate for me this is an online class, no real instructor support, so I have no idea what I'm looking at

  2. Relevant commands, code, scripts, algorithms:

#!/bin/bash
if false then
echo "1"
elif true

then
echo "2"
else
echo "3"
fi
  1. The attempts at a solution (include all code and scripts):
    Brand new user, trying to decipher what this script is supposed to do?

  2. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Tarrant County Community College, North Richland Hills, TX, USA, Steve Smiley (professor), Course # ITSC 1407
Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Nothing -- it's syntactically invalid. Here's how you fix it:

#!/bin/bash
# false is a program that always returns a nonzero value.
# if considers nonzero as fail, so this 
# You can't just put 'then' straight after, either a newline or ; is needed between
if false
then
         echo "1"
# true on the other hand always returns 0, so it does 'echo 2'.
elif true
then
       echo "2"
else
# it never runs 'echo 3', under any circumstances.
        echo "3"
fi
1 Like

thank you very much, I'm searching the forums for a beginners guide, so I have a cheat sheet for syntax, i.e. when to put [ xyz ] and what (( xyz )) means so I won't have to bug folks too terribly much

---------- Post updated at 08:21 PM ---------- Previous update was at 08:05 PM ----------
Another one I can't decipher..

#!/bin/bash
for NAME in /etc/*
do
if [ r "$NAME" af "$NAME" ] then
wc c "$NAME"
fi
done

That's also not valid syntax, I'm not sure what it's supposed to do either.

I'll tackle it in parts instead.

for NAME in /etc/*
do
        echo $NAME
done

This would print every folder or file found directly inside /etc one by one, because NAME would be set to it before it gets to 'echo' every loop.

if [ r "$NAME" af "$NAME" ]

I think this was supposed to test if NAME was a file and whether you can read it.

Much simpler all-in-one-line thing:

[ -f "$NAME" ] && [ -r "$NAME" ] && wc -c "$NAME"

To see how && and || work, try true && echo asdf and false && echo asdf and true || echo asdf and false || echo asdf .

To see what wc -c does, see man wc .

Also see the advanced bash scripting guide and test operators specifically. You're going to really like the reference cards.

1 Like

you're a life saver, these were scripts given to us, and we were supposed to translate and tell what they do.. I sat for 2 hours and I think i reached the end of google!.. thank you!