Bash script fails where command line works2019 Community Moderator ElectionHow can I run a “grep | grep” command as a string in a bash function?Howto run interactive commands, as another useraliasing a slightly complex script on Linux/bashGetting wrong $LINENO for a trapped functionHow can I run a bash variable as a command exactly without additional quotation?functions argumentsBash script inheritance? Calling a function from another script?How is a bash script executed via its filename as command name with and without shebang?Correct way of building variable length argument line to external command in bashCorrectly Escape echo -ne
How would you translate "more" for use as an interface button?
Language involving irrational number is not a CFL
Unable to disable Microsoft Store in domain environment
What does "Scientists rise up against statistical significance" mean? (Comment in Nature)
Anime with legendary swords made from talismans and a man who could change them with a shattered body
How do you justify more code being written by following clean code practices?
How to reduce predictors the right way for a logistic regression model
If Captain Marvel (MCU) were to have a child with a human male, would the child be human or Kree?
What is this high flying aircraft over Pennsylvania?
How to preserve electronics (computers, iPads and phones) for hundreds of years
Giving feedback to someone without sounding prejudiced
Did I make a mistake by ccing email to boss to others?
Can I cause damage to electrical appliances by unplugging them when they are turned on?
The Digit Triangles
What does "tick" mean in this sentence?
Is there anyway, I can have two passwords for my wi-fi
Visualizing the difference curve in a 2D plot?
Why the "ls" command is showing the permissions of files in a FAT32 partition?
El Dorado Word Puzzle II: Videogame Edition
Proving an identity involving cross products and coplanar vectors
Why didn't Voldemort know what Grindelwald looked like?
Make a Bowl of Alphabet Soup
Sound waves in different octaves
Does the Crossbow Expert feat's extra crossbow attack work with the reaction attack from a Hunter ranger's Giant Killer feature?
Bash script fails where command line works
2019 Community Moderator ElectionHow can I run a “grep | grep” command as a string in a bash function?Howto run interactive commands, as another useraliasing a slightly complex script on Linux/bashGetting wrong $LINENO for a trapped functionHow can I run a bash variable as a command exactly without additional quotation?functions argumentsBash script inheritance? Calling a function from another script?How is a bash script executed via its filename as command name with and without shebang?Correct way of building variable length argument line to external command in bashCorrectly Escape echo -ne
I wrote a basic bash function to test for the occurence of a string search argument in another string. Running it from the command line gives expected results. Running the same from a shell script fails. Can somebody show me the errors of my ways?
me@mylaptop $ line='someidentifier 123 another identifier 789 065 theend'
me@mylaptop $ sarg='+([0-9])+([ ])another identifier'
me@mylaptop $ type strstr
strstr is a function
strstr ()
[ "$1#*$2*" = "$1" ] && return 1;
return 0
me@mylaptop $ strstr "$line" "$sarg" ; echo "$?"
0
me@mylaptop $
My script:
me@mylaptop $ cat l.sh
#!/bin/bash
function strstr ()
[ "$1#*$2*" = "$1" ] && return 1;
return 0
line='someidentifier 123 another identifier 789 065 theend'
sarg='+([0-9])+([ ])another identifier'
echo '==='"$line"'==='
echo '==='"$sarg"'==='
strstr "$line" "$sarg"
echo "$?"
me@mylaptop $ ./l.sh
===someidentifier 123 another identifier 789 065 theend===
===+([0-9])+([ ])another identifier===
1
me@mylaptop $
bash bash-expansion
add a comment |
I wrote a basic bash function to test for the occurence of a string search argument in another string. Running it from the command line gives expected results. Running the same from a shell script fails. Can somebody show me the errors of my ways?
me@mylaptop $ line='someidentifier 123 another identifier 789 065 theend'
me@mylaptop $ sarg='+([0-9])+([ ])another identifier'
me@mylaptop $ type strstr
strstr is a function
strstr ()
[ "$1#*$2*" = "$1" ] && return 1;
return 0
me@mylaptop $ strstr "$line" "$sarg" ; echo "$?"
0
me@mylaptop $
My script:
me@mylaptop $ cat l.sh
#!/bin/bash
function strstr ()
[ "$1#*$2*" = "$1" ] && return 1;
return 0
line='someidentifier 123 another identifier 789 065 theend'
sarg='+([0-9])+([ ])another identifier'
echo '==='"$line"'==='
echo '==='"$sarg"'==='
strstr "$line" "$sarg"
echo "$?"
me@mylaptop $ ./l.sh
===someidentifier 123 another identifier 789 065 theend===
===+([0-9])+([ ])another identifier===
1
me@mylaptop $
bash bash-expansion
add a comment |
I wrote a basic bash function to test for the occurence of a string search argument in another string. Running it from the command line gives expected results. Running the same from a shell script fails. Can somebody show me the errors of my ways?
me@mylaptop $ line='someidentifier 123 another identifier 789 065 theend'
me@mylaptop $ sarg='+([0-9])+([ ])another identifier'
me@mylaptop $ type strstr
strstr is a function
strstr ()
[ "$1#*$2*" = "$1" ] && return 1;
return 0
me@mylaptop $ strstr "$line" "$sarg" ; echo "$?"
0
me@mylaptop $
My script:
me@mylaptop $ cat l.sh
#!/bin/bash
function strstr ()
[ "$1#*$2*" = "$1" ] && return 1;
return 0
line='someidentifier 123 another identifier 789 065 theend'
sarg='+([0-9])+([ ])another identifier'
echo '==='"$line"'==='
echo '==='"$sarg"'==='
strstr "$line" "$sarg"
echo "$?"
me@mylaptop $ ./l.sh
===someidentifier 123 another identifier 789 065 theend===
===+([0-9])+([ ])another identifier===
1
me@mylaptop $
bash bash-expansion
I wrote a basic bash function to test for the occurence of a string search argument in another string. Running it from the command line gives expected results. Running the same from a shell script fails. Can somebody show me the errors of my ways?
me@mylaptop $ line='someidentifier 123 another identifier 789 065 theend'
me@mylaptop $ sarg='+([0-9])+([ ])another identifier'
me@mylaptop $ type strstr
strstr is a function
strstr ()
[ "$1#*$2*" = "$1" ] && return 1;
return 0
me@mylaptop $ strstr "$line" "$sarg" ; echo "$?"
0
me@mylaptop $
My script:
me@mylaptop $ cat l.sh
#!/bin/bash
function strstr ()
[ "$1#*$2*" = "$1" ] && return 1;
return 0
line='someidentifier 123 another identifier 789 065 theend'
sarg='+([0-9])+([ ])another identifier'
echo '==='"$line"'==='
echo '==='"$sarg"'==='
strstr "$line" "$sarg"
echo "$?"
me@mylaptop $ ./l.sh
===someidentifier 123 another identifier 789 065 theend===
===+([0-9])+([ ])another identifier===
1
me@mylaptop $
bash bash-expansion
bash bash-expansion
edited 20 hours ago
Rui F Ribeiro
41.6k1483141
41.6k1483141
asked 20 hours ago


JdeHaanJdeHaan
359214
359214
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You have enabled the extglob
shell option in your interactive shell, but not in your script:
$ strstr "$line" "$sarg" ; echo "$?"
1
$ shopt -s extglob
$ strstr "$line" "$sarg" ; echo "$?"
0
Note that your function could be simplified into
strstr ()
[ "$1#*$2*" != "$1" ]
Correct. Perfect. Thanks. Both for the solution and the improvement.
– JdeHaan
19 hours ago
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f507369%2fbash-script-fails-where-command-line-works%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have enabled the extglob
shell option in your interactive shell, but not in your script:
$ strstr "$line" "$sarg" ; echo "$?"
1
$ shopt -s extglob
$ strstr "$line" "$sarg" ; echo "$?"
0
Note that your function could be simplified into
strstr ()
[ "$1#*$2*" != "$1" ]
Correct. Perfect. Thanks. Both for the solution and the improvement.
– JdeHaan
19 hours ago
add a comment |
You have enabled the extglob
shell option in your interactive shell, but not in your script:
$ strstr "$line" "$sarg" ; echo "$?"
1
$ shopt -s extglob
$ strstr "$line" "$sarg" ; echo "$?"
0
Note that your function could be simplified into
strstr ()
[ "$1#*$2*" != "$1" ]
Correct. Perfect. Thanks. Both for the solution and the improvement.
– JdeHaan
19 hours ago
add a comment |
You have enabled the extglob
shell option in your interactive shell, but not in your script:
$ strstr "$line" "$sarg" ; echo "$?"
1
$ shopt -s extglob
$ strstr "$line" "$sarg" ; echo "$?"
0
Note that your function could be simplified into
strstr ()
[ "$1#*$2*" != "$1" ]
You have enabled the extglob
shell option in your interactive shell, but not in your script:
$ strstr "$line" "$sarg" ; echo "$?"
1
$ shopt -s extglob
$ strstr "$line" "$sarg" ; echo "$?"
0
Note that your function could be simplified into
strstr ()
[ "$1#*$2*" != "$1" ]
answered 19 hours ago


KusalanandaKusalananda
136k17257426
136k17257426
Correct. Perfect. Thanks. Both for the solution and the improvement.
– JdeHaan
19 hours ago
add a comment |
Correct. Perfect. Thanks. Both for the solution and the improvement.
– JdeHaan
19 hours ago
Correct. Perfect. Thanks. Both for the solution and the improvement.
– JdeHaan
19 hours ago
Correct. Perfect. Thanks. Both for the solution and the improvement.
– JdeHaan
19 hours ago
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f507369%2fbash-script-fails-where-command-line-works%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown