Shell Script call a function with a variable? - function

Hi I'm creating a shell script.
and an example code looks like
#!/bin/bash
test_func(){
{
echo "It works!"
}
funcion_name = "test_func"
I want to somehow be able to call test_func() using the variable "function_name"
I know that's possible in php using call_user_func($function_name) or by sying $function_name()
is this also possible in the shell scripting?
Huge appreciation for the help! :)

You want the bash built-in eval. From man bash:
eval [arg ...]
The args are read and concatenated together into a single command. This command is then read and executed by the shell, and its exit status is returned as the value of eval. If there are no
args, or only null arguments, eval returns 0.
You can also accomplish it with simple variable substitution, as in
#!/bin/bash
test_func() {
echo "It works!"
}
function_name="test_func"
$function_name

#!/bin/bash
test_func() {
echo "It works!"
}
function_name="test_func"
eval ${function_name}

Related

TCL exec output delayed

I have a c-shell script that hypothetically does a ton of things and gives little messages along the way.
Example (test.csh):
#!/bin/csh
echo "hello world."
sleep 10
echo "hello again."
sleep 10
I am calling the script from tclsh.
exec /bin/csh test.csh
All the output is held back until end of the script. That is not desired. Desired outcome is to get output from the script as it happens. How would I modify the TCL call to achieve the desired outcome?
Thanks.
One approach could be as follows:
exec >#stdout 2>#stderr /bin/csh test.csh
Check out the corresponding sections of the exec manpage, e.g.: >#
It turns out that your question has been raised before: How can I run a csh script from a tcl script?

How to get variable from other while loop?

#!/bin/bash
read()
{
count=0
cat localfile |
while read line
do
FILE[$((count += 1))]="$line"
done
}
read
for((i=0;i<${#FILE[#]});i++)
do
echo ${FILE[i]}
done
The result of echo is whole blank. Is there any way to get the FILE array?
You posted this under ash, the Almquist shell, but you are using bash, the Bourne-Again SHell.
One problem is the pipe. When you run a pipe in bash, each side runs in its own sub-shell, and any variables are local to it. The correct mechanism is to redirect into the loop.
Another problem is that your function is called read, which is a shell-builtin (you use it!). A good idea is to use a naming convention, like an f_ prefix, so you don't get these name collisions.
Another issue you have is that the syntax of your second for loop is wrong in several ways. Here is a corrected version:
#!/bin/bash
f_read()
{
count=0
while read line
do
FILE[$((count += 1))]="$line"
done < localfile
}
f_read
for ((i=0;i<${#FILE[#]};i++))
do
echo ${FILE[i]}
done

how to make a shell script function able to either specify arguments in command line or get them from a pipe?

For example, I want to write a function called fooFun, which will do some process on a PDF file. I'd like to make it able to run on both of the ways as following:
$ fooFun foo.pdf
$ ls *.pdf | fooFun
Any ideas? Thanks.
I don't think you can easily do this with a shell function. A better idea is to make it a script, let it take command line arguments, and achieve the second style with xargs:
ls *.pdf | xargs fooFun
I agree with #larsmans, better to stick with passing arguments as parameters. However, here's how to achieve what you're asking:
foofun() {
local args arg
if [[ $# -eq 0 ]]; then
args=()
# consume stdin
while IFS= read -r arg; do args+=($arg); done
else
args=("$#")
fi
# do something with "${args[#]}"
}

How do I use the eval statement in tcl

So basically in my tcl script I generate a line of tcl code that I have to execute( ie I come up with this instruction during runtime). Once I have the tcl instruction say for example puts "hello world $test_var". How do I execute this using tcl?
Do I use the eval command or what?
The eval command is a reasonable way of doing this, but you might want to consider using catch instead, since that will trap any problems found during the evaluation of the generated code, for example like this:
# Generate the code somehow
set variable {puts "hello word $test_var"}
# Execute and trap problems
if {[catch $variable resultMsg]} {
# An error (or other exception) happened
puts stderr "BOOM, Error! $resultMsg"
}
Instead of using [eval] which works perfectly well, in newer versions of Tcl you can use the {*} syntax.
For example:
set cmd "puts"
lappend cmd "abcd ef"
{*}$cmd
Note that it's a good habit to use list commands to build up your command so you wont run into quoting issues.
I'm afraid you don't have another reasonable choice than to use the eval command, e.g.:
set variable {puts "hello world $test_var"}
eval $variable

ksh alias or function that does "setTermName blah"

I have the following line:
echo -ne "\033]0;blah\007"
that correctly sets the term name to blah. But if I place that line within a function, as in:
setTermName()
{
echo -ne "\033]0;blah\007"
}
it doesn't work anymore. I guess escape sequences are not treated correctly within the function. So my question could be reformulated as: How do you use escape sequences within a function?
I only want to be able to do setTermName foo from command line.
You invoke that echo command from interactive ksh also? Are you sure it understands -ne? It's not standard. Maybe use printf.
And you can try to use alias instead.
UPD: I've checked with AIX ksh, the following function worked:
set_tn()
{
printf "\033]0;$1\007"
}