shell script to test condition on passed string
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
condition
string either start with upper or lower alphabets or underscore
for example _abc or xyz or Abc
string may contain digits after first character
like _f9 or f10 or car20 or top10cars
string should never contain special characters like ! @ # $ % ^ & * ( ) + - =
here my tiny script
if [[ $1 =~ ^[A-Za-z_]+$ ]]; then
echo it matches
else
echo does_not match
fi
when i pass argument like only & or * or ( , script don't work , sometimes cursor never comes backs , i need to press ctrl+c
for example ./script.sh (abc
./script.sh &&
./script.sh &abc
whats wrong with script
bash shell-script
add a comment |Â
up vote
0
down vote
favorite
condition
string either start with upper or lower alphabets or underscore
for example _abc or xyz or Abc
string may contain digits after first character
like _f9 or f10 or car20 or top10cars
string should never contain special characters like ! @ # $ % ^ & * ( ) + - =
here my tiny script
if [[ $1 =~ ^[A-Za-z_]+$ ]]; then
echo it matches
else
echo does_not match
fi
when i pass argument like only & or * or ( , script don't work , sometimes cursor never comes backs , i need to press ctrl+c
for example ./script.sh (abc
./script.sh &&
./script.sh &abc
whats wrong with script
bash shell-script
edited question
â user143252
Sep 25 '17 at 17:07
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
condition
string either start with upper or lower alphabets or underscore
for example _abc or xyz or Abc
string may contain digits after first character
like _f9 or f10 or car20 or top10cars
string should never contain special characters like ! @ # $ % ^ & * ( ) + - =
here my tiny script
if [[ $1 =~ ^[A-Za-z_]+$ ]]; then
echo it matches
else
echo does_not match
fi
when i pass argument like only & or * or ( , script don't work , sometimes cursor never comes backs , i need to press ctrl+c
for example ./script.sh (abc
./script.sh &&
./script.sh &abc
whats wrong with script
bash shell-script
condition
string either start with upper or lower alphabets or underscore
for example _abc or xyz or Abc
string may contain digits after first character
like _f9 or f10 or car20 or top10cars
string should never contain special characters like ! @ # $ % ^ & * ( ) + - =
here my tiny script
if [[ $1 =~ ^[A-Za-z_]+$ ]]; then
echo it matches
else
echo does_not match
fi
when i pass argument like only & or * or ( , script don't work , sometimes cursor never comes backs , i need to press ctrl+c
for example ./script.sh (abc
./script.sh &&
./script.sh &abc
whats wrong with script
bash shell-script
bash shell-script
edited Sep 25 '17 at 17:06
asked Sep 25 '17 at 17:00
user143252
95
95
edited question
â user143252
Sep 25 '17 at 17:07
add a comment |Â
edited question
â user143252
Sep 25 '17 at 17:07
edited question
â user143252
Sep 25 '17 at 17:07
edited question
â user143252
Sep 25 '17 at 17:07
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
Quoting.
In the script, use "$1"
rather than just $1
.
On the command line, use
./script '*(ontehu'
instead of
./script *(ontehu
./script.sh (abc
This is a syntax error in the shell grammar../script.sh &&
This makes the shell think there is a conditional AND on the command line and it expects something on the right hand side of the&&
operator../script.sh &abc
This is two commands:./script
started as a background process (with&
), and the commandabc
.
In all these cases, the argument should be quoted (single quoted, ideally, unless you need the shell to insert the value of a shell variable, in which case it should be double quoted).
Your script:
if [[ $1 =~ ^[A-Za-z_]+$ ]]; then
echo it matches
else
echo does_not match
fi
Quote $1
:
if [[ "$1" =~ ^[A-Za-z_]+$ ]]; then
echo it matches
else
echo does_not match
fi
Allow digits in the tail end of the value:
if [[ "$1" =~ ^[A-Za-z_][A-Za-z0-9_]+$ ]]; then
echo it matches
else
echo does_not match
fi
Do proper reporting of errors (this is extra):
if [[ "$1" =~ ^[A-Za-z_][A-Za-z0-9_]+$ ]]; then
printf '"%s" is a valid variable namen' "$1"
else
printf '"%s" is not a proper variable namen' "$1" >&2
exit 1
fi
add a comment |Â
up vote
1
down vote
That's because these are reserved characters.
& means run command in the background
* resolves to all files/dirs in the actual directory, which are then passed as arguments
() is used for command order preference or function declarations
If you want such a characters in the string, put it in quotes " " or ' '
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Quoting.
In the script, use "$1"
rather than just $1
.
On the command line, use
./script '*(ontehu'
instead of
./script *(ontehu
./script.sh (abc
This is a syntax error in the shell grammar../script.sh &&
This makes the shell think there is a conditional AND on the command line and it expects something on the right hand side of the&&
operator../script.sh &abc
This is two commands:./script
started as a background process (with&
), and the commandabc
.
In all these cases, the argument should be quoted (single quoted, ideally, unless you need the shell to insert the value of a shell variable, in which case it should be double quoted).
Your script:
if [[ $1 =~ ^[A-Za-z_]+$ ]]; then
echo it matches
else
echo does_not match
fi
Quote $1
:
if [[ "$1" =~ ^[A-Za-z_]+$ ]]; then
echo it matches
else
echo does_not match
fi
Allow digits in the tail end of the value:
if [[ "$1" =~ ^[A-Za-z_][A-Za-z0-9_]+$ ]]; then
echo it matches
else
echo does_not match
fi
Do proper reporting of errors (this is extra):
if [[ "$1" =~ ^[A-Za-z_][A-Za-z0-9_]+$ ]]; then
printf '"%s" is a valid variable namen' "$1"
else
printf '"%s" is not a proper variable namen' "$1" >&2
exit 1
fi
add a comment |Â
up vote
2
down vote
accepted
Quoting.
In the script, use "$1"
rather than just $1
.
On the command line, use
./script '*(ontehu'
instead of
./script *(ontehu
./script.sh (abc
This is a syntax error in the shell grammar../script.sh &&
This makes the shell think there is a conditional AND on the command line and it expects something on the right hand side of the&&
operator../script.sh &abc
This is two commands:./script
started as a background process (with&
), and the commandabc
.
In all these cases, the argument should be quoted (single quoted, ideally, unless you need the shell to insert the value of a shell variable, in which case it should be double quoted).
Your script:
if [[ $1 =~ ^[A-Za-z_]+$ ]]; then
echo it matches
else
echo does_not match
fi
Quote $1
:
if [[ "$1" =~ ^[A-Za-z_]+$ ]]; then
echo it matches
else
echo does_not match
fi
Allow digits in the tail end of the value:
if [[ "$1" =~ ^[A-Za-z_][A-Za-z0-9_]+$ ]]; then
echo it matches
else
echo does_not match
fi
Do proper reporting of errors (this is extra):
if [[ "$1" =~ ^[A-Za-z_][A-Za-z0-9_]+$ ]]; then
printf '"%s" is a valid variable namen' "$1"
else
printf '"%s" is not a proper variable namen' "$1" >&2
exit 1
fi
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Quoting.
In the script, use "$1"
rather than just $1
.
On the command line, use
./script '*(ontehu'
instead of
./script *(ontehu
./script.sh (abc
This is a syntax error in the shell grammar../script.sh &&
This makes the shell think there is a conditional AND on the command line and it expects something on the right hand side of the&&
operator../script.sh &abc
This is two commands:./script
started as a background process (with&
), and the commandabc
.
In all these cases, the argument should be quoted (single quoted, ideally, unless you need the shell to insert the value of a shell variable, in which case it should be double quoted).
Your script:
if [[ $1 =~ ^[A-Za-z_]+$ ]]; then
echo it matches
else
echo does_not match
fi
Quote $1
:
if [[ "$1" =~ ^[A-Za-z_]+$ ]]; then
echo it matches
else
echo does_not match
fi
Allow digits in the tail end of the value:
if [[ "$1" =~ ^[A-Za-z_][A-Za-z0-9_]+$ ]]; then
echo it matches
else
echo does_not match
fi
Do proper reporting of errors (this is extra):
if [[ "$1" =~ ^[A-Za-z_][A-Za-z0-9_]+$ ]]; then
printf '"%s" is a valid variable namen' "$1"
else
printf '"%s" is not a proper variable namen' "$1" >&2
exit 1
fi
Quoting.
In the script, use "$1"
rather than just $1
.
On the command line, use
./script '*(ontehu'
instead of
./script *(ontehu
./script.sh (abc
This is a syntax error in the shell grammar../script.sh &&
This makes the shell think there is a conditional AND on the command line and it expects something on the right hand side of the&&
operator../script.sh &abc
This is two commands:./script
started as a background process (with&
), and the commandabc
.
In all these cases, the argument should be quoted (single quoted, ideally, unless you need the shell to insert the value of a shell variable, in which case it should be double quoted).
Your script:
if [[ $1 =~ ^[A-Za-z_]+$ ]]; then
echo it matches
else
echo does_not match
fi
Quote $1
:
if [[ "$1" =~ ^[A-Za-z_]+$ ]]; then
echo it matches
else
echo does_not match
fi
Allow digits in the tail end of the value:
if [[ "$1" =~ ^[A-Za-z_][A-Za-z0-9_]+$ ]]; then
echo it matches
else
echo does_not match
fi
Do proper reporting of errors (this is extra):
if [[ "$1" =~ ^[A-Za-z_][A-Za-z0-9_]+$ ]]; then
printf '"%s" is a valid variable namen' "$1"
else
printf '"%s" is not a proper variable namen' "$1" >&2
exit 1
fi
edited Sep 25 '17 at 17:22
answered Sep 25 '17 at 17:09
Kusalananda
106k14209327
106k14209327
add a comment |Â
add a comment |Â
up vote
1
down vote
That's because these are reserved characters.
& means run command in the background
* resolves to all files/dirs in the actual directory, which are then passed as arguments
() is used for command order preference or function declarations
If you want such a characters in the string, put it in quotes " " or ' '
add a comment |Â
up vote
1
down vote
That's because these are reserved characters.
& means run command in the background
* resolves to all files/dirs in the actual directory, which are then passed as arguments
() is used for command order preference or function declarations
If you want such a characters in the string, put it in quotes " " or ' '
add a comment |Â
up vote
1
down vote
up vote
1
down vote
That's because these are reserved characters.
& means run command in the background
* resolves to all files/dirs in the actual directory, which are then passed as arguments
() is used for command order preference or function declarations
If you want such a characters in the string, put it in quotes " " or ' '
That's because these are reserved characters.
& means run command in the background
* resolves to all files/dirs in the actual directory, which are then passed as arguments
() is used for command order preference or function declarations
If you want such a characters in the string, put it in quotes " " or ' '
answered Sep 25 '17 at 17:10
Jaroslav Kucera
4,3904621
4,3904621
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f394355%2fshell-script-to-test-condition-on-passed-string%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
edited question
â user143252
Sep 25 '17 at 17:07