Sponsored Content
Top Forums Shell Programming and Scripting Help with simple program. (beginner) Post 302513964 by Corona688 on Thursday 14th of April 2011 12:04:35 PM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
UPSRW(8)							    NUT Manual								  UPSRW(8)

NAME
upsrw - UPS variable administration tool SYNOPSIS
upsrw ups upsrw -h upsrw -s variable [-u username] [-p password] ups DESCRIPTION
upsrw allows you to view and change the read/write variables inside your UPS. It sends commands via the server upsd(8) to your driver, which configures the hardware for you. The list of variables that allow you to change their values is based on the capabilities of your UPS equipment. Not all models support this feature. Typically, cheaper hardware does not support any of them. Run upsrw with a UPS identifier to see what will work for you. OPTIONS
-h Display the help message. -s variable Specify the variable to be changed inside the UPS. For unattended mode such as in shell scripts, use the format VAR=VALUE to specify both the variable and the value, for example: -s input.transfer.high=129 Without this argument, upsrw will just display the list of the variables and their possible values. Some variables are strings, and can be set to any value within the length limit. Others are enumerated types and can only be set to one of those values. Others may be within an allowed range of values. Refer to the list to know what's available in your hardware. -u username Set the NUT username for the connection to the server. This is optional, and you will be prompted for this when using the -s option if you don't specify -u on the command line. NUT usernames are defined in upsd.users(5), and are not linked to system usernames. -p password Set the password to authenticate to the server. This is also optional like -u, and you will be prompted for it if necessary. ups View or change the settings on this UPS. The format for this option is upsname[@hostname[:port]]. The default hostname is "localhost". UNATTENDED MODE
If you run this program inside a shell script or similar to set variables, you will need to specify all of the information on the command line. This means using -s VAR=VALUE, -u and -p. Otherwise it will put up a prompt and your program will hang. This is not necessary when displaying the list, as the username and password are not required for read-only mode. DIAGNOSTICS
upsrw can't set variables on your UPS unless you provide a valid username and password. If you get "access denied" errors, make sure that your upsd.users(5) has an entry for you, and that the username you are using has permissions to SET variables. HISTORY
This program used to be called upsct2, which was ambiguous and confusing. SEE ALSO
upsd(8), upscmd(8) Internet resources: The NUT (Network UPS Tools) home page: http://www.networkupstools.org/ Network UPS Tools 05/24/2012 UPSRW(8)
All times are GMT -4. The time now is 06:28 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy