0

I need to check if a given string in a bash script is a command. In other words: I need to check if that String is a filename in the /bin directory (Only /bin).

I tried

echo "Write a bash command: "

read -r var2

if [[ -z (find /bin -name $var2) ]]
    then echo "That's not a command" && exit 1

fi

But it didn't work.

Ideas?

EDIT: Solved. As amdixon suggested I changed (find /bin -name $var2) for $(find /bin -name $var2).

Thanks dude.

2
  • 2
    change (find /bin -name $var2) to $(find /bin -name $var2) Commented Sep 24, 2015 at 9:34
  • which $var2 will tell you if the command is found, and where. Commented Feb 28, 2020 at 16:37

3 Answers 3

1

Depending on your actual requirements, it can be easier than that:

if ! [ -x /bin/"$var2" ]
then
    echo "That's not a command" && exit 1
fi

[ is short for the test command and with the -x argument, it will return 0 (true) if the given file is executable by you. Note that this will exclude commands that are executable by other users only because you have insufficient permissions.

If you use the -f argument instead, [ will test for any file in the /bin directory, whether it is executable or not (of course usually all of them are):

if ! [ -f /bin/"$var2" ]
then
    echo "That's not a command" && exit 1
fi

If you need to make sure that the file is executable (even if it may not be executable by you), see this question for a solution using file.

Type help test on the command line to read more about possible arguments for [.

Sign up to request clarification or add additional context in comments.

2 Comments

What -f and -x do?
@Argos: I'll add a few lines.
0
echo "Write a bash command: "

read -r var2

if [ ! -f /bin/"$var2" ]
    then echo "That's not a command" && exit 1
fi

is the natural way to do this in bash. [ expr ] is the shorthand for the builtin test command. Type man builtins and scroll until you find test for a complete description of what testcan do for you. For instance, if you prefer testing simultaneously if the file exists and is executable, you can replace:

[ ! -f /bin/"$var2" ]

by:

[ ! -x /bin/"$var2" ]

Comments

0

I need to check if a given string in a bash script is a command.

vs

I need to check if that String is a filename in the /bin directory (Only /bin).

This is by no means the same. I guess you refer to well-known "shell commands" and in this case, There are three reasons you might be on the wrong path:

  1. The root hierarchy is for everything needed to boot up the system. This might include what you consider "commands", but other stuff as well (like e.g. lvm or cryptsetup)
  2. For the same reason, binaries you would consider "commands" might be missing from /bin, living in /usr/bin instead.
  3. Shells have "builtin" commands and there is no guarantee you will find them as separate binaries at all.

Given all that, if you still want to look for executables in /bin, there is really no reason to use find at all. test (or the abbreviated version just writing brackets) will be enough, like if [ -x /bin/$var2 ]; then ...

2 Comments

Don't think this is what the question was asking.
The question is asking about two different requirements (assumed to be the same, which I try to clarify here) and presents a needlessly complicated solution to the second requirement (find really is not necessary here). But of course you could just point out how it is used wrongly ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.