Sponsored Content
Top Forums Shell Programming and Scripting Bash: How to use read with conditions & loops Post 302983547 by Arnaudh78 on Wednesday 12th of October 2016 07:31:54 PM
Old 10-12-2016
Bash: How to use read with conditions & loops

Hello,

Below I try to control that the input is good an IP :
Code:
#!/bin/bash

cp /home/scripts/choice_interfaces.txt /home/scripts/interfaces.txt
chmod 644 /home/scripts/interfaces.txt

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

sed -n '1,10p' choice_interfaces.txt

printf "address " ; read -r address
if [[ $address =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
printf "network " ; read -r network
printf "netmask " ; read -r netmask
printf "broadcast " ; read -r broadcast
printf "gateway " ; read -r gateway
else
echo "$address doesn't correct"
fi
sed -i '12s/$/\n address '$address'/' interfaces.txt
sed -i '13s/$/\n network '$network'/' interfaces.txt
sed -i '14s/$/\n netmask '$netmask'/' interfaces.txt
sed -i '15s/$/\n broadcast '$broadcast'/' interfaces.txt
sed -i '16s/$/\n gateway '$gateway'/' interfaces.txt

mv /home/scripts/interfaces.txt /etc/network/interfaces

I try a loop.. :
Code:
printf "address " ; read -r address
while [[ $address =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; do
printf "address " ; read -r address
done 
I wanted that the user be invited to type until his input is good
printf "network " ; read -r network 
printf "netmask " ; read -r netmask
printf "broadcast " ; read -r broadcast
printf "gateway " ; read -r gateway

I do an example that I will wish, because I row.. :
Code:
#!/bin/bash

cp /home/scripts/choice_interfaces.txt /home/scripts/interfaces.txt
chmod 644 /home/scripts/interfaces.txt

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

sed -n '1,10p' choice_interfaces.txt

while[[ $address $network $netmask $broadcast $gateway  =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] || [[-z $address $network $netmask $broadcast $gateway]]; do (I found it on google to test if the input is empty but Its not working)
printf "address " ; read -r address
printf "network " ; read -r network
printf "netmask " ; read -r netmask
printf "broadcast " ; read -r broadcast
printf "gateway " ; read -r gateway
done

sed -i '12s/$/\n address '$address'/' interfaces.txt
sed -i '13s/$/\n network '$network'/' interfaces.txt
sed -i '14s/$/\n netmask '$netmask'/' interfaces.txt
sed -i '15s/$/\n broadcast '$broadcast'/' interfaces.txt
sed -i '16s/$/\n gateway '$gateway'/' interfaces.txt

#mv /home/scripts/interfaces.txt /etc/network/interfaces1

Thanks in advance SmilieSmilie I hope I was enough clear Smilie
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
RBASH(1)						      General Commands Manual							  RBASH(1)

NAME
rbash - restricted bash, see bash(1) RESTRICTED SHELL
If bash is started with the name rbash, or the -r option is supplied at invocation, the shell becomes restricted. A restricted shell is used to set up an environment more controlled than the standard shell. It behaves identically to bash with the exception that the follow- ing are disallowed or not performed: o changing directories with cd o setting or unsetting the values of SHELL, PATH, ENV, or BASH_ENV o specifying command names containing / o specifying a file name containing a / as an argument to the . builtin command o specifying a filename containing a slash as an argument to the -p option to the hash builtin command o importing function definitions from the shell environment at startup o parsing the value of SHELLOPTS from the shell environment at startup o redirecting output using the >, >|, <>, >&, &>, and >> redirection operators o using the exec builtin command to replace the shell with another command o adding or deleting builtin commands with the -f and -d options to the enable builtin command o using the enable builtin command to enable disabled shell builtins o specifying the -p option to the command builtin command o turning off restricted mode with set +r or set +o restricted. These restrictions are enforced after any startup files are read. When a command that is found to be a shell script is executed, rbash turns off any restrictions in the shell spawned to execute the script. SEE ALSO
bash(1) GNU Bash-4.0 2004 Apr 20 RBASH(1)
All times are GMT -4. The time now is 06:00 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy