Help with simple program. (beginner)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with simple program. (beginner)
# 1  
Old 04-14-2011
Help with simple program. (beginner)

Hey all,

Writing a program that searches for a username and if they are online creates a 'beep' and sends the username and date to a log file.

the error i am getting is:

Code:
paul.obrien16@aisling:~/os$ bash checklogin
: command not found
Enter username
paul.obrien16
': not a valid identifier `username
checklogin: line 36: syntax error: unexpected end of file
paul.obrien16@aisling:~/os$

Here is my code for refrence:


Code:
#!/bin/bash

# get username
echo "Enter username"
read username
#finds the user in the list of people currently logged in
if [ $ cat /etc/passwd | cut -d: -f1= "$username" ]
then
# loop while user is logged in
while [ $(who | grep $username) != 0 ]
do
sleep 60
done

echo "$username logged in"
#generate beep
echo -e \a
#output to a log file calles date.txt
echo "user is $(username) Today's date is $(date)" > /tmp/date.txt
#exit function
exit 1
else
#display message
echo "$username aint logged in!"
#output to a log file called date.txt
echo "user is $(username) Today's date is $(date)" > /tmp/date.txt
#exit the function
exit 1
#end the script
fi

exit 0


Last edited by sexyladywall; 04-14-2011 at 12:46 PM.. Reason: mistake in code
# 2  
Old 04-14-2011
A good start but there's a few things to figure out here:
Code:
if [ $ cat /etc/passwd | cut -d: -f1= "$username" ]

You don't put programs inside [ ] like that, you can just put them there raw.

That is also a useless use of cat, whenever you have cat stuff | program it's better to do program < stuff.

There's a trick you can do with grep to check for a user in the password file:

Code:
if grep -q "^${username}:" /etc/passwd
then
...

The ^ makes it look at the beginning of the line, so it looks for ${username}: at the beginning of any line in /etc/passwd. The -q tells it not to print anything, just return true or false as usual.

Code:
while [ $(who | grep $username) != 0 ]

This is a useless use of backticks (since the $(stuff) syntax is a special form of backticks). You don't check a return value in that fashion, either -- what that statement would actually end up doing is checking the string output of who | grep $username.

How you check a return value is like:

Code:
# there really are 'true' and 'false' programs, so go ahead and
# try these statements
if true
then
       echo "true returns true"
fi

if ! false
then
        echo "false returns false"
fi

So how about:
Code:
while who | grep -q "${username}"
do
...

Code:
exit 1

1 means error, by returning 1 you're telling whatever ran it that your program failed. Use 'exit 0' to show that your program ran successfully.
# 3  
Old 04-14-2011
Validate your input not blank, is in /etc/passwd, first, before proceeding, if bad then display, exit -- done, no deep nesting, all parts of the function point close together. A cat is not necessary to get files to stdin, < does that cheaper. One user name might be a substring of another, so the grep of who should be more qualified. date can produce the entire message using a "+%Y-%m-%d %H:%M:%S $username" sort of argument, and have a nicer format. $(xxx) is not for diplaying variables, that is ${xxx}, $(xxx) turns a command or script stdout into a string on the command line, just the opposite of echo, which turns a string on the command line into a stream on stdout. The error message does not describe the error, unnown user name.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help/How-to - simple UNIX script / gzip (beginner)

Hey all, I would like to ask your help and sorry in advance for my ignorance since I am a complete beginner in this. I need to create a UNIX script that will: - scan a small number of folders and subfolders (see a similar file tree in the attachment) - - for each final folder (each of... (8 Replies)
Discussion started by: McNulty
8 Replies

2. Programming

A simple C program query ...

Given the following code inside the function ext3_write_super(): (It's there in Linux kernel 2.6.27.59) static void ext3_write_super (struct super_block * sb) { if (mutex_trylock(&sb->s_lock) != 0) BUG(); sb->s_dirt = 0; } The conditional test at if... (2 Replies)
Discussion started by: Praveen_218
2 Replies

3. UNIX for Dummies Questions & Answers

Help with creating a simple program!!

i am new to shell scripting!! i am making this program in bourne shell, that asks the user to input "Hello (their name)" or "question (their name)", any other input, "ERROR" will be outputted. if they input "Hello (name)", i want to out saying Hello (name) but if they input "question (name)", i... (4 Replies)
Discussion started by: bshell_1214
4 Replies

4. Shell Programming and Scripting

very simple but mount everest for beginner

List directories at the home directory in increasing order. First of all is this correct? cd ~/ du -h|sort -n|more First command is to go to the home directory and second does the rest. Is there a way to do them in single command. Thanks in advance. (1 Reply)
Discussion started by: kevincobain2000
1 Replies

5. UNIX for Dummies Questions & Answers

Installing a Program called MAQ (Beginner Here)

Basically, I'm trying to install this program called MAQ. Maq User's Manual I've downloaded it and I tried to type in $ ./configure; make; make install but the ouput I got was this: bash: ./configure: No such file or directory make: Entering directory `/home/vlay/bwa-0.5.0' make:... (8 Replies)
Discussion started by: vlay2
8 Replies

6. Shell Programming and Scripting

simple program help required

The circumfrence of a circle is #!/usr/bin/perl print 2 * 3.141592654 * 12.50 \n"; # pi= 3.141592654 # r= 12.50 I need a simple program showing me all the steps..to modify the above to prompt for and accept a radius from the person running the... (3 Replies)
Discussion started by: Q2wert
3 Replies

7. Programming

Xlib simple program.

I don't know if it is right to ask you this. Can someone help me write a simple Xlib program,with button on it,and all that button do is switch 2 messages. I have tried and tried,but never get past Hello World. Can someone help me please? ---------- Post updated at 10:17 PM ---------- Previous... (2 Replies)
Discussion started by: megane16v
2 Replies

8. UNIX for Dummies Questions & Answers

A beginner for cygwin simple question

the current pwd is: c:\cygwin\home\hui which command can go to c:\documents and setting\hui\my documents\hui\reply.txt (1 Reply)
Discussion started by: zhshqzyc
1 Replies

9. Programming

a simple chat program

any suggestions on how i could create a simple chat program between two terminals using pipes? thanks (1 Reply)
Discussion started by: kelogs1347
1 Replies

10. Programming

QUESTION...simple program?

I am new to the unix/linux environment. AND........ I need to create a mini shell..that displays prompt (i.e., READY:$), accepts a command from std-in, and prints the command and any parameters passed to it. HELP!!!! (8 Replies)
Discussion started by: jj1814
8 Replies
Login or Register to Ask a Question