Sponsored Content
Top Forums Shell Programming and Scripting bash read within function with arguments Post 302553444 by Corona688 on Wednesday 7th of September 2011 02:51:00 PM
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..
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
TREE(3) 						   BSD Library Functions Manual 						   TREE(3)

NAME
SPLAY_PROTOTYPE, SPLAY_GENERATE, SPLAY_ENTRY, SPLAY_HEAD, SPLAY_INITIALIZER, SPLAY_ROOT, SPLAY_EMPTY, SPLAY_NEXT, SPLAY_MIN, SPLAY_MAX, SPLAY_FIND, SPLAY_LEFT, SPLAY_RIGHT, SPLAY_FOREACH, SPLAY_INIT, SPLAY_INSERT, SPLAY_REMOVE, RB_PROTOTYPE, RB_PROTOTYPE_STATIC, RB_PROTOTYPE_INSERT, RB_PROTOTYPE_INSERT_COLOR, RB_PROTOTYPE_REMOVE, RB_PROTOTYPE_REMOVE_COLOR, RB_PROTOTYPE_FIND, RB_PROTOTYPE_NFIND, RB_PROTOTYPE_NEXT, RB_PROTOTYPE_PREV, RB_PROTOTYPE_MINMAX, RB_GENERATE, RB_GENERATE_STATIC, RB_GENERATE_INSERT, RB_GENERATE_INSERT_COLOR, RB_GENERATE_REMOVE, RB_GENERATE_REMOVE_COLOR, RB_GENERATE_FIND, RB_GENERATE_NFIND, RB_GENERATE_NEXT, RB_GENERATE_PREV, RB_GENERATE_MINMAX, RB_ENTRY, RB_HEAD, RB_INITIALIZER, RB_ROOT, RB_EMPTY, RB_NEXT, RB_PREV, RB_MIN, RB_MAX, RB_FIND, RB_NFIND, RB_LEFT, RB_RIGHT, RB_PARENT, RB_FOREACH, RB_FOREACH_FROM, RB_FOREACH_SAFE, RB_FOREACH_REVERSE, RB_FOREACH_REVERSE_FROM, RB_FOREACH_REVERSE_SAFE, RB_INIT, RB_INSERT, RB_REMOVE -- implementations of splay and red-black trees SYNOPSIS
#include <sys/tree.h> SPLAY_PROTOTYPE(NAME, TYPE, FIELD, CMP); SPLAY_GENERATE(NAME, TYPE, FIELD, CMP); SPLAY_ENTRY(TYPE); SPLAY_HEAD(HEADNAME, TYPE); struct TYPE * SPLAY_INITIALIZER(SPLAY_HEAD *head); SPLAY_ROOT(SPLAY_HEAD *head); bool SPLAY_EMPTY(SPLAY_HEAD *head); struct TYPE * SPLAY_NEXT(NAME, SPLAY_HEAD *head, struct TYPE *elm); struct TYPE * SPLAY_MIN(NAME, SPLAY_HEAD *head); struct TYPE * SPLAY_MAX(NAME, SPLAY_HEAD *head); struct TYPE * SPLAY_FIND(NAME, SPLAY_HEAD *head, struct TYPE *elm); struct TYPE * SPLAY_LEFT(struct TYPE *elm, SPLAY_ENTRY NAME); struct TYPE * SPLAY_RIGHT(struct TYPE *elm, SPLAY_ENTRY NAME); SPLAY_FOREACH(VARNAME, NAME, SPLAY_HEAD *head); void SPLAY_INIT(SPLAY_HEAD *head); struct TYPE * SPLAY_INSERT(NAME, SPLAY_HEAD *head, struct TYPE *elm); struct TYPE * SPLAY_REMOVE(NAME, SPLAY_HEAD *head, struct TYPE *elm); RB_PROTOTYPE(NAME, TYPE, FIELD, CMP); RB_PROTOTYPE_STATIC(NAME, TYPE, FIELD, CMP); RB_PROTOTYPE_INSERT(NAME, TYPE, ATTR); RB_PROTOTYPE_INSERT_COLOR(NAME, TYPE, ATTR); RB_PROTOTYPE_REMOVE(NAME, TYPE, ATTR); RB_PROTOTYPE_REMOVE_COLOR(NAME, TYPE, ATTR); RB_PROTOTYPE_FIND(NAME, TYPE, ATTR); RB_PROTOTYPE_NFIND(NAME, TYPE, ATTR); RB_PROTOTYPE_NEXT(NAME, TYPE, ATTR); RB_PROTOTYPE_PREV(NAME, TYPE, ATTR); RB_PROTOTYPE_MINMAX(NAME, TYPE, ATTR); RB_GENERATE(NAME, TYPE, FIELD, CMP); RB_GENERATE_STATIC(NAME, TYPE, FIELD, CMP); RB_GENERATE_INSERT(NAME, TYPE, FIELD, CMP, ATTR); RB_GENERATE_INSERT_COLOR(NAME, TYPE, FIELD, ATTR); RB_GENERATE_REMOVE(NAME, TYPE, FIELD, ATTR); RB_GENERATE_REMOVE_COLOR(NAME, TYPE, FIELD, ATTR); RB_GENERATE_FIND(NAME, TYPE, FIELD, CMP, ATTR); RB_GENERATE_NFIND(NAME, TYPE, FIELD, CMP, ATTR); RB_GENERATE_NEXT(NAME, TYPE, FIELD, ATTR); RB_GENERATE_PREV(NAME, TYPE, FIELD, ATTR); RB_GENERATE_MINMAX(NAME, TYPE, FIELD, ATTR); RB_ENTRY(TYPE); RB_HEAD(HEADNAME, TYPE); RB_INITIALIZER(RB_HEAD *head); struct TYPE * RB_ROOT(RB_HEAD *head); bool RB_EMPTY(RB_HEAD *head); struct TYPE * RB_NEXT(NAME, RB_HEAD *head, struct TYPE *elm); struct TYPE * RB_PREV(NAME, RB_HEAD *head, struct TYPE *elm); struct TYPE * RB_MIN(NAME, RB_HEAD *head); struct TYPE * RB_MAX(NAME, RB_HEAD *head); struct TYPE * RB_FIND(NAME, RB_HEAD *head, struct TYPE *elm); struct TYPE * RB_NFIND(NAME, RB_HEAD *head, struct TYPE *elm); struct TYPE * RB_LEFT(struct TYPE *elm, RB_ENTRY NAME); struct TYPE * RB_RIGHT(struct TYPE *elm, RB_ENTRY NAME); struct TYPE * RB_PARENT(struct TYPE *elm, RB_ENTRY NAME); RB_FOREACH(VARNAME, NAME, RB_HEAD *head); RB_FOREACH_FROM(VARNAME, NAME, POS_VARNAME); RB_FOREACH_SAFE(VARNAME, NAME, RB_HEAD *head, TEMP_VARNAME); RB_FOREACH_REVERSE(VARNAME, NAME, RB_HEAD *head); RB_FOREACH_REVERSE_FROM(VARNAME, NAME, POS_VARNAME); RB_FOREACH_REVERSE_SAFE(VARNAME, NAME, RB_HEAD *head, TEMP_VARNAME); void RB_INIT(RB_HEAD *head); struct TYPE * RB_INSERT(NAME, RB_HEAD *head, struct TYPE *elm); struct TYPE * RB_REMOVE(NAME, RB_HEAD *head, struct TYPE *elm); DESCRIPTION
These macros define data structures for different types of trees: splay trees and red-black trees. In the macro definitions, TYPE is the name tag of a user defined structure that must contain a field of type SPLAY_ENTRY, or RB_ENTRY, named ENTRYNAME. The argument HEADNAME is the name tag of a user defined structure that must be declared using the macros SPLAY_HEAD(), or RB_HEAD(). The argument NAME has to be a unique name prefix for every tree that is defined. The function prototypes are declared with SPLAY_PROTOTYPE(), RB_PROTOTYPE(), or RB_PROTOTYPE_STATIC(). The function bodies are generated with SPLAY_GENERATE(), RB_GENERATE(), or RB_GENERATE_STATIC(). See the examples below for further explanation of how these macros are used. SPLAY TREES
A splay tree is a self-organizing data structure. Every operation on the tree causes a splay to happen. The splay moves the requested node to the root of the tree and partly rebalances it. This has the benefit that request locality causes faster lookups as the requested nodes move to the top of the tree. On the other hand, every lookup causes memory writes. The Balance Theorem bounds the total access time for m operations and n inserts on an initially empty tree as O((m + n)lg n). The amortized cost for a sequence of m accesses to a splay tree is O(lg n). A splay tree is headed by a structure defined by the SPLAY_HEAD() macro. A structure is declared as follows: SPLAY_HEAD(HEADNAME, TYPE) head; where HEADNAME is the name of the structure to be defined, and struct TYPE is the type of the elements to be inserted into the tree. The SPLAY_ENTRY() macro declares a structure that allows elements to be connected in the tree. In order to use the functions that manipulate the tree structure, their prototypes need to be declared with the SPLAY_PROTOTYPE() macro, where NAME is a unique identifier for this particular tree. The TYPE argument is the type of the structure that is being managed by the tree. The FIELD argument is the name of the element defined by SPLAY_ENTRY(). The function bodies are generated with the SPLAY_GENERATE() macro. It takes the same arguments as the SPLAY_PROTOTYPE() macro, but should be used only once. Finally, the CMP argument is the name of a function used to compare tree nodes with each other. The function takes two arguments of type struct TYPE *. If the first argument is smaller than the second, the function returns a value smaller than zero. If they are equal, the function returns zero. Otherwise, it should return a value greater than zero. The compare function defines the order of the tree elements. The SPLAY_INIT() macro initializes the tree referenced by head. The splay tree can also be initialized statically by using the SPLAY_INITIALIZER() macro like this: SPLAY_HEAD(HEADNAME, TYPE) head = SPLAY_INITIALIZER(&head); The SPLAY_INSERT() macro inserts the new element elm into the tree. The SPLAY_REMOVE() macro removes the element elm from the tree pointed by head. The SPLAY_FIND() macro can be used to find a particular element in the tree. struct TYPE find, *res; find.key = 30; res = SPLAY_FIND(NAME, head, &find); The SPLAY_ROOT(), SPLAY_MIN(), SPLAY_MAX(), and SPLAY_NEXT() macros can be used to traverse the tree: for (np = SPLAY_MIN(NAME, &head); np != NULL; np = SPLAY_NEXT(NAME, &head, np)) Or, for simplicity, one can use the SPLAY_FOREACH() macro: SPLAY_FOREACH(np, NAME, head) The SPLAY_EMPTY() macro should be used to check whether a splay tree is empty. RED-BLACK TREES A red-black tree is a binary search tree with the node color as an extra attribute. It fulfills a set of conditions: 1. Every search path from the root to a leaf consists of the same number of black nodes. 2. Each red node (except for the root) has a black parent. 3. Each leaf node is black. Every operation on a red-black tree is bounded as O(lg n). The maximum height of a red-black tree is 2lg(n + 1). A red-black tree is headed by a structure defined by the RB_HEAD() macro. A structure is declared as follows: RB_HEAD(HEADNAME, TYPE) head; where HEADNAME is the name of the structure to be defined, and struct TYPE is the type of the elements to be inserted into the tree. The RB_ENTRY() macro declares a structure that allows elements to be connected in the tree. In order to use the functions that manipulate the tree structure, their prototypes need to be declared with the RB_PROTOTYPE() or RB_PROTOTYPE_STATIC() macro, where NAME is a unique identifier for this particular tree. The TYPE argument is the type of the structure that is being managed by the tree. The FIELD argument is the name of the element defined by RB_ENTRY(). Individual prototypes can be declared with RB_PROTOTYPE_INSERT(), RB_PROTOTYPE_INSERT_COLOR(), RB_PROTOTYPE_REMOVE(), RB_PROTOTYPE_REMOVE_COLOR(), RB_PROTOTYPE_FIND(), RB_PROTOTYPE_NFIND(), RB_PROTOTYPE_NEXT(), RB_PROTOTYPE_PREV(), and RB_PROTOTYPE_MINMAX() in case not all functions are required. The indi- vidual prototype macros expect NAME, TYPE, and ATTR arguments. The ATTR argument must be empty for global functions or static for static functions. The function bodies are generated with the RB_GENERATE() or RB_GENERATE_STATIC() macro. These macros take the same arguments as the RB_PROTOTYPE() and RB_PROTOTYPE_STATIC() macros, but should be used only once. As an alternative individual function bodies are generated with the RB_GENERATE_INSERT(), RB_GENERATE_INSERT_COLOR(), RB_GENERATE_REMOVE(), RB_GENERATE_REMOVE_COLOR(), RB_GENERATE_FIND(), RB_GENERATE_NFIND(), RB_GENERATE_NEXT(), RB_GENERATE_PREV(), and RB_GENERATE_MINMAX() macros. Finally, the CMP argument is the name of a function used to compare tree nodes with each other. The function takes two arguments of type struct TYPE *. If the first argument is smaller than the second, the function returns a value smaller than zero. If they are equal, the function returns zero. Otherwise, it should return a value greater than zero. The compare function defines the order of the tree elements. The RB_INIT() macro initializes the tree referenced by head. The red-black tree can also be initialized statically by using the RB_INITIALIZER() macro like this: RB_HEAD(HEADNAME, TYPE) head = RB_INITIALIZER(&head); The RB_INSERT() macro inserts the new element elm into the tree. The RB_REMOVE() macro removes the element elm from the tree pointed by head. The RB_FIND() and RB_NFIND() macros can be used to find a particular element in the tree. struct TYPE find, *res; find.key = 30; res = RB_FIND(NAME, head, &find); The RB_ROOT(), RB_MIN(), RB_MAX(), RB_NEXT(), and RB_PREV() macros can be used to traverse the tree: for (np = RB_MIN(NAME, &head); np != NULL; np = RB_NEXT(NAME, &head, np)) Or, for simplicity, one can use the RB_FOREACH() or RB_FOREACH_REVERSE() macro: RB_FOREACH(np, NAME, head) The macros RB_FOREACH_SAFE() and RB_FOREACH_REVERSE_SAFE() traverse the tree referenced by head in a forward or reverse direction respec- tively, assigning each element in turn to np. However, unlike their unsafe counterparts, they permit both the removal of np as well as free- ing it from within the loop safely without interfering with the traversal. Both RB_FOREACH_FROM() and RB_FOREACH_REVERSE_FROM() may be used to continue an interrupted traversal in a forward or reverse direction respectively. The head pointer is not required. The pointer to the node from where to resume the traversal should be passed as their last argument, and will be overwritten to provide safe traversal. The RB_EMPTY() macro should be used to check whether a red-black tree is empty. NOTES
Trying to free a tree in the following way is a common error: SPLAY_FOREACH(var, NAME, head) { SPLAY_REMOVE(NAME, head, var); free(var); } free(head); Since var is freed, the FOREACH() macro refers to a pointer that may have been reallocated already. Proper code needs a second variable. for (var = SPLAY_MIN(NAME, head); var != NULL; var = nxt) { nxt = SPLAY_NEXT(NAME, head, var); SPLAY_REMOVE(NAME, head, var); free(var); } Both RB_INSERT() and SPLAY_INSERT() return NULL if the element was inserted in the tree successfully, otherwise they return a pointer to the element with the colliding key. Accordingly, RB_REMOVE() and SPLAY_REMOVE() return the pointer to the removed element otherwise they return NULL to indicate an error. SEE ALSO
queue(3) AUTHORS
The author of the tree macros is Niels Provos. BSD
January 24, 2015 BSD
All times are GMT -4. The time now is 10:59 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy