Bash: How to use read with conditions & loops


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash: How to use read with conditions & loops
# 8  
Old 10-13-2016
Quote:
Originally Posted by MadeInGermany
You need a trick that stuffs the result into the variable name that has been passed to the function as an argument.
Like Chubler_XL's
Thanks a lot, I have found a possible solution alone I know that isn't the best but It works ^^ But I will take your solution ^^:
Code:
#!/bin/bash


address_status() {
 printf "%s: " "$1"
  until
    read
    [[ "$REPLY" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]] &&
    {
    IFS=. read ip1 ip2 ip3 ip4 <<< "$REPLY"
    [[ $ip1 -le 255 && $ip2 -le 255 && $ip3 -le 255 && $ip4 -le 255 ]]
    }
  do
    printf "Invalid $1 IP address - Please re-enter\n%s: " "$1"
  done
 sed -i '12s/$/\n address '$REPLY'/' interfaces
}

network_status() {
 printf "%s: " "$1"
  until
    read
    [[ "$REPLY" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]] &&
    {
    IFS=. read ip1 ip2 ip3 ip4 <<< "$REPLY"
    [[ $ip1 -le 255 && $ip2 -le 255 && $ip3 -le 255 && $ip4 -le 255 ]]
    }
  do
    printf "Invalid $1 IP address - Please re-enter\n%s: " "$1"
  done
sed -i '13s/$/\n network '$REPLY'/' interfaces
}

netmask_status() {
 printf "%s: " "$1"
  until
    read
    [[ "$REPLY" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]] &&
    {
    IFS=. read ip1 ip2 ip3 ip4 <<< "$REPLY"
    [[ $ip1 -le 255 && $ip2 -le 255 && $ip3 -le 255 && $ip4 -le 255 ]]
    }
  do
    printf "Invalid $1 IP address - Please re-enter\n%s: " "$1"
  done
sed -i '14s/$/\n netmask '$REPLY'/' interfaces
}

broadcast_status() {
 printf "%s: " "$1"
  until
    read
    [[ "$REPLY" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]] &&
    {
    IFS=. read ip1 ip2 ip3 ip4 <<< "$REPLY"
    [[ $ip1 -le 255 && $ip2 -le 255 && $ip3 -le 255 && $ip4 -le 255 ]]
    }
  do
    printf "Invalid $1 IP address - Please re-enter\n%s: " "$1"
  done
sed -i '15s/$/\n broadcast '$REPLY'/' interfaces
}
gateway_status() {
  printf "%s: " "$1"
  until
    read
    [[ "$REPLY" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]] &&
    {
    IFS=. read ip1 ip2 ip3 ip4 <<< "$REPLY"
    [[ $ip1 -le 255 && $ip2 -le 255 && $ip3 -le 255 && $ip4 -le 255 ]]
    }
  do
    printf "Invalid $1 IP address - Please re-enter\n%s: " "$1"
  done
sed -i '16s/$/\n gateway '$REPLY'/' interfaces
}

cd /etc/network/

sed -i '1,30d' interfaces

echo -e "#--------------------------------------------------------------\n# The loopback network interface\n#--------------------------------------------------------------\n\nauto lo\niface lo inet loopback\n\n
#--------------------------------------------------------------\n# Eth0\n#--------------------------------------------------------------\nauto eth0\niface eth0 inet static" > interfaces


echo -e "Please enter the network informations into the /etc/network/interfaces file, complete them below :\n"

sed -n '1,10p' interfaces

printf "address " ; address_status
printf "network " ; network_status
printf "netmask " ; netmask_status
printf "broadcast " ; broadcast_status
printf "gateway " ; gateway_status


Last edited by Arnaudh78; 10-13-2016 at 05:08 PM..
# 9  
Old 10-13-2016
Quote:
Originally Posted by MadeInGermany
You can have more than one line in a while/until condition.
The following uses a code block between until...do plus another explicit code block within {...}
Code:
#!/bin/bash
fetchip() {
  printf "%s: " "$1"
  until
    read
    [[ "$REPLY" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]] &&
    {
    IFS=. read ip1 ip2 ip3 ip4 <<< "$REPLY"
    [[ $ip1 -le 255 && $ip2 -le 255 && $ip3 -le 255 && $ip4 -le 255 ]]
    }
  do
    printf "Invalid $1 IP address - Please re-enter\n%s: " "$1"
  done
}
...

Nice touch - I'm a bit of a RegEx fan, and considering we are already matching with one I'm inclined to extend it perhaps like this:

Code:
fetchip() {

    local IPDIGIT="(25[0-5]|2[0-4][0-9]|[1]?[0-9]?[0-9])"
    printf "%s: " "$1"
    until 
        read
        [[ "$REPLY" =~ ^(${IPDIGIT}\.){3}${IPDIGIT}$ ]]
    do
      printf "Invalid IP address - Please re-enter\n%s: " "$1"
    done

    printf -v $1 "%s" "$REPLY"
}


Quote:
Originally Posted by Arnaudh78
Thanks a lot, I have found a possible solution alone I know that isn't the best but It works
Yuck, repeating the same code 4 times. At least you realize this is bad LOL.

Last edited by Chubler_XL; 10-13-2016 at 06:45 PM..
# 10  
Old 10-14-2016
better late than never Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Loops & Variable

I am using this code: for e in {1..14} do awk '{gsub(/^.*GGGGGG|TTTTT.*$/,"",$0)} 1' $e.1 > ${e}.2 done However, in the second loop instead of GGGGGG|TTTTT, I should use AAAAAA|CCCCCC. For third loop CCAAAA|CCCCAA, so on and so forth. Is there any way to accomplish this without writing... (20 Replies)
Discussion started by: Xterra
20 Replies

2. Shell Programming and Scripting

[Bash] Read History function & Read Arrowkeys

Hi. How can I create a history function? (By "read" command or so) & How can I configure a read command so that the arrow keys are not displayed so funny? (^[[A) Thanks in advance. (4 Replies)
Discussion started by: sinnlosername
4 Replies

3. Shell Programming and Scripting

GNU & BSD Makefile Directives & Conditions Compatibility

Firstly, I would like to apologize if this is not the appropriate sub-forum to post about GNU/BSD makefile scripting. Though my code is in C++, because I am focusing on the makefile I thought it would go better in shell scripting. Please correct me if I am wrong. Secondly, I am not interested in... (0 Replies)
Discussion started by: AntumDeluge
0 Replies

4. Shell Programming and Scripting

bash loops

hello i'm writing a script and I want to use a for loop inside a while loop as following: while read line; do echo $line for i in $vrm; do echo $i done done < './contacts' when i use just the while loop it prints the lines from file ./contacts just... (13 Replies)
Discussion started by: vlm
13 Replies

5. UNIX for Dummies Questions & Answers

Faster than nested while read loops?

Hi experts, I just want to know if there is a better solution to my nested while read loops below: while read line; do while read line2; do while read line3; do echo "$line $line2 $line3" done < file3.txt done < file2.txt done < file1.txt >... (4 Replies)
Discussion started by: chstr_14
4 Replies

6. Shell Programming and Scripting

PHP read large string & split in multidimensional arrays & assign fieldnames & write into MYSQL

Hi, I hope the title does not scare people to look into this thread but it describes roughly what I'm trying to do. I need a solution in PHP. I'm a programming beginner, so it might be that the approach to solve this, might be easier to solve with an other approach of someone else, so if you... (0 Replies)
Discussion started by: lowmaster
0 Replies

7. Shell Programming and Scripting

Bash Script to Read & Write on different directories

Hi, root@server] df -h 121G 14G 101G 12% /home 147G 126G 14G 91% /backup We having our site files and images are storing in /backup/home/user/files/ through symbolic link created in /home directory pointing in /backup directory as following. root@server] cd /home... (1 Reply)
Discussion started by: mirfan
1 Replies

8. Shell Programming and Scripting

multiple conditions in if using && operator

VARIABLE="project" if && ] then echo "VARIABLE is not empty" fi this is not working what is wrong in the syntax?? (2 Replies)
Discussion started by: codeman007
2 Replies

9. Shell Programming and Scripting

scripting headache... loops & variables

Surely there's an easier way to do this, lets see if anyone knows! I am new to scripting so go easy on me! I have the following script and at the moment it doesn't work and I believe the problem is that I am using a while loop within a while loop. When I run the script using sh -x I can see... (6 Replies)
Discussion started by: StevePace
6 Replies

10. Shell Programming and Scripting

if statement with two conditions -e, &&

Wow I'm so zoned out I don't even know if I posted this question up already (I couldn't find it in my book marks or in "yesterday's" post). My question is, I'm writing a korn script that does something like the following, but I don't yet completely understand the syntax. I need to check that... (16 Replies)
Discussion started by: yongho
16 Replies
Login or Register to Ask a Question