LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-21-2005, 07:37 PM   #1
noir911
Member
 
Registered: Apr 2004
Posts: 682

Rep: Reputation: Disabled
bash boundary check


I have written a bash script to add user which will take the user name as a command line argument. But I would like to add a boundary check such that if the script is executed without a user name it will exit. Current script:

#!/bin/bash
for name in "$@"
do
echo user name is ${user}
done
echo "hello $user"
adduser $user

How am i going to add the boundary check in my script?
 
Old 06-21-2005, 07:44 PM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,785

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
"user" is not set in your script
 
Old 06-21-2005, 10:15 PM   #3
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Rep: Reputation: 52
What you are looking for is $#. This tells you the # of arguments given. Like so:

if [ $# -eq 0 ]; then
echo "$0: You must supply an argument!!"
exit 1
fi

However, this will accept 1 or more arguments. For this script, you'd want only 1 argument. I'll leave that to you to figure out.

-twantrd
 
Old 06-22-2005, 02:20 AM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,785

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Can you first explain the logic of your script ? It accepts several arguments, display all of them but use only the last one to add a user, assuming the loop is really "for user in ...", is it really what you want ?

Should you actually want to add every user name passed as an argument, here is a simpler way, which need no boundary check:
Code:
#!/bin/bash
for user
do
    echo hello "$user"
    adduser $user    
done
 
Old 06-22-2005, 08:13 AM   #5
frandalla
Member
 
Registered: Oct 2003
Location: Tokyo - Japan
Distribution: Slackware
Posts: 348
Blog Entries: 1

Rep: Reputation: 37
I like twantrd's solution and here is my solution:
Code:
if [ "$1" == "" ]; then
echo "$0: You must supply an argument!!"
exit 1
fi
the only difference is that I'm checking only the first parameter
 
Old 06-22-2005, 10:09 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,785

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Your test is weaker as the script can be passed several arguments and the first one be an empty string.
Twantrd is doing it all right.
 
Old 06-24-2005, 05:09 AM   #7
noir911
Member
 
Registered: Apr 2004
Posts: 682

Original Poster
Rep: Reputation: Disabled
ok solved.

I used

if [-z "$@"]; then
echo "...."
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash-Check If FS is Already Mounted On Directory fortezza Programming 15 04-27-2013 07:06 PM
Bash: Check if a program is running naimslim89 Programming 12 05-08-2012 10:24 PM
Root user check in bash script doesn't work tawalker Programming 6 12-18-2011 03:06 AM
BASH scripting: check for numeric values linuxLuser Programming 11 11-14-2011 10:11 AM
Bash scripting to check text in a website carlp Programming 2 09-20-2005 11:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration