Help with simple IF statements


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with simple IF statements
# 1  
Old 10-01-2010
Help with simple IF statements

Hi im new to unix shell scripting but not new to codeing itself.

Anyways im making a auto .conf for an eggdropbot and need an If statment.

Code:
read -p 'Bot Nickname:' ecnick
echo Your Bots Nick Is $ecnick

What i want is for if nothing is entered it will do "read -p 'Bot Nickname:' ecnick" again untill the users enters a nickname and if a nickname is entered it will do "echo Your Bots Nick Is $ecnick".

Thanks
Gemster

Moderator's Comments:
Mod Comment Use code tags when posting code, data or logs etc. ty.
# 2  
Old 10-01-2010
Code:
#!/bin/bash
c=0
while :;
 do
  read -p 'Bot Nickname:' ecnick
  if [ -z "$ecnick" ] ; then
    echo Your Bots Nick Is --\"Empty\"-- $ecnick $c tries
  else
    echo Your Bots Nick Is $ecnick
    break
  fi
  (( c++))
 done

# 3  
Old 10-01-2010
Thanks ygemici, it works but ive had to change the coding a little and your script dont work now :/

Code:
read -p 'Bot Nickname:' ecnick
read -p 'Bot Username:' ecusername
read -p 'Bot realname:' ecrealname

So basically if the Nickname is not entered it wall ask for it again and again untill its entered then it will go the the username and so on.

Also I dont need the tries part of it Smilie

Thanks
Gemster
# 4  
Old 10-01-2010
Quote:
Originally Posted by Gemster
Thanks ygemici, it works but ive had to change the coding a little and your script dont work now :/

Code:
read -p 'Bot Nickname:' ecnick
read -p 'Bot Username:' ecusername
read -p 'Bot realname:' ecrealname

So basically if the Nickname is not entered it wall ask for it again and again untill its entered then it will go the the username and so on.

Also I dont need the tries part of it Smilie

Thanks
Gemster
"tries " is a little sample from me Smilie

lets try this Smilie

Code:
#!/bin/bash
while :;
 do
  read -p 'Bot Nickname:' ecnick
  if [ -z "$ecnick" ] ; then
    read -p 'Bot Nickname:' ecnick
  else
    echo Your Bots Nick Is $ecnick
    read -p 'Bot Username:' ecusername
    read -p 'Bot realname:' ecrealname
    break
  fi
 done

# 5  
Old 10-01-2010
Ok havent tested it yet,

But from looking at it the first part is right for the Nickname but dont need the "echo Your Bots Nick Is $ecnick" and i need the Username and Realname to do the same as the Nickname ie: if they are not entered it will ask then the question again.

Thanks
Gemster
# 6  
Old 10-01-2010
Sorry for later reply (but i am sleeping methinks Smilie )

Code:
#!/bin/bash
zero () {
if [ -z $1 ] ; then
 if [ $2 == Nickname ] ; then
  read -p "Bot "$2":" ecnick
  zero "$ecnick" "Nickname"
 fi
  if [ $2 == "Username" ] ; then
   read -p "Bot "$2":" ecuname
   zero "$ecuname" "Username"
  fi
   if [ $2 == "Realname" ] ; then
    read -p "Bot "$2":" ecrname
  zero "$ecrname" "RealName"
  fi
   elif [ -n "$1" ] ; then
echo "Bot $2: $1"
fi
}
   read -p "Bot Nickname:" ecnick
   zero "$ecnick" "Nickname"
   read -p "Bot Username:" ecuname
   zero "$ecuname" "Username"
   read -p "Bot Realname:" ecrname
   zero "$ecrname" "Realname"

This User Gave Thanks to ygemici For This Post:
# 7  
Old 10-01-2010
Wow seems like i need to rethink learning unix scripting lol,

Thought is be as simple as like if nickname == $null or something like that or maybe $null dont exist in unix scripting idk,

Anyways thanks ygemici ill try it in the morning as im heading off to bed lol.

Thanks
Gemster
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Syslog.conf: looking for a simple answer on a simple question

Cheers! In /etc/syslog.conf, if an error type is not specified, is it logged anywhere (most preferable is it logged to /var/log/messages) or not? To be more precise I am interested in error and critical level messages. At default these errors are not specified in syslog.conf, and I need to... (6 Replies)
Discussion started by: dr1zzt3r
6 Replies

2. Shell Programming and Scripting

Help making simple perl or bash script to create a simple matrix

Hello all! This is my first post and I'm very new to programming. I would like help creating a simple perl or bash script that I will be using in my work as a junior bioinformatician. Essentially, I would like to take a tab-delimted or .csv text with 3 columns and write them to a "3D" matrix: ... (16 Replies)
Discussion started by: torchij
16 Replies

3. UNIX for Dummies Questions & Answers

if and then statements

I came across a bash script that outputs the forecast for the day and the max temperature but at the end of the day the max temperature disappears ($6) and I am left with "°C" after the forecast. Here is the script: #! /bin/bash curl -s --connect-timeout 30... (7 Replies)
Discussion started by: _light_
7 Replies

4. Homework & Coursework Questions

Using While and If statements

1. The problem statement, all variables and given/known data: Two problems I need solving please. I created a script where the user types in 7 numbers as standard input and each one is then stored in an array. Now I need to perform the following calculations on those numbers: 1) Use a while... (11 Replies)
Discussion started by: jjb1989
11 Replies

5. Shell Programming and Scripting

vi and if statements

Hi I am very new to Unix programming and shell scripting. I am trying t figure out how to write a little script that will output the number of directories. I can find the number of directories using ls -l | grep "^d" | wc -l I can not figure out how to do it so when I type the name... (8 Replies)
Discussion started by: Reddoug
8 Replies

6. Shell Programming and Scripting

If statements....

Good morning all! I want to know if Im interpreting this if statement below right. if(((1) || (0)) && (1)){ do stuff; } This is saying: if true piped into false, then true, then do stuff. Right? What does the && stand for? thanks in advance! ben (3 Replies)
Discussion started by: bigben1220
3 Replies

7. UNIX for Dummies Questions & Answers

Help with For Statements

Hi, I am trying to write a for statement that will allow for the ps, who, finger, and date commands to run. Can anyone help? I use Putty. (22 Replies)
Discussion started by: lexydoll87
22 Replies

8. Shell Programming and Scripting

HELP!! if statements

I am kind of new in Unix and i have to make a menu. I want to put an if statement in the menu. you should enter the filename and it goes to that file. How do i do this? (1 Reply)
Discussion started by: trob
1 Replies

9. Shell Programming and Scripting

Please help on IF statements.

I had different problem scenarios with IF statement. Can any expert please enlighten me on the difference with these scenarios. Thank you. 1st Scenario: testdate=`date +%Y%m` test=`cat /var/log/database0.$testdate*.log | grep "Errors found during processing" | tail -10` if then ... (4 Replies)
Discussion started by: filthymonk
4 Replies

10. Shell Programming and Scripting

or statements?

how do i do an or in an if-then statement? i tried: if ; then bleh fi how???? (1 Reply)
Discussion started by: Blip
1 Replies
Login or Register to Ask a Question