Can anyone enlighten me on this one?Code:#!/bin/bash ask() { $1 } ask "echo This is not working >> 2.txt"
The echo command works but the re-direction to the text file 2.txt does not work
Can someone explain why this is the case.
Can anyone enlighten me on this one?Code:#!/bin/bash ask() { $1 } ask "echo This is not working >> 2.txt"
The echo command works but the re-direction to the text file 2.txt does not work
Can someone explain why this is the case.
Try with this
Code:#!/bin/bash ask() { $1 } ask "echo This is not working" >> 2.txt
try:
ask()
{
$1
}
ask "echo This is not working" >> 2.txt
ETA: TeoBigusGeekus, you beat me by a minute
What do you want to do? Maybe you can use the read command of bash to assign your input to a variable. Test making the file echoer of the following three lines
and run it withCode:#! /bin/bash echo "Input line of text and press Enter" read txt echo "Echoing $txt"Code:bash ./echoer
The Problem that i now have is that if i want a second command/line of text that I want to pass to the function for example "echo hello" redirects its output to the 2.txt file as-well
This is
Code:#!/bin/bash ask() { $1 $2 } ask "echo This is not working" > 2.txt "echo hello"
that works how I'd expect it to.
Can you clarify what you're trying to so?
#!/bin/bash
ask()
{
$2 > $1
$3
}
ask "2.txt" "echo This is not working" "echo hello"
This prints "This is not working" to file and hello to the console ... is that what you're after?
iMetaphyisicz ...
I think you are 're-using' my function without understanding 'function' and its use.
In your example the "function" is whatever is passed as parameter "${1}" to the script.Code:#!/bin/bash function ask() { echo -n "$@" '[y/n] ' ; read ans case "$ans" in y*|Y*) return 0 ;; *) return 1 ;; esac } ask "Do you understand functions?" && echo "YES" > yesweunderstandfunctions.txt
Not the best way to provide a function ... it might help if you explain what your trying to do. The following perhaps?Code:% cat param.sh #!/bin/bash echo "Parameter \${1} is ${1}" % param.sh yes Parameter ${1} is yes % param.sh gobbldygook Parameter ${1} is gobbldygook
Just so you understand, functions can do other things other than 'ask' questions.Code:#!/bin/bash if [[ -z ${@} ]]; then echo "No question provided." echo "Usage: $(basename ${0}) question" exit fi function ask() { echo -n "$@" '[y/n] ' ; read ans case "$ans" in y*|Y*) return 0 ;; *) return 1 ;; esac } ask "${@}" && echo "${@}=YES" > answer.txt
With this function 'nd newdir' will make a directory 'newdir' and 'cd newdir'.Code:nd () { test -d "${1}" || mkdir -p "${1}" && cd "${1}" }
Hope that helps clarify things ... khay
Last edited by Khayyam; February 26th, 2012 at 05:55 PM. Reason: changed "${1}" to "${@}"
Thanks Khayyam.
Essentially I am trying to reuse the ask function to do different things depending on the situation, in some instances I want to append to a log file, in other instances I want to call another function depending on user response.
In the code below rdx would be another function that the script calls
Now using your code with the && will add text to the yesweunderstandfunctions.txt if the user says yes because the function was successfully completed, but if the user says no it will not, I would also have to capture this information to a text file.Code:ask() { local i=0 for i in {1..10} do echo -n "$1" read ans ans=$(echo $ans | tr '[A-Z]' '[a-z]') case $ans in y) $2 return 0 ;; n) $3 return 0 ;; *) echo "Please enter y or n" local j=$((10-$i)) echo "If neither y/n is pressed after $j attempts it will be assumed that $3" esac done echo $3 } ask "Please Enter an RDX Cartridge if formatting is required, is the RDX cartridge inserted (y/n)?" "rdx" "RDX Cartridge formatting is not required" ask "echo Has this script been useful" "echo Very Useful >> feedback.txt" "echo Try again Buddy >> feedback.txt"
That's why I have used the "echo Very Useful >> feedback.txt"
Note, the code I have written above may not work as it was a rough example, as I ammended it from the script I'm currently writting. However I do not have that script on the computer I'm currently on.Code:ask "Do you understand functions?" && echo "YES" > yesweunderstandfunctions.txt
Bookmarks