Sponsored Content
Top Forums Shell Programming and Scripting IF condition against a ARRAY in shell script Post 302416512 by frans on Monday 26th of April 2010 06:00:54 PM
Old 04-26-2010
Code:
#!/bin/bash
ARRAY=( a b c d e f )
read -p "Enter element : " K
FOUND=0
for E in ${ARRAY[@]}
do	[ "$K" = "$E" ] && FOUND=1
done
echo -n "$K "; ((FOUND)) && echo "found" || echo "not found"

I use a flag here but if you only have to perform an action if found then use following code
Code:
#!/bin/bash
ARRAY=( a b c d e f )
read -p "Enter element : " K
for E in ${ARRAY[@]}
do	if [ "$K" = "$E" ]
        then echo "$K found"
        fi
done

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

need help with test condition in shell script

I'm new to scripting and I need help with a bourn shell script. What i'm trying to do is a test condition where "if the time is within 2 hours, it's true" and so on. The time is in the following format DATE=`/bin/date +"%Y%m%d%H%S"` for example, 20060907152000. So, what the script first... (9 Replies)
Discussion started by: pieman8080
9 Replies

2. Shell Programming and Scripting

Help with shell script to check the condition.

:) Hi, I want to script for this scenerio, OSR Settings Scenario : We are looking to find all the *.a files from the following locations in the filesystem of a server. OSR Directories /etc /bin /usr/bin /usr/sbin /var/adm These *.a files should have the permissions on... (12 Replies)
Discussion started by: sakthilinux
12 Replies

3. AIX

if condition in AIX5.3-10 shell script

True if file exists and has been modified since it was last read. if then command else exit fi i am on AIX5.3-10. it does not understand -N any other way. i can use -ot (file1 is older than file2), but prefer -N if possible. (3 Replies)
Discussion started by: tjmannonline
3 Replies

4. Shell Programming and Scripting

An issue with condition statement in shell script

Hello forum members. please go through the below mentioned issue and let me know the right solution. I have to write a script which runs another script .the executable script take input parmeters.so iam writing the the script below . Sample Code:Begins #! /bin/ksh echo " enter... (2 Replies)
Discussion started by: rajkumar_g
2 Replies

5. Shell Programming and Scripting

Errors in if condition validations-Shell Script

Hello All, i am facing difficulty in validating the values, kindly help me in resolving the issue. Thanks a lot in advance. -Chandra Script:Test.sh #! /bin/sh # *************************************************************************** # Function to display help function usage()... (1 Reply)
Discussion started by: duddukuri
1 Replies

6. UNIX for Dummies Questions & Answers

While condition in shell script

while do if ;then read driverName else driverName="" fi done can anyone please explain what exactly is happening on 1st line...is it like the conditions being ORed...I have no clue about this. (4 Replies)
Discussion started by: rtagarra
4 Replies

7. Shell Programming and Scripting

Substring check in IF condition in shell script

I want to check if the string has the substring in IF condition then process... i tried below but not working if ]; then ............. field can be "reserved1" ....reservedn / fillspaces1 ... fillspacesn (4 Replies)
Discussion started by: greenworld123
4 Replies

8. Shell Programming and Scripting

Need Help to add Condition in Shell Script..

Hi Team, I m very new to shell scripting , i want to add following condition in my script . Can anybody help me. There are three port in My node "$port" port_A port_B port_C I Want to add following Conditions in my script. If Node is connected to port_A and port_B script... (10 Replies)
Discussion started by: Ganesh Mankar
10 Replies

9. Shell Programming and Scripting

If condition shell script beginner

Hi all I have the folloing process that needs checking often: ps -ef | grep ih bscsrtx 206 15901 0 11:28:10 pts/6 0:00 fih -r4 bscsrtx 218 15901 0 11:28:27 pts/6 0:01 aih bscsrtx 29763 15901 4 11:27:16 pts/6 0:59 rdh -prih root 429 27268 0 11:30:15 pts/td ... (13 Replies)
Discussion started by: fretagi
13 Replies

10. Shell Programming and Scripting

Pass C shell array to another C shell script(csh) and shell(sh)

Dear Friends, Please help me on this my script name is send.csh In this i have written the statement like this set args = ( city state country price ) I want to pass this array to another c shell called receiver.csh. and i want to use it in this c shell or how to pass to... (2 Replies)
Discussion started by: SA_Palani
2 Replies
IO::Async::Connector(3pm)				User Contributed Perl Documentation				 IO::Async::Connector(3pm)

NAME
"IO::Async::Connector" - perform non-blocking socket connections SYNOPSIS
This object is used indirectly via an "IO::Async::Loop": use IO::Async::Loop; my $loop = IO::Async::Loop->new; $loop->connect( host => "www.example.com", service => "http", socktype => 'stream', on_connected => sub { my ( $sock ) = @_; print "Now connected via $sock "; ... }, on_resolve_error => sub { die "Cannot resolve - $_[-1] "; }, on_connect_error => sub { die "Cannot connect - $_[0] failed $_[-1] "; }, ); DESCRIPTION
This module extends an "IO::Async::Loop" to give it the ability to create socket connections in a non-blocking manner. There are two modes of operation. Firstly, a list of addresses can be provided which will be tried in turn. Alternatively as a convenience, if a host and service name are provided instead of a list of addresses, these will be resolved using the underlying loop's "resolve" method into the list of addresses. When attempting to connect to any among a list of addresses, there may be failures among the first attempts, before a valid connection is made. For example, the resolver may have returned some IPv6 addresses, but only IPv4 routes are valid on the system. In this case, the first connect(2) syscall will fail. This isn't yet a fatal error, if there are more addresses to try, perhaps some IPv4 ones. For this reason, it is possible that the operation eventually succeeds even though some system calls initially fail. To be aware of individual failures, the optional "on_fail" callback can be used. This will be invoked on each individual socket(2) or connect(2) failure, which may be useful for debugging or logging. Because this module simply uses the "getaddrinfo" resolver, it will be fully IPv6-aware if the underlying platform's resolver is. This allows programs to be fully IPv6-capable. METHODS
$loop->connect( %params ) This method performs a non-blocking connection to a given address or set of addresses, and invokes a continuation when the socket is connected. In plain address mode, the %params hash takes the following keys: addrs => ARRAY Reference to an array of (possibly-multiple) address structures to attempt to connect to. Each should be in the layout described for "addr". Such a layout is returned by the "getaddrinfo" named resolver. addr => HASH or ARRAY Shortcut for passing a single address to connect to; it may be passed directly with this key, instead of in another array on its own. This should be in a format recognised by IO::Async::OS's "extract_addrinfo" method. See also the "EXAMPLES" section. local_addrs => ARRAY local_addr => HASH or ARRAY Optional. Similar to the "addrs" or "addr" parameters, these specify a local address or set of addresses to bind(2) the socket to before connect(2)ing it. on_connected => CODE A continuation that is invoked on a successful "connect(22)" call to a valid socket. It will be passed the connected socket handle, as an "IO::Socket" object. $on_connected->( $handle ) on_stream => CODE An alternative to "on_connected", a continuation that is passed an instance of IO::Async::Stream when the socket is connected. This is provided as a convenience for the common case that a Stream object is required as the transport for a Protocol object. $on_stream->( $stream ) on_socket => CODE Similar to "on_stream", but constructs an instance of IO::Async::Socket. This is most useful for "SOCK_DGRAM" or "SOCK_RAW" sockets. $on_socket->( $socket ) on_connect_error => CODE A continuation that is invoked after all of the addresses have been tried, and none of them succeeded. It will be passed the most significant error that occurred, and the name of the operation it occurred in. Errors from the connect(2) syscall are considered most significant, then bind(2), then finally socket(2). $on_connect_error->( $syscall, $! ) on_fail => CODE Optional. After an individual socket(2) or connect(2) syscall has failed, this callback is invoked to inform of the error. It is passed the name of the syscall that failed, the arguments that were passed to it, and the error it generated. I.e. $on_fail->( "socket", $family, $socktype, $protocol, $! ); $on_fail->( "bind", $sock, $address, $! ); $on_fail->( "connect", $sock, $address, $! ); Because of the "try all" nature when given a list of multiple addresses, this callback may be invoked multiple times, even before an eventual success. When performing the resolution step too, the "addrs" or "addr" keys are ignored, and instead the following keys are taken: host => STRING service => STRING The hostname and service name to connect to. local_host => STRING local_service => STRING Optional. The hostname and/or service name to bind(2) the socket to locally before connecting to the peer. family => INT socktype => INT protocol => INT flags => INT Optional. Other arguments to pass along with "host" and "service" to the "getaddrinfo" call. socktype => STRING Optionally may instead be one of the values 'stream', 'dgram' or 'raw' to stand for "SOCK_STREAM", "SOCK_DGRAM" or "SOCK_RAW". This utility is provided to allow the caller to avoid a separate "use Socket" only for importing these constants. on_resolve_error => CODE A continuation that is invoked when the name resolution attempt fails. This is invoked in the same way as the "on_error" continuation for the "resolve" method. It is necessary to pass the "socktype" hint to the resolver when resolving the host/service names into an address, as some OS's "getaddrinfo" functions require this hint. A warning is emitted if neither "socktype" nor "protocol" hint is defined when performing a "getaddrinfo" lookup. To avoid this warning while still specifying no particular "socktype" hint (perhaps to invoke some OS-specific behaviour), pass 0 as the "socktype" value. EXAMPLES
Passing Plain Socket Addresses The "addr" or "addrs" parameters should contain a definition of a plain socket address in a form that the IO::Async::OS "extract_addrinfo" method can use. This example shows how to use the "Socket" functions to construct one for TCP port 8001 on address 10.0.0.1: $loop->connect( addr => { family => "inet", socktype => "stream", port => 8001, ip => "10.0.0.1", }, ... ); This example shows another way to connect to a UNIX socket at echo.sock. $loop->connect( addr => { family => "unix", socktype => "stream", path => "echo.sock", }, ... ); AUTHOR
Paul Evans <leonerd@leonerd.org.uk> perl v5.14.2 2012-10-24 IO::Async::Connector(3pm)
All times are GMT -4. The time now is 06:06 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy