Reputation: 3
Type()
{
if [ -d $1 ]
then
return 1
elif [ -e $1 ]
then
return 2
else
return 0
fi
}
Types()
{
local arg1 arg2
for arg1 in $@
do
arg2=$(Type $arg1)
if [ arg2 -eq 1 ]
then
echo "$arg1 est un répertoire."
elif [ arg2 -eq 2 ]
then
echo "$arg1 n est pas un répertoire."
else
echo "$arg1 ne correspond à aucune entrée du répertoire."
fi
done
}
I don't know how can I use the function 'Type' in 'Types'. I tried "arg2=$(Type $arg1)" bur it doesn't seem to work. What's the correct syntax please ?
Upvotes: 0
Views: 62
Reputation: 32082
return 1
Bash functions often return values through their standard output. For example, you could use something like this instead:
exec echo 1 # sends 1 to the standard output and then ends the function
Alternatively, you could return an integer between 0 and 255 as an exit code (as you are trying to do). If you choose to do so, you need to do:
Type $arg1
arg2=$? # obtains exit code of last command/function executed
However, if you need to return an array, you must use a global variable. You may want to refer to the Complex Functions and Function Complexities section of the Advanced Bash-Scripting Guide for examples of this method.
Upvotes: 1
Reputation: 11571
If you want to use the function with $(Type ...)
, then you can change the return ...
statements to echo ...
(not exec echo ...
, which does something else). If you want to keep the return ...
statements in Type()
, then you need to do something like Type ...; arg2=$?
to test the return code from Type()
.
Upvotes: 1