bash read within function with arguments


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash read within function with arguments
# 1  
Old 09-07-2011
Error bash read within function with arguments

I have trouble getting this logic to work

Code:
#!/bin/bash
function assign_var(){
while [ -z $1 ]
do
        read -p "$2 :" $3
done
}
assign_var '$IPADDRESS' ipaddress IPADDRESS

Basicly, i want to make sure that entry is made (i can add more sophisticated checks later), but the idea is to recycle the same function "assign_var" many times over.


i.e.

Code:
assign_var '$IPADDRESS' ipaddress IPADDRESS
assign_var '$NETMASK' netmask NETMASK
assign_var '$NGATEWAY' gateway GATEWAY
...

And needless to say, the variable must be global and accessible later.

Any help is appreciated.

Thanks
ilya
# 2  
Old 09-07-2011
$1 is never -z because it's the literal string '$IPADDRESS' or whatever. You can't do "pointer" like variables that way. You can do VARNAME="QWERTY" ; echo ${!VARNAME} to get the contents of ${QWERTY} though, so, how about:

Code:
#!/bin/bash

function read_var # VARNAME prompt
{
        while [ -z "${!1}" ]
        do
                read -p "$2: " $1
        done
}

exec 5<varlist

while read -u 5 VARNAME PROMPT
do
        read_var "$VARNAME" "$PROMPT"
done

exec 5<&-

so you can reuse your code reuse Smilie Just add more data lines to varlist instead of repeating read_var over and over.

It will never prompt for variables that're already set, you should know.

Last edited by Corona688; 09-07-2011 at 03:59 PM..
# 3  
Old 09-07-2011
wont work, goes into infinite loop until ctrl-c.

thanks
# 4  
Old 09-07-2011
It doesn't, not the code I gave you at least.
Code:
$ cat varlist
IPADDRESS       ipaddress
GATEWAY         gateway
$ cat readvar.sh
#!/bin/bash

function read_var # VARNAME prompt
{
        while [ -z "${!1}" ]
        do
                read -p "$2: " $1
        done
}

exec 5<varlist

while read -u 5 VARNAME PROMPT
do
        read_var "$VARNAME" "$PROMPT"
done

exec 5<&-

$ ./readvar.sh
ipaddress: 1234
gateway: asdf
$

Show us the code you ran and how you ran it, word for word, letter for letter, keystroke for keystroke.
# 5  
Old 09-07-2011
Corona688

You are correct and my mistake, i got the first email at 2:51pm and used the sample i saw in the email.

This version does work but adds a minor over head - would you know of a way to get my sample code to work. The idea is to keep it simple using functions and recycle the same function over and over again that inturn sets up variables for each argument passed into a function.

Your fast response and help is appreciated

Thanks
ilya
# 6  
Old 09-07-2011
Look at my code a third time. It does exactly what you want. Just ditch the loop if you don't want it. read_var IPADDRESS "type an IP address"

You might change your mind about your clever recycling of code once you get bored copy-pasting it though Smilie

Last edited by Corona688; 09-07-2011 at 07:03 PM..
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 09-08-2011
Hi Corona,

Thanks for the help, i got it to work and decided to take it step further. I'm hoping you would know what i'm doing wrong.

I would like for the user to be prompted, but if value for variable exists, display it in brackets and if user presses enter without modifying the string, the same value is used for that variable. However, in my attempt to headlock $1=$var within function and if/else statement fail with "command not found".


Output
IPADDRESS: [testhost1]
./main.sh: line 23: DHCPIPADDRESS=testhost1: command not found


My code (i begin adding tempvars in attempt to debug and resolve the issue, but havenot had much luck)

Code:
function settings_verify_entry(){
        if [ ! -z "${!1}" ]
        then
                        tempvar3="${!1}"
                        read -p "$2: ["${!1}"] " $1
                        tempvar=$1
                        if [ -z "${!tempvar}" ]
                                then
                                "$1"="$tempvar3"
                        fi
        else
                        while [ -z "${!1}" ]
                                do
                                       read -p "$2: " $1
                                done
        fi
}


Also, i'd like to fill the gap in this area, what is this concept called and where would i find more details on it? My basic google search did not yield any results, but i'm guessing my keywords arent correct.

Once again thanks

-ilya
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Call same function using 2 different arguments

I have a script that uses 2 arguments. I want to call the function part within this script using these same arguments. Below is what I came up with below script so far, any guidance would be helpful. Thank you! cat backup.sh #!/bin/bash function usage { echo "USAGE: $(basename $0)... (6 Replies)
Discussion started by: mbak
6 Replies

2. Shell Programming and Scripting

Bash function failing with [: too many arguments

I'm reading Wicked Cool Shell Scripts. For some reason, the function pasted in below gives the error: ./inpath2: line 10: in_path() 4 { 5 cmd=$1 ourpath=$2 result=1 6 oldIFS=$IFS IFS=":" 7 8 for directory in "$ourpath" 9 do 10 if ; then 11 result=0 12 fi... (9 Replies)
Discussion started by: jakeroberts
9 Replies

3. UNIX for Advanced & Expert Users

[BASH] Read pipe of unkown number of arguments?

Heays So i have that script to which i'd like to pipe (rather than just regular arguments) some data from another virtual output command. Simplified: echo * | script.sh When i know how many args i expect, i can handle this simple by: && \ read ONE TWO && \ set ONE TWO echo "$1 : $2... (7 Replies)
Discussion started by: sea
7 Replies

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

5. UNIX for Dummies Questions & Answers

Invoke Function WithOut Non-Positional Arguments

Hello Everyone, Is there a way i can pass the arguments as parameters or variables instead of positional arguments to a function, below i am calling the function defined in a script. Call: notify "Error While Generating The List File: ${GEN_PARAM_LIST9} For Feed Data Validation Errors In... (1 Reply)
Discussion started by: Ariean
1 Replies

6. Shell Programming and Scripting

Need to call a function with arguments

I need to call a function within a code with $database and $ service as the arguments How do I proceed ? and how would a function be defined and these two arguments would be used inside the function? calc_pref_avail $database $service Best regards, Vishal (7 Replies)
Discussion started by: Vishal_dba
7 Replies

7. Shell Programming and Scripting

Help with grep and read function in a BASH Script

I'm putting together a script that will search my mail archives for emails that meet certain criteria and output the files to a text file. I can manually cat that text file and pipe it into sendmail and it will work (i.e. cat /pathtofile/foo.txt | sendmail -t me@company.com) My script sends... (7 Replies)
Discussion started by: binary-ninja
7 Replies

8. UNIX for Advanced & Expert Users

Function not called when no arguments is passed

Hi Guys, I am trying to pass arguments to the script i am wrinting. When no argument is passed or wrong argument is passed, the script needs to output the way it needs to be called and exit. Currently, when no arguments is passed, it is not getting exited but goes on assuming those... (3 Replies)
Discussion started by: mac4rfree
3 Replies

9. Shell Programming and Scripting

cat arguments to a function

Hi, I've a logging function in bourne shell, flog() which logs the first argument passed to it. How can I pass arguments to this function from a file, like cat filename | sed '...filtering...' | flog or cat filename | sed '...filtering...' | xargs flog Which did not work, after which... (3 Replies)
Discussion started by: Random_Net
3 Replies

10. Shell Programming and Scripting

count no of arguments passed to a function

hi i have a function abc { //from this function i am passing args to antoher function like def a b c j k l } now i want to count the no of args coming to def() function and iterate over those values is there any way to do this one please help (2 Replies)
Discussion started by: satish@123
2 Replies
Login or Register to Ask a Question