Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Check which flag is used more to get the version of the command ( -version or --version ). Began this after being totally annoyed that java --version doesn't work.
#!/bin/bash
total_commands=0
count_one_dash=0
count_two_dash=0
commands_only_one_dash=0
for command in bash gcc java python perl gunzip unzip less cat vi nano pico ls who find locate rm cp
do
((total_commands++))
eval "$command -version" > /dev/null 2>&1
ret_code=$?
accepts_one_dash=$(expr $ret_code == 0)
if [[ $accepts_one_dash == 1 ]]
then
((count_one_dash++))
fi
eval "$command --version" > /dev/null 2>&1
ret_code=$?
accepts_two_dashes=$(expr $ret_code == 0)
if [[ $accepts_two_dashes == 1 ]]
then
((count_two_dash++))
fi
if [[ $accepts_one_dash == 1 && $accepts_two_dashes != 1 ]]
then
echo "hello $command , why do you only support -version?"
fi
done
echo "Number of total commands checked : $total_commands"
echo "Commands that use -version : $count_one_dash"
echo "Commands that use --version : $count_two_dash"
hello java , why do you only support -version?
hello unzip , why do you only support -version?
Number of total commands checked : 18
Commands that use -version : 6
Commands that use --version : 16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment