Bash shell scripting

Ok not really C++ here, but I have this shell script assignment that I can't get to work. Just takes some input and throws it into some variables right now. Anyway, it has to loop back to the beginning using a while loop if there was a mistake. I can't get the condition to work at all. Just spits out errors everytime.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
done=0
while [["$done"!="y"]]
do
    echo "First name: "
    read fname
    echo "Last name: "
    read lname
    echo "Login name: "
    read loginname
    echo "User ID: "
    read userid
    echo "Group ID: "
    read groupid
    echo "Create home dir?[y/n] "
    read homedir

    echo
    echo "Name: $fname $lname"
    echo "Login name: $loginname"
    echo "UID: $userid"
    echo "Group ID: $groupid"
    echo "Want home dir? $homedir"
    echo
    echo "Is this correct?[y/n]"
    read done
done


That while condition has been through every different way to do it that I can find, and I get the same type of message of
[[0!=y]]: not found

every time. No idea what I'm doing and I can't seem to find any documentation on how the condition is supposed to be here for while loops here.
Try
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/bin/bash
done=0

while [[ $done != "y" ]]
do
    echo "First name: "
    read fname
    echo "Last name: "
    read lname
    echo "Login name: "
    read loginname
    echo "User ID: "
    read userid
    echo "Group ID: "
    read groupid
    echo "Create home dir?[y/n] "
    read homedir

    echo
    echo "Name: $fname $lname"
    echo "Login name: $loginname"
    echo "UID: $userid"
    echo "Group ID: $groupid"
    echo "Want home dir? $homedir"
    echo
    echo "Is this correct?[y/n]"
    read done
done

note the space between the brackets.
Last edited on
Wow, I'm not at my machine with linux on it, but does the space really matter?? I saw this nowhere in my searching for answers -_-
I'm not experienced in shell scripting but the examples I found searching all had spaces and here http://tldp.org/LDP/abs/html/loops1.html in example 11-14 it states the space is necessary because they are test brackets.
he removed the quotes around the variable bro and actually space shouldnt matter but I dont see why you have two [] sets
Space does matter, since [[ and ]] are reserved keywords in BASH. [[ is different from single bracket [ in that [ is a system command ( /bin/[ which happens to take a ] as its last arguement ). While [ is almost completely portable, it supports far less in terms of what it can do, you are basically limited to the -eq -ne -lt -le etc type of comparisons, instead of == != < <= etc.

Only the BASH and Korn shell support double brackets.
What do the -eq, etc flags mean? I can assume -eq means equal to, -ne means not equal to, but the other two I don't know and I can't seem to find documentation on it anywhere.

EDIT:
I actually found out why it wasn't working. I was running it as "sh adduser", not "bash adduser" -_-

This is why tutorials suck and documentation is the way to go.

Anywho, my conditional doesn't seem to be checking correctly as if I type in "y" for the done variable, it still continues to loop through.
Last edited on
It worked fine for me.

censored@censored:~/Desktop/progstuff$ bash whileloop.sh 
First name: 
test
Last name: 
mctest
Login name: 
testm
User ID: 
1
Group ID: 
01
Create home dir?[y/n] 
n

Name: test mctest
Login name: testm
UID: 1
Group ID: 01
Want home dir? n

Is this correct?[y/n]
y
censored@censored:~/Desktop/progstuff$


Try encapsulating $done in { brackets like this:
while [[ ${done} != "y" ]]

Also it wouldn't hurt to see what your done variable is actually holding at the end of the loop for test purposes

echo "\$done = \"${done}\""

Edit: Those are the old fashion comparison tags from sh. -lt is less than, -le is less than or equal to.

There is also -gt, -ge for greater than, -z for null and -n for not null.

http://tldp.org/LDP/abs/html/comparison-ops.html
Last edited on
see 'man bash'

also chmod +x /path/to/script

...better yet, use csh instead of bash.
whats csh
I've thought about installing csh to play around with it because it seems fun, but haven't gotten to it yet.
Don't use csh (or rather tcsh) for scripts.
http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/
Topic archived. No new replies allowed.