LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-23-2018, 03:47 PM   #1
bluethundr
Member
 
Registered: Jun 2003
Location: Summit, NJ
Distribution: CentOS 5.4
Posts: 142

Rep: Reputation: 15
Post run lines of code only if user input greater than a number in bash script


In a bash script I want to run some lines of code only if a user input is greater than 1. I'm asking the user to tell us how many AWS servers they want to create.

If the number of servers is greater than one, the user inputs server names into an array. If the number is only 1 then it reads the server name into a variable.

Is this the right test to be performing?

Code:
    printf "Enter Number of Instances: "
    read -r num_instances
    if (( "$num_instances" > 1 )); then
      while [ "$num_instances" ]
       do
         printf "Enter a server name: "
         read -a server_names
         done
       else
          printf "Enter a server name: "
          read -r name_tag
       fi

Last edited by bluethundr; 08-23-2018 at 03:56 PM.
 
Old 08-23-2018, 09:31 PM   #2
berndbausch
Senior Member
 
Registered: Nov 2013
Location: Tokyo
Distribution: Redhat/Centos, Ubuntu, Raspbian, Fedora, Alpine, Cirros, OpenSuse/SLES
Posts: 3,492

Rep: Reputation: 921Reputation: 921Reputation: 921Reputation: 921Reputation: 921Reputation: 921Reputation: 921Reputation: 921
Quote:
Originally Posted by bluethundr View Post
Code:
    printf "Enter Number of Instances: "
    read -r num_instances
    if (( "$num_instances" > 1 )); then
      while [ "$num_instances" ]
       do
         printf "Enter a server name: "
         read -a server_names
       done
    else
          printf "Enter a server name: "
          read -r name_tag
    fi
You never count down num_instances, so that the while loop will run forever.

You may want to add code that checks if num_instances is really a number. Inside ((...)), you don't need the dollar or the quote characters; the code is prettier without them. Instead of [[...]], you can use ((...)), again without dollar or quotes.

Why do you distinguish between the two cases of a single server and more than one server? It seems to me that this makes your program overly complicated.

Last edited by berndbausch; 08-23-2018 at 09:36 PM. Reason: additional advice
 
  


Reply

Tags
bash, bash scripting


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 Script For Reading User Input then Compressing That Input to Tar file braveranger Linux - Newbie 13 11-15-2017 09:36 AM
Perl vs. bash code - number of lines noir911 Programming 11 10-07-2011 12:35 PM
[SOLVED] using sed to trim lines greater than maximum number of characters cxny Linux - Software 10 09-03-2011 01:25 PM
bash script to count number of lines with a specific property7 hhamid Programming 10 08-13-2010 01:35 AM
Bash : how do i check the user input and make sure is only chracter or only number ? CheeSen Programming 10 09-07-2008 03:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:20 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration