Bash User Input Valid Then Continue

I have a script which reads a controller ID and continues. I am trying to add some checks for user input "ctrl".

Valid responses would be whole number 1 thur 5. If value is between 1-5 (single numeric value) then continue with the rest of the script. If any other value then Exit script or continue to ask user input until valid.

I tried a couple of while loops do with esac, but can not seem to get a handle

Code:
read -p "Controller ID#: " ctrl


Any help would be greatly appreciated


Similar Content



Bash User Input With Static Value Validation

So I created this big script and want to ask for username and set a variable, however if the username exist in the username variable then continue with script otherwise ask the user to enter his name.

Flow:
-- start script "Enter User Name"
-- ask user to input username
-- if username is blank or different value then put that username in $USERNAME variable
-- then continue with the rest of the script

-- next time this user runs the script it will have his name loaded so he just presses enter to continue with the script.

-- If it is NOT the same user then, new user can erase existing input and enter a new name.

Hope it makes sense...

Thanks in advance for the help.

How To Return From Shell 'read' Command Passed In Expect Script?

I have a shell script that calls an expect script I wrote to ssh login to another host and get user input regarding that host's network configuration. I pass four arguments to the expect script: the remote host ip address, the username, the password, and the list of commands to run. My expect script is below:

#!/usr/bin/expect
# Usage: expectssh <host> <ssh user> <ssh password> <script>

set timeout 60
set prompt "(%|#|\\$) $"
set commands [lindex $argv 3];

spawn ssh [lindex $argv 1]@[lindex $argv 0]

expect {
"*assword:" {
send -- "[lindex $argv 2]\r"
expect -re "$prompt"
send -- "$commands\r"
}

"you sure you want to continue connecting" {
send -- "yes\r"
expect "*assword:"
send -- "[lindex $argv 2]\r"
expect -re "$prompt"
send -- "$commands\r"
}

timeout {
exit }

expect -re $prompt
send -- "exit\r"
}

The script runs well, except that if I send a command such as 'read' that requires user input, the script does not continue or exit after the user presses enter. It just hangs.

The commands I pass to the expect script and it's call are as follows:
SCRIPT='hostname > response.txt;netstat -rn;read net_card?"What is the network interface card number? " >> response.txt; read net_mask?"What is the subnet mask? " >> response.txt'

/usr/bin/expect ./expectssh.exp $hostip $usr $pswd "$SCRIPT"

Any suggestions on how I can pass a command to my expect script that requires user input without it hanging?

On a side note because I know it will come up - I am not allowed to do key-based automatic SSH login. I have to prompt for a username and password, which is done from my main shell script.

Thanks for any suggestions and help you can provide!

How To Copy File From Remote Host To Local Host Then Delete From Remote Host

I have an expect script to SSH to a remote host and obtain some user inputs and information about the server/network configuration. The responses are saved in a text file that I then need to copy to my local host so that I can read the lines into variables for use in the parent shell script.

Is there a way to do this without needing to enter the username and password for the local host to use function scp? I have tried the following in my expect script to no avail:
Code:
spawn scp $usr@$host:$flnm .
expect {
	-re "(.*)assword:" { 
		send -s "$pswd\r"
	}
}

I have also tried to directly scp the file and enter the username and password to try to debug the issue, and that doesn't work either:
Code:
spawn scp file.txt user@host:file.txt
expect {
	-re "(.*)assword:" {
		send -s "password\r"
	}
	"you sure you want to continue connecting" {
		send -s "yes\r"
		exp_continue
	}
}

In both scenarios I have used exp_internal 1, and there are no errors. But I do not end up with the file on my local host.

Following the copy, I would like to delete the file from the remote host. Any suggestions on how to accomplish this?

Regular Expression In Expect Script To Prevent Printing To Screen

Hello, I have an expect script where I ssh to a remote host to determine the network configuration and get from the user the network interface card that should be used. From their response, I determine the subnet mask and save the information to a text file that is later transmitted back to my local host. This is all so that I can set up virtual IP aliasing and verify that the physical IP address of the local and remote host are on the same subnet prior to continuing with the setup. I am running the script on Linux, with expect version 5.45.

The code itelf works just fine, but I'm having some issues with how it displays on the screen. As you'll see below in the example, the default system prompt displays, as does the user input command that I'm sending to the shell from the expect script.

Is there a regular expression or something that I can write to prevent the prompt and command that I'm sending from printing to the screen? I know that it should be suppressed if I have an expect command following the Code:
send -s "\nread n_card?'Enter the network interface card number for this server (i.e. eth0):   '\r

command, but everything I have tried for strings and regular expressions to expect causes the netstat -rn output to not show up all of a sudden. I'm new to expect, so I'm not really sure why this is happening.

I would really appreciate any help/suggestions. Thanks for your time!

Part of the Script Code:
Code:
expect {
   -re $prompt {   ;# Send individual commands and get user input
        set timeout -1
        
        # Get partner hostname and put in vipsetup.txt file
        send -s "hostname > vipsetup.txt\r"  
        expect -re $prompt
        
        # Display the network routing info for the user and prompt for 
        # network interface card number
        send -s "print \"The network routing table for the $ptner server is displayed below:\n\" ; netstat -rn \r"
        
        expect -re "\r(.*):\r"
        send -s "\nread n_card?'Enter the network interface card number for this server (i.e. eth0):   '\r"
        interact "\r" return    ;# Wait for user input from read command
        send -- "\r"
        send -s "echo \$n_card >> vipsetup.txt\r"  
        
        # Obtain subnet mask information for partner based on network 
        # interface card number being used
        send -s "msk=\$(cat /etc/sysconfig/network-scripts/ifcfg-\$n_card | grep NETMASK)\r"
        send -s "msk=\$(echo \${msk#NETMASK=})\r"
        send -s "echo \$msk >> vipsetup.txt\r"
    }
    timeout {
        send_user "Connection to host $hostip timed out."
        exit 6 
    }
    eof {
        send_user "Connection to host $hostip failed."
        exit
    }
}


Script Output:
Code:
The network routing table for the PRIMARY server is displayed below:
 
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
10.105.65.0     0.0.0.0         255.255.255.0   U         0 0          0 eth0
0.0.0.0         10.105.65.1     0.0.0.0         UG        0 0          0 eth0
 [root@remotehost root]$
[root@remotehost root]$ ber for this server (i.e. eth0):   '              < 
Enter the network interface card number for this server (i.e. eth0):   eth0

Facing Problem While Solving Josephus Problem In BASH

I want to solve this question using BASH

100 people standing in a circle in an order 1 to 100. No. 1 has a sword. He kills the next person (i.e. No. 2) and gives the sword to the next (i.e. No. 3). All people do the same until only 1 survives. Which number survives at the last?

If input is taken by keyboard, it should print the saved person number. Not only for hard coded 100 person.

Suppose I enter 5 as user input, it should print "3 is safe".
if I enter 11, it should print "7 is safe".
Please help me to write this script.

Thanks

Bash Scrip Running On Remote Server

This is my script and the syntax to run this script is give ip and next will be the file or script you want to perform on remote server



#!/bin/bash

# The private key used to identify this machine
IDENTITY_KEY=/home/adnew.pem

syntax()
{
echo "Syntax: Ec2.sh server_ip scriptFile]"
echo "For example: ./Ec2.sh server_ip scriptFile"
exit 1
}

if [ $# -ne 2 ]
then
echo not enough arguments
syntax
fi


echo "Running script $2 on $1"
ssh -ttq -i $IDENTITY_KEY ec2-user@$1 sudo -i 'bash -s' < $2
exit
exit
echo "Done"



on script file i have give for testing

touch /root/test
ls /root/test
exit
exit

it makes the file but do not show the ls output by giving error


tcgetattr: Inappropriate ioctl for device

exit


what I have to do ??

Automating Script To Run Every Minute Through Other Manually Created User Using Crond

Hy All,

I have two nodes running RHEL 6.4 which are in cluster. I have clustered httpd service. Now I have certain application suppose X which reside in SAN partition. Now my job is to only automate this script to run by analyzing httpd status. I have successfully created the script apptrigger.sh whose content is given below,

-bash-4.1$ cat apptrigger.sh
a=0
b=0
c=0
d=0
a="service httpd status"
b=`eval $a`
##### CHECKING WHETHER HTTPD SERVICE IS RUNNING OR NOT #####
if [ "$b" != "httpd is stopped" ]; then
c="bash /switchapp/switch/ezlinkenterprise/bin/ezlink-cluster.sh status"
d=`eval $c`
##### CHECKING WHETHER EZ IS RUNNING OR NOT #####
if [ "$d" != "EzTaskMgr is Running" ]; then
ezstart
fi
fi

I have created this script in say test user. Now when I put this script to automate or run every minute through test user and defined its value in crontab -e of test user whose content is as shown below
$crontab -l
* * * * * /bin/bash /home/test/apptrigger.sh

but this script is running as root user so I cannot get the o/p
I think some environment variable need to be set. Anyone can guide me with this step? Thanks for your views and suggestion.

Best Way To Run Two Interdependent Scripts

Hi All,

I have two scripts, the aim of these two scripts is, to check whether a particular script is running or not, if it wont runs, then throw a mail.

How i Achieved this output is, I wrote first script.

I created an infinite while loop which performs below steps

1. It creates a touch file
2. Triggers the script which needs to be monitored if its working or not.
3. Removes the touch file.

If the second step fails, then the remove file command will not happen and the script will stuck there itself.

I created an another script which checks the creation time of the touch file and if it is more than ten minutes, it means the second step in the first script is hanged, which also means that particular script is not working.

So if the creation time is more than 2 minutes the second script will throw a mail.

Below are the two scripts.

Code:
#!/bin/ksh



userid="chansd"

filename="/apps/log/check.txt"

while true ;do
touch $filename
pass=`/apps/eDMZ/call_st.ksh $userid`
sleep 20
rm $filename
done

Below script checks the file creation time and throws email if it is older than 2 minutes
Code:
#!/bin/ksh


filename="/apps/log/check.txt"

if [ -f "${filename}" ]
then
if test "`find $filename -mmin +2`"
then
echo "script is not working ! Please act on it" | mail -s "Script  is not working" Example@mail.com
fi


else

        exit 1

fi


What im going to do is

1. I am going to run the first script in background so it runs forever.
2. I am going to run the second script in cron forevry 5 mins to check the file creation time.

3. So if the first script hangs . I will kill the process using process id and after the issue resolves with the inner script, I will run the main script again.

I am new to Linux, Please let me know if this approach will work as expected.

Script

user="john bob randy susan"
I extracted local user list as: cat /etc/passwd | cut -d ":" -f1

Now I need to write a script to find the difference in user between these two (users defined as above and local user). I tried many ways its not working. Any help

#!/bin/bash
users="john bob randy susan"
luser=`/bin/cat /etc/passwd | cut -d ":" -f1`
......
....


Thank you

How To Determine If Syntax Array=( Str1 Str2 Str3 ) To Declare Array Is Valid

Hi, I am creating a Korn shell script and need to create an array where each element corresponds to one line of an input file. Being able to do the following would be awesome: Code:
array=( $(cat file.txt) )

However, I'm finding that not all of my development boxes allow this. Some return this: Code:
ksh: syntax error: `(' unexpected

I suspect this has something to do with the Korn shell version. Does anyone know how I can find out what version is required in order to be able to declare arrays, as above? Or is this an OS version issue?

I would really appreciate any tips on how I can find out the minimum requirements for this syntax to be valid.

Thanks.