Script variable help, Varying number of arguments to excute script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script variable help, Varying number of arguments to excute script
# 8  
Old 06-02-2013
Further Info

Hi Don,
Thanks for your response again its really appreacited, terminating the output ctl-d would be good, and in regards to invoking the singlemsisdn script
it would need to be as per the below
for example the following numbers were entered earlier
Code:
447867364871
447867364872
447867364873
447867364874

Code:
singlemsisdn 447867364871 447867364872 447867364873 447867364874

So essentially it requires the script name followed by each MSISDN with a space in between.
Thanks again.

Last edited by Scrutinizer; 06-02-2013 at 07:11 PM.. Reason: code tags
# 9  
Old 06-03-2013
For the record, private e-mail answered the question about how many operands can be given to singlemsisdn in a single invocation: the response was 100.

The following script was tested using both bash and a recent (i.e., version newer than 1988) Korn shell, but specifies bash since that is the shell the OP requested. This script uses variable arrays, substring variable expansions, and pattern substitution variable expansions that are extensions to the standards but work the same in both bash and ksh.

For this forum, this script is huge, but the well over half of the script is comments. It is written assuming that this script is stored in a file named getUKmobile, but you can change the name to anything you want. (If you change the name, please replace the name in the 2nd line of this with whatever name you use.)
Code:
#!/bin/bash
# USAGE: getUKmobile
# DESCRIPTION:
#    Get a list of UK mobile phone numbers from the user and verify that they
#    are valid mobile phone or wireless phone numbers authorized by the
#    National Telephone Numbering Plan regulated by the UK government's Office
#    of Communications (Ofcom) for the United Kingdom and its Crown dependencies
#    as described by Wikipedia:
#       http://en.wikipedia.org/wiki/Telephone_numbers_in_the_United_Kingdom
#
#    When the user terminates the input list, the singlemsisdn utility will be
#    invoked with the accumlated, validated numbers as operands.
#
# EXIT STATUS:
#       1       No valid 44 format entries were entered.
#       other   Exit status from singlemsisdn invoked with validated numbers
#               provided as operands.

# USAGE: v44 in
# DESCRIPTION:
# The v44 function verifies that an input string ("in") is a twelve digit string
# representing a mobile or WiFi phone number that starts with the 44 UK country
# code and is followed by a 10 digit number that is a valid number according to
# the UK NTNP.
#
# From the website noted above, valid UK mobile and WiFi phone numbers are of
# the form:
#       074xx xxxxxx    Mobile phones (in use since November 2009)
#       075xx xxxxxx    Mobile phones (in use since May 2007)
#       07624 xxxxxx    mobile phones on the Isle of Man
#       077xx xxxxxx    Mobile phones (former 03xx and 04xx-mostly Vodafone and
#                       O2 (formerly Cellnet))
#       078xx xxxxxx    Mobile phones (former 05xx, 06xx and 08xx-mostly
#                       Vodafone and O2 (formerly Cellnet))
#       079xx xxxxxx    Mobile phones (former 09xx-mostly Orange and T-Mobile
#                       (formerly one2one))
#       07911 2xxxxx
#       07911 8xxxxx    WiFi numbers (used by companies such as Tovo and
#                       Mobiboo)
#
# and valid personal numbers and pager numbers are of the forms:
#       070 xxxx xxxx   Personal Numbering
#       076 xxxx xxxx   Pagers (excluding 07624, used for mobile phones on the
#                       Isle of Man)
#
# Although personal numbers and pager numbers are in the same set of numbers
# (starting with 447), this function rejects numbers in these ranges.
#
# Validated numbers are added to the valid[] array and the exit status shall be
# zero.  Otherwise, a non-zero exit status will be returned and and the valid[]
# array will not be modified.
#
# EXIT STATUS:
#       0       Validation succeeded, "in" added to valid[].
#       1       wrong number of arguments
#       2       in not 14 character
#       3       in contains non-numeric characters
#       4       in does not start with 447
#       5       in is not in range for a UK NTNP mobile or WiFi phone number
v44() { [ $# -ne 1 ] && return 1
        [ ${#1} -ne 12 ] && return 2
        [ "${1//[0-9]/}" != "" ] && return 3
        v44_in=${1#447} # Strip off leading 447
        [ 447$v44_in != $1 ] && return 4
        v44_1=${v44_in:0:1}
        [ "${v44_1#[45789]}" != "" ] && [ ${v44_in:0:3} != 624 ] && return 5
        valid[$((vpnc++))]=$1
}

# Initialize variables:
err=(   "internal error"    # Error messages corresponding to v44 exit codes
        "internal error: v44 called incorrectly"
        "Input must be 14 characters"
        "Input must be numeric"
        "Input must start with \"447\""
        "Input number is not in range for a UK mobile or WiFi phone number"
)
limit=100       # Maximum number of valid entries to accept in a single run.
vpnc=0          # Validated Phone Number Count

# main program:
while printf "Enter MSISDN in 44 format and hit RETURN (ctl-D to exit): "
do      IFS="" read -r input || break
        v44 "$input"
        rc=$?
        if [ $rc -eq 0 ]
        then    if [ $vpnc -ge $limit ]
                then    printf "Input limit reached."
                        break
                fi
        else    printf "%s: \"%s\" is not in 44 format\n%s\n\n" \
                        "${0##*/}" "$input" "${err[$rc]}" >&2
        fi
done
if [ ${#valid[@]} -gt 0 ]
then    printf "\nProcessing %d phone numbers, please wait...\n" "${#valid[@]}"
        ./singlemsisdn "${valid[@]}"
        exit $?
else    printf "\n%s: No valid 44 format numbers were entered.\n" "${0##*/}" >&2
        exit 1
fi

# 10  
Old 07-22-2013
Thanks Don, One more question

Hi Don, Finally got round to checking the above and got stuck on the script this then intiates. The output for the above for example would be

singlemsisdn 447825812345 447825823344

The singlemsisdn script i have is below

Code:
#!/usr/bin/expect
log_user 1
set msisdn0 [lindex $argv 0]
set esc "\033"
log_user 0
set timeout 10
spawn ssh -o "StrictHostKeyChecking no" remote_eng@INMSN1
expect -re "*******@*******'s password:"
send "********\r"
expect -re "Quit"
send "2\r"
expect "Hit ENTER or type command to continue"
send "\r"
send "i"
sleep 1
send "$msisdn0"
send "$esc"
send "$esc"
send ":wq\r"
expect -re "Press <Return> to continue"
send "\r"
sleep 1
send "3\r"
log_user 1
interact \r return
send "20\r"

As you can see this example is a lookup for 1 example msisdn but setting the argument that was entered within the script intial string.

In the script you have written for me it passes a variable number of arguments depending on how many the user entered. How would you recommend to paste this into the script where it shows
send "$msisdn0" I cant seem to get it to work.

Also I have noticed on your original script the user is unable to paste a list of MSISDN's in as it asks for them individually and if you attempt to paste a list you get the below

Code:
Enter MSISDN in 44 format and hit RETURN (ctl-D to exit): 447825*****
447825*****
447825*****Enter MSISDN in 44 format and hit RETURN (ctl-D to exit): Enter MSISDN in 44 format and hit RETURN (ctl-D to exit):

Is there anyway of being able to paste a list in ?

Thanks for your help.

Last edited by mutley2202; 07-22-2013 at 06:10 PM.. Reason: adding
# 11  
Old 07-23-2013
I assume that you remember that you told me in PM that singlemsisdn could take up to 100 operands. I limited getUKmobile so it would never invoke singlemsisdn with more than 100 operands. Now you say it only takes one???

You could rewrite getUKmobile to invoke singlemsisdn every time an msisdn is verified (instead of saving them up and calling singlemsisdn once at the end) or you can modify singlemsisdn to accept multiple operands. I haven't written any expect scripts for a couple of decades and I can't entirely determine the menu system of the command with which your expect script is interacting, but the following should provide something close that you can modify to make work for you:
Code:
#!/usr/bin/expect
log_user 1
set msisdn0 [lindex $argv 0]
set argn 0
set esc "\033"
log_user 0
set timeout [expr {10 * $argc}]
spawn ssh -o "StrictHostKeyChecking no" remote_eng@INMSN1
expect -re "*******@*******'s password:"
send "********\r"
expect -re "Quit"
while { $argn < $argc } {
        set msisdn0 [lindex $argv $argn]
        incr argn
        send "2\r"
        expect "Hit ENTER or type command to continue"
        send "\r"
        send "i"
        sleep 1
        send "$msisdn0"
        send "$esc"
        send "$esc"
        send ":wq\r"
        expect -re "Press <Return> to continue"
        send "\r"
        sleep 1
}
send "3\r"
log_user 1
interact \r return
send "20\r"

The code in red is added code; the code in yellow is removed code. Code indentation changes are not color coded, but are obvious. I have no idea if the modified timeout value will be appropriate for your needs, but I tried 10 times the number of operands as a starting point for you to adjust.

I wrote the getUKmobile bash script to your specifications. If you want to change the specifications, why don't you write up the new requirements and try modifyting the script to add this new feature? As a hint I would suggest modifying the following code:
Code:
while printf "Enter MSISDN in 44 format and hit RETURN (ctl-D to exit): "
do      IFS="" read -r input || break
        v44 "$input"
        rc=$?
        if [ $rc -eq 0 ]
        then    if [ $vpnc -ge $limit ]
                then    printf "Input limit reached."
                        break
                fi
        else    printf "%s: \"%s\" is not in 44 format\n%s\n\n" \
                        "${0##*/}" "$input" "${err[$rc]}" >&2
        fi
done

to read an array or an explicit maximum number of variables and add a loop to process each array element or (non-empty) variable. You'll need to set IFS to the separator your users will use between entries pasted together on input lines. (Don't forget to update the prompt to indicate how to enter multiple 44 format MSISDN entries.) Please try to make the modifications on your own and let us know if you run into problems.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assigning any number to the variable in cshell script

Hello Guys, I would like to ask you for a favor. Could you please help me how can I assign any number as the parameter to a, from stdin (-c), in the following command line by using the 'switch' in a script? awk '$8>a {print "File name:" $5,$8}' I would also appreciate if you can share any... (1 Reply)
Discussion started by: Padavan
1 Replies

2. Shell Programming and Scripting

Bash script to give multiple choices and a varying number of answers

Hello everybody, I use `case' quite a lot but , excellent as it is , it only gives one final result ; can anyone suggest a way whereas , say long lists of choices are given and I , or a user could select either one two or any number of results to be echoed . many thanks in... (3 Replies)
Discussion started by: V686
3 Replies

3. Shell Programming and Scripting

Number of arguments to array - Perl Script

I have the following proc. proc get_add {arg1 arg2 arg3 arg4 arg 5 .. ... arg N } { } i need to count the number of arguments and also i need those arguments stored in an array. please help out ---------- Post updated at 06:33 PM ---------- Previous update was at 05:30 PM ---------- ... (1 Reply)
Discussion started by: Syed Imran
1 Replies

4. Shell Programming and Scripting

Login, excute command, logout Script Help

Good Evening all, After spending the last week or so reading many posts I decided to register and join in. This is my first post on the forum so please forgive me as im new to this, Im after some help in throwing together a quick basic script without using expect to change the password on several... (4 Replies)
Discussion started by: mutley2202
4 Replies

5. Shell Programming and Scripting

Needed value after the last delimeter in a file with varying number of delimited columns

Hi All, My file has the records as below: aaa\bbb\c\dd\ee\ff\gg zz\vv\ww pp\oo\ii\uu How can I get the value after the last delimeter. My o/p: gg ww uu Thanks in Advance, (5 Replies)
Discussion started by: HemaV
5 Replies

6. Shell Programming and Scripting

Looking for help with script to assign all disk space to slice#0 on multiple disks of varying sizes

Hi Folks, I am trying to make a script to assign all diskspace to slice 0, on multiple sized disks. Since the disks are new they may need to be labelled also to avoid the error: Cannot get disk geometry Below is my code struggling with logic which doesn't seem to be producing the desired... (0 Replies)
Discussion started by: momin
0 Replies

7. Shell Programming and Scripting

How to call arguments with variable in a script??

Hello, I was wondering if it were possible to call arguments passed to a script using a variable. For example: sh script.sh yes no good bad x=$# while do echo (last argument, then second last etc until first argument) let x=($x-1) done should print out bad good no (4 Replies)
Discussion started by: VanK
4 Replies

8. Shell Programming and Scripting

Varying number of awk search strings

I've created an awk script that handles a varying number of search strings handed to it as command line parameters ($1 $2 etc). There may be 1, or 2 or 3 or more. A simplified version of the script is: awk -v TYP="$1 $2 $3 $4 $5 $6" ' BEGIN { CTYP = split (TYP,TYPP," ") } ... (2 Replies)
Discussion started by: CarlosNC
2 Replies

9. Shell Programming and Scripting

replacing a number with random variable inside shell script

Hi All. I need help for the below logic. I ve a file like following input file: NopTX(5) // should be remain same as input ----Nop(@100); //1 Nop(90); //2 --Nop(80); //3 @Nop(70); //4 --@Nop(60); //5 @Nop@(@50); //6 --Nop@( 40); ... (3 Replies)
Discussion started by: user_prady
3 Replies

10. Shell Programming and Scripting

is there any way to excute script every N seconds?

Hello i have script that show me stuff , i need to excute this script every N seconds , is there any way to do it with one liner ? ( mybe perl ) thanks (7 Replies)
Discussion started by: umen
7 Replies
Login or Register to Ask a Question