How to solve Environment Variables with $ Character? - linux

I have a Environment Variables:
"PWD=uNfob$bA5052433"
When I print PWD out it always:
'uNfob'
I have tried :
'PWD=uNfob$bA5052433'
or
PWD='uNfob$bA5052433'
all not work,any suggestion ?

This works for me:
VAR='aaa$bbb'
echo $VAR
aaa$bbb
PWD is a special shell variable that represents the current directory. You might want to use someting else.

I solved it by 2 steps:
1.use single quotes 'PWD=uNfob$bA5052433'
2.add '/' 'PWD=uNfob/$bA5052433'

Related

how to use user defined variables in file path concatenation using shell script

$var='system1'
data=C:/data/$var/current_extract/*
Output should be
data=C:/data/system1/current_extract/*"
but i still see the result C:/data/$var/current_extract/* $var value is
**system1** not showing the path
Remove the dollar in the assignment
var='system1'
For setting a variable in Linux variable_name=value. For displaying it you have to prefix the variable name with $ symbol. for example echo $variable_name
var='system1'
data=C:/data/$var/current_extract/*

Escape char in Gitlab Secret Variables

I have a secret var :
But when I do - echo %MySecretVar%, runner displays foo only
How can i escape special chars like ! in Gitlab Secret Vars ?
I had the same problems with Gitlab, job running on windows, but I assume it will reproduce on Linux as well, because it seems Gitlab parsing issue or relay weird escaping.
So I have set environment variable
APPPOOL_PWD: 'blabla!foo$bar'
and output of echo %APPPOOL_PWD% or echo $APPPOOL_PWD was 'blabla'
The Gitlab seems to be was eating out the exclamation mark sign ! and dollar sign $. To avoid it as proposed in comment for exclamation mark I have used ^^ and for dollar sign I have used $$ as proposed in the Gitlab variables documentation.
So following variable works well:
APPPOOL_PWD: 'blabla^^!foo$$bar'
and output of the echo command in this case would be 'blabla!foo$bar'.
I was able to use a value with special characters this way:
Define Gitlab CI variable FOO with special characters in the value, e.g. ?!asdf&%fghjkl
In .gitlab-ci.yml define:
variables:
bar: '"%FOO%"'
script:
- echo %bar%
This way the variable will stay exactly the way it is typed in your CI variable field.
I'm using Windows batch shell. If you use another shell for script running, the syntax is a little different from %bar%. Check out the syntax here: Gitlab CI reference
I am using GitLab 15.3.3-ee and I don't see any issue with the ! it get's passed through. However for $ you will have to use extra $ as escape character, just like mentioned in the first comment.

string manipulation of Directory structure

Scenario: I have a script but no idea where I am in the directory tree, I need to resolve back to the nearest known location UPROC[something]
What I have so far:
I have a script running in a directory for example:
/home/jim/query/UPROCL/test/bob/dircut.sh
now the only constant in this is that the Directory I want will begin with UPROC... maybe not UPROCL but definitely UPROC
So I have written the following:
#!/bin/bash
#Absolute path for this script
SCRIPT=$(readlink -f "$0")
echo $SCRIPT
#Gets Path of script without script name
SCRIPTPATH=$(dirname "$SCRIPT")
echo $SCRIPTPATH
#Cuts everything after UPROC(.* is wildcard)/
CUTDOWN=$(sed 's/\(UPROC.*\/\).*/\1/' <<< $SCRIPTPATH)
echo $CUTDOWN
The only problem is that it output is:
/home/jim/query/UPROCL/test/bob/dircut.sh
/home/jim/query/UPROCL/test/bob
/home/jim/query/UPROCL/test/
Can some tell me what is wrong with my sed command as it is not cutting down to
/home/jim/query/UPROCL/
Because * is greedy. You want to be more selective about what characters are allowed following "UPROC" -- any non-slash
Not
sed 's/\(UPROC.*\/\).*/\1/'
but
sed -r 's,(UPROC[^/]*/).*,\1,'
Using different delimiters for the s/// command reduces the "leaning toothpick" problem.
Because the .* in the () is matching to the / at the end of test/.
You need [^/]* instead of . to not match any slashes.
When you want to know in which directory you are, why don't use pwd?
One thing which might be useful: the command pwd shows the value of the environment variable PWD (uppercase). In case you want to use the current directory as a value, you might use this.

Enclosing brackets when setting variables

Why does this command destroy the search path?
PATH=($PATH:$HOME/bin)
The PATH appears unchanged, but the shell cannot find commands.
It was entered in error for
PATH=$PATH:$HOME/bin
Probably confused with
PATH=$(echo $PATH:$HOME/bin)
Using parentheses you create an array:
$ a=(x:y:z v:w:x)
$ echo ${a[0]}
x:y:z
$ echo ${a[1]}
v:w:x
In your case you created an array with one element (the whole path). This is then not interpreted anymore as the path to search for executables. This PATH has to be a string of directories separated by colons, not an array.
If you want to acheive PATH=$PATH:$HOME/bin
Try PATH=(\$PATH:\$HOME/bin)

Bash Shell - The : Command

The colon command is a null command.
The : construct is also useful in the conditional setting of variables. For example,
: ${var:=value}
Without the :, the shell would try to evaluate $var as a command. <=???
I don't quite understand the last sentence in above statement. Can anyone give me some details?
Thank you
Try
var=badcommand
$var
you will get
bash: badcommand: command not found
Try
var=
${var:=badcommand}
and you will get the same.
The shell (e.g. bash) always tries to run the first word on each command line as a command, even after doing variable expansion.
The only exception to this is
var=value
which the shell treats specially.
The trick in the example you provide is that ${var:=value} works anywhere on a command line, e.g.
# set newvar to somevalue if it isn't already set
echo ${newvar:=somevalue}
# show that newvar has been set by the above command
echo $newvar
But we don't really even want to echo the value, so we want something better than
echo ${newvar:=somevalue}.
The : command lets us do the assignment without any other action.
I suppose what the man page writers meant was
: ${var:=value}
Can be used as a short cut instead of say
if [ -z "$var" ]; then
var=value
fi
${var} on its own executes the command stored in $var. Adding substitution parameters does not change this, so you use : to neutralize this.
Try this:
$ help :
:: :
Null command.
No effect; the command does nothing.
Exit Status:
Always succeeds.

Resources