Bash string literal comparison to variable failing [closed] - bash

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have the following simple bash code to test string comparison:
#!/bin/sh
BRANCH="master"
echo $ref
if [[ "$ref" = "refs/heads/$BRANCH" ]]
then
echo "Matches"
else
echo "Do not match"
fi
When I ran the code using export ref=/refs/heads/master && . sample I get the following result:
/refs/heads/master
Do not match
What may be causing the problem?

What is causing the problem is the missing slash in your test: /refs/heads/master is not equal to refs/heads/master!

Related

Variable assignment and simultaneous check of the return code in conditional statements [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
What are the contras for writing bash scripts like in the example below?
Would you separate a variable assignment or a function call from the check of its return code? If so, why? What is wrong - if anything - with this kind of conditionals?
local return_code=0
if username=$(get_username "${user}") \
&& is_from_earth "${user}"; then
echo "${username} is an earthling"
else
echo "ERROR: no name or not from the Earth"
return_code=1
fi
There's nothing wrong, per se, but in the interest of more accurate error messages, I'd probably write something like
local return_code=1
if ! username=$(get_username "$user"); then
echo "ERROR: no such user '$user'" >&2
elif ! is_from_earth "$username"; then
echo "ERROR: $username is not from Earth" >&2
else
echo "$username is an earthing"
return_code=0
fi

raku: Support for utf8-c8 issue in Raku 2020.10 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have a file from Wild Wild Web and it contains malformed UTF8. I handled malformed UTF8 in my other codes in previous versions of Raku. In 2020.10 version, I am running into this issue below. Has the support for utf8-c8 changed (this page says it should work, but it doesn't seem to) :
https://docs.raku.org/language/unicode#index-entry-UTF-8_Clean-8
This page has this example:
say slurp($test-file, enc => 'utf8-c8');
Now my code on the command line:
raku -e 'my $a = slurp("zlist"); for $a.lines { .say }'
Malformed UTF-8 near bytes 73 e2 5f at line 55 col 14
in block <unit> at -e line 1
Then using this:
raku -e 'my $a = slurp("zlist", enc => 'utf8-c8'); for $a.lines { .say }'
===SORRY!=== Error while compiling -e
Undeclared routine:
utf8-c8 used at line 1
My code is simple and essentially copied from the example. What am I doing wrong?

how to sed for certain parts of a string by delimiter [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have lots of different strings that look like redhat-ubi-ubi7-7.8
I want to use the string to make variables so that I end up having something like
vendor=redhat
product=ubi
image=ubi7
tag=7.8
How can I do this?
With bash and a here string:
string='redhat-ubi-ubi7-7.8'
IFS=- read -r vendor product image tag <<< "$string"
echo "$vendor"
Output:
redhat
Using P.E. parameter expansion.
string='redhat-ubi-ubi7-7.8'
vendor=${string%%-*}
tag=${string##*-}
image=${string%-*}
product=${image#*-}
product=${product%-*}
image=${image##*-}
printf '%s\n' vendor=$vendor product=$product image=$image tag=$tag
Output
vendor=redhat
product=ubi
image=ubi7
tag=7.8

Unable to find the reason for this: bash missing' [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
why am I getting "bash missing'" for this:
function get_xserver ()
{
case $TERM in
xterm )
XSERVER=$(who am i | awk '{print $NF}' | tr -d ')''(' )
;;
aterm | rxvt)
# Find some code that works here. ...
;;
esac
}
This is the exact error:
bash: [: missing `]'
The error is not in the code you posted. The error message:
-bash: [: missing `]'
Means exactly what it says - there is a missing ] character, namely in a [ test ] statement.
Try it:
$ [ 1 -eq 2
-bash: [: missing `]'
You need to identify where the error actually is, and add the missing closing bracket.

Append to variable within shell script loop [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm trying to loop over all environment variables in a shell script, and create an HTML query string from ones which match a pattern. Unfortunately, I can't seem to assign to variables in the loop. I've got this:
#!/bin/sh
IFS=$'\n'
TAGS=""
for item in $(printenv)
do
if [[ $item == FOO_TAG_* ]]
then
TAGS = "${TAGS}&${item}"
fi
done
But this gives me
/etc/script.sh: line 9: TAGS: command not found
/etc/script.sh: line 9: TAGS: command not found
How do I fix this?
In the assignment, remove space between variable name and =
TAGS="${TAGS}${item}"

Resources