Can I create a function to return non-interger value in shell script?
for example,
function getcommand ()
{
echo "read command"
read command
echo $command
}
command=$(getcommand)
I tried to do something as above. The statement echo "read command" does not show up.
Normally, how can we create a function to return something except an integer?
#!/bin/ksh
concatenate_two_words()
{
echo "$1""$2"
}
word1="hi"
word2="there"
both=$(concatenate_two_words "$word1" "$word2")
echo "$both"
To return a string echo "the string" as the last line of the function and then call the function as a subprocess
with either ` ` backtics or $ ( )
another alternative is to define a "global" variable
funct_ret_value=""
function getcommand ()
{
echo "read command"
read command
funct_ret_value=$command
}
getcommand
echo $funct_ret_value
When you call a function in ` ` or $( ), the child process is spawned. Am I right?
Therefore, the statement echo "read command" does not print out to the shell I am running the script. How can I print it out to the running shell if I use the function that is called in ` ` or $( ). The echo statement is in that function.
Why are you echo - ing and then reading a value inside of a function that runs as a subprocess?
- The echo "read command" appears in the variable you are trying to receive in the calling process. It does not appear on the console.
Actually, it is nothing I want to do. I am starting to learn unix and I am trying many possible ways to make myself understood as much as possible. Therefore, this question came up when I compare a function in shell programming to other programming langauges.
Furthermore, the book I have been studying writes something about break:
while true
do
cmd=$(getcommand)
if [ "cmd" =quit ]
then
break
fi
done
See that made me think about how to write the getcommand function. So I put the echo-ing and read in a function and return a string.
Thanks for your answers anyway