What does "-?" mean in bash script - linux

I have this script I am looking at, learning scripting but I cannot figure out what this line means:
if [[ $1 = "-?" ]]
I understand the $1 is first argument, but the after the equals I cannot figure it out the -?.h
if [[ $1 = "-?" ]]
then
echo "Use is: 235.sh <username>"
exit 1

I think it would be checking to see if the first argument is a string equal to -?

You can check this by running:
# [[ "a" = "-?" ]] && echo true
# [[ "-a" = "-?" ]] && echo true
# [[ "a-?" = "-?" ]] && echo true
# [[ "-?" = "-?" ]] && echo true
true
I would guess that is comparing something to the string "-?".

Related

How to compare number with string by -gt in shell programming

I don't know if exist a var $b ,make the value of the expression :
[[ "$a" -gt "$b" ]] && [[ "$b" -gt 88888888 ]]
is true?...
It's not terribly well documented (IIRC), but -gt (and the other integer comparison operators) forces the comparison to be made in an arithmetic context, which means an empty value of $b is treated the same as 0.
$ [[ 3 -gt $undefined ]] && echo true
true
$ [[ -3 -lt $undefined ]] && echo true
true
$ [[ 0 -eq $undefined ]] && echo true
true
This is more clearly seen using the fact that strings are recursively expanded as variables until an integer is found as well:
$ foo=bar
$ bar=3
$ [[ 3 -eq bar ]] && echo true
true
$ [[ 3 == bar ]] || echo false
false

Shell script if condition not evaluated for a help info display for the user

I had written a help menu for reference about the usage of a shell script my_script.sh
echo $'\n\n'
echo $(printf '=%.0s' {1..100})
printf ' %.0s' {1..40}
echo "Welcome"
echo $(printf '=%.0s' {1..100})
echo $'\n'
arg=$1
echo "Input : $arg"
echo
if [[ arg -eq "-h" ]] || [[ arg -eq "-H" ]] || [[ arg -eq "-help" ]] || [[ arg -eq "-Help" ]] || [[ arg -eq "--h" ]] || [[ arg -eq "--H" ]] || [[ arg -eq "--help" ]] || [[ arg -eq "--Help" ]]; then
echo "Help menu requested...."
echo $'\n\n'
echo $(printf '~%.0s' {1..100})
printf ' %.0s' {1..43}
echo "Help Menu"
echo $(printf '~%.0s' {1..100})
echo $'\n'
exit 0
else
echo "Executing a program...."
./another_script.sh
fi
When I execute `myscript.sh -h' (or any of the '-' prefixed option), it goes to the if condition, but any other argument doesn't. What am I doing wrong here? I'm new to bash scripts.
Two simple problems with your if:
-eq is for integer comparison, = or == for strings
Use $arg in your if (instead of arg).
But: I would recommend using getopts instead of string comparison. This would make the part more robust, taking care of different ordering of parameters, or when one letter parameters are combined into a single argument.
Unfortunately I do not know the exact reason why your code does not work, but I can offer you a quick fix: You can write "==" instead of "-eq" and prefix your variable "arg" with a dollar sign. Then your script should work fine.
Working example (GNU bash 4.4.19):
arg=$1
if [[ $arg == "-h" ]] || [[ $arg == "-H" ]]; then
echo "Help!"
else
echo "Stop!"
fi
the -eq operation is only used for comparing numbers.
To compare strings uses the operation =
You forgot $ sign for variables arg in Bash, should be $arg
When we use variables in Bash, we should better use double quote.
use #() for multiple strings comparison.
so the if conditions [[ arg -eq "-h" ]] should be [[ "$arg" = "-h" ]]
When comparing a variable with multiple strings, we can use [[ "$arg" = #(-h|-H|--help|--HELP|--h|--H|-help|--HELP) ]].
if [[ "$arg" = #(-h|-H|--help|--HELP|--h|--H|-help|--HELP) ]]; then
echo "Help menu requested...."
echo $'\n\n'
echo $(printf '~%.0s' {1..100})
printf ' %.0s' {1..43}
echo "Help Menu"
echo $(printf '~%.0s' {1..100})
echo $'\n'
exit 0
else
echo "Executing a program...."
./another_script.sh
fi
In addition, we can use boxes(boxes - Command line ASCII boxes unlimited!
) to generate a comment box
cat <<EOF | boxes -a c -d shell -p a5 -s 30x9
HELP MENU
bla bla
EOF
output:
########################################
# #
# #
# HELP MENU #
# #
# bla bla #
# #
# #
########################################

How to check if a string contains brackets ")("?

I'm trying to check if an input string contains parentheses, which are: ()[]{}.
I wrote the following code:
#!/bin/bash
str="$1"
if [ -z "$str" ]; then
echo "Usage: $(basename $0) string"
exit 1
fi
if [[ "$str" == *['\{''}''\[''\]''('')']* ]];
then
echo "True"
else
echo "False"
fi
If the string contains any of these: []{} then the output is correct but if the string contains () then I get an error:
-bash: syntax error near unexpected token `('
These are the things I've tried so far:
*['\(''\)']*
*['()']*
*[()]*
Any idea how it should be written?
Edit #1:
[root#centolel ~]# date
Tue Nov 3 18:39:37 IST 2015
[root#centolel ~]# bash -x asaf.sh {
+ str='{'
+ '[' -z '{' ']'
+ [[ { == *[{}\[\]\(\)]* ]]
+ echo True
True
[root#centolel ~]# bash -x asaf.sh (
-bash: syntax error near unexpected token `('
[root#centolel ~]#
You can use this glob pattern with () and [] escaped inside [...]:
[[ $str == *[{}\(\)\[\]]* ]] && echo "yes" || echo "no"
Testing:
str='abc[def' && [[ $str == *[{}\(\)\[\]]* ]] && echo "yes" || echo "no"
yes
str='abc}def' && [[ $str == *[{}\(\)\[\]]* ]] && echo "yes" || echo "no"
yes
str='abc[def' && [[ $str == *[{}\(\)\[\]]* ]] && echo "yes" || echo "no"
yes
str='abc(def' && [[ $str == *[{}\(\)\[\]]* ]] && echo "yes" || echo "no"
yes
str='abc)def' && [[ $str == *[{}\(\)\[\]]* ]] && echo "yes" || echo "no"
yes
str='abc{}def' && [[ $str == *[{}\(\)\[\]]* ]] && echo "yes" || echo "no"
yes
str='abc}def' && [[ $str == *[{}\(\)\[\]]* ]] && echo "yes" || echo "no"
yes
str='abcdef' && [[ $str == *[{}\(\)\[\]]* ]] && echo "yes" || echo "no"
no

How do I test if a variable is a string in bash?

I tried the following but without success
[root#OBAMA~]# bash
[root#OBAMA~]# a=HelloWorld
[root#OBAMA~]# [[ $a == [A-Za-z] ]] && echo "YES ITS STRING"
( the command not prints anything )
[root#OBAMA~]# [[ $a == [A-Z][a-z] ]] && echo "YES ITS STRING"
( the command not prints anything )
Change your command lke below.
$ [[ $a =~ [A-Za-z]+ ]] && echo "YES ITS STRING"
YES ITS STRING
Use =~ operator to test an input string against a regex.
Add + next to the character class, so that it would repeat the previous pattern or token one or more times. Here it's unnecessary.
Add anchors , in-order to do an exact string match. [[ $a =~ [A-Za-z] ]] && echo "YES ITS STRING" alone will print the string YES ITS STRING because the variable a contains atleast an alphabet.
$ a="HelloWorld"
$ [[ $a =~ ^[A-Za-z]+$ ]] && echo "YES ITS STRING"
YES ITS STRING
$ a="Hello World"
$ [[ $a =~ ^[A-Za-z]+$ ]] && echo "YES ITS STRING"
$
how do you define "a string"
[[ -n $a ]] && echo variable a is not empty
[[ $a == *[[:alpha:]]* ]] && echo variable a contains a letter
shopt -s extglob failglob
[[ $a == +([[:alpha:]]) ]] && echo variable a only has letters
Your glob expressions are not matching because your checking that your variable contains only 1 character or 2 characters.

Bash - Check if the string starts with a predefined string (substring)

I have a variable $projectName how can i check if it starts with this string 'testProject'
You can use this check in BASH:
[[ "$projectName" == "testProject"* ]]
You can for example use:
[[ "$projectName" =~ ^testProject ]] && echo "yes"
^
beginning of line
Test
$ var="hello"
$ [[ "$var" =~ ^he ]] && echo "yes" || echo "no"
yes
$ var="ahello"
$ [[ "$var" =~ ^he ]] && echo "yes" || echo "no"
no

Resources