Reputation: 13
below is my script to run some sub commands. When i am running the script with ./test.sh sub_cmd $APP
it is running fine. But when i ran it like test sub_cmd $APP
it throwing an error.
I want to run the script without using ./
error :- test: ing: unary operator expected
#!/bin/bash
ProgName=$(basename $0)
APP=${2?Need chart name}
sub_help(){
echo "Usage: $ProgName <subcommand> [options]\n"
}
sub_dep(){
echo "Running 'dep' command."
vi some_path/${APP}/templates/deployment.yaml
}
sub_ing(){
echo "Running 'ing' command."
vi some_path/${APP}/templates/ingress.yaml
}
sub_svc(){
echo "Running 'ing' command."
vi some_path/${APP}/templates/service.yaml
}
subcommand=$1
case $subcommand in
"" | "-h" | "--help")
sub_help
;;
*)
shift
sub_${subcommand} $@
if [ $? = 127 ]; then
echo "Error: '$subcommand' is not a known subcommand." >&2
echo " Run '$ProgName --help' for a list of known subcommands." >&2
exit 1
fi
;;
esac
Upvotes: 1
Views: 163
Reputation: 585
You need to run "test.sh" not "test".
test sub_cmd $APP
What you are actually running is the test command which lives in one of the lib directories on your path.
If you type
which test
you can see which command/script/program that will be run.
When you prefix the command with a relative path (such as ./) or an absolute path (ie.e starting with a /) then bash will look in that specific folder for the command. If you omit the path then it will search the directories listed in the environment variable $PATH and execute the first one it comes across. The which command does the same thing, but just lists the script/program instead of executing it.
Upvotes: 2