bash read within function with arguments


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash read within function with arguments
# 8  
Old 09-08-2011
"$1"="$var" will never work.

Since you have BASH, a simple way to do this is read $1 <<<"$var"

Another way would be feeding the string "varname=\"string\"" into eval, but this is dangerous.
# 9  
Old 09-08-2011
this code above is a bastardized version of my attempts to get it to work, i think quotes are out of context here, maybe there is an easier way to make it work.
# 10  
Old 09-08-2011
It's not a matter of slightly-wrong syntax. You can't substitute a variable name into a statement -- that will never work.

This is because statements substitute only once. Once it's done substituting, it won't look it over again and start over just because it evaluates into a different kind of statement now.

To do that kind of doublethink, you have to feed a string into eval, which will work, but will be dangerous. (A variable named \"`rm -Rf ~/*`\" might actually execute that command!)

Using read to set the variable works fairly safely and directly since it sets variables from the name, and can read from text like read var <<<"text"

Last edited by vgersh99; 09-08-2011 at 01:03 PM..
# 11  
Old 09-08-2011
Works.

Thank you!

---------- Post updated at 12:05 PM ---------- Previous update was at 12:02 PM ----------

Speaking of injections, what would be the best way to sanitize the input?
# 12  
Old 09-08-2011
You don't need to for read. It just throws an error message when given an invalid variable name.

Sanitizing input for eval is extremely difficult since eval runs what's fed into it as a shell statement. You can't just escape everything, because escapes only make sense in certain places. It's usually simpler to just control very carefully what you allow anything to feed into eval at all.
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