Iam unable to set PS1 variable in ksh shell - ksh

Below command to set the PS1 doesn't work for me
here is my shell:-
[u#h w]$echo $SHELL
/bin/ksh93
command:-
PS1="[\u#\h \w]\$"
returned output:-
[u#h w]$
Expected output:-
Linux#LinuxDistro /home/sohil$

in ksh \u etc won't work. You have to use something like
PS1='[$(id -un)#$(hostname -s) $PWD]$ '
Use single quotes, otherwise the prompt will not change with each command.
Or if you want to always show the initial user and hostname
PS1="[$(whoami)#$(hostname -s) \$PWD]$ "

Related

Cron removing $ character from environment variable in shell script

I have an environment variable in a docker container that stores a password with special characters. This particular password contains a $ in it. I output this environment variable in a shell script. If I run the script manually, everything is fine. When the cron runs the script, the $ and the following 2 characters are removed. I have tried escaping the special characters in several ways, the latest of which is below, but the outcome is the same (fine manually, missing with the cron). For this example, assume the password is blahblah$xy*blahblah, which is what I would see when running the script. If the cron runs the script, I would get blahblah*blahblah.
My script (testVars.sh):
#!/bin/bash
echo "Testing variables"
MY_PASS=$MY_PASSWORD
TEST_PASS=$(sed -e 's/[^a-zA-Z0-9,._+#%/-]/\\&/g; 1{$s/^$/""/}; 1!s/^/"/; $!s/$/"/' <<< $MY_PASSWORD)
echo ${MY_PASS}
echo ${TEST_PASS}
My cron:
BASH_ENV=/root/env_vars.sh
33 13 * * * root /opt/testVars.sh >> /opt/cron.log
I am assuming that it is actually possible to have a $ sign in a string in this way.
I solved it by added the following into the docker-entrypoint.sh file, just before my printenv command:
export MY_PASSWORD=$(sed -e 's/[^a-zA-Z0-9,._+#%/-]/\\&/g; 1{$s/^$/""/}; 1!s/^/"/; $!s/$/"/' <<<"$MY_PASSWORD")
Thanks to Ture PĂ„lsson for pointing me in the right direction.

Bash, double quotes and "reboot" command

Assume you have those two statements in your bash script:
# No. 1
MSG="Automatic reboot now."
echo $MSG
# No. 2
MSG=""Automatic reboot now.""
echo $MSG
The output of statement number 1 is as expected (it is simply printed). If bash runs statement two, the machine is rebooted (any valid bash command will be executed).
But why?
That's because the meaning of MSG=""Automatic reboot now."" is the following:
Execute reboot now. with the env. var. MSG set to Automatic.
It's equivalent to:
MSG=Automatic reboot now.
A lesser known shell feature is the ability to set environment variables for the duration of a single command. This is done by prepending a command with one or more assignments, as in: var1=foo var2=bar command.
Here's a demonstration. Notice how the original value of $MSG is preserved.
$ export MSG=Hello
$ bash -c 'echo $MSG'
Hello
$ MSG=Goodbye bash -c 'echo $MSG'
Goodbye
$ bash -c 'echo $MSG'
Hello
Now on to your question:
MSG=""Automatic reboot now.""
The pairs of double quotes nullify each other, and might as well not be there. It's equivalent to:
MSG=Automatic reboot now.
which executes reboot with an argument of now. and the $MSG environment variable set to Automatic.

Problems to get a remote shell variable

I have a problem with an execution of a shell script into a remote shell.
I can't get value of $ARQ_END.
ssh -T user#MACHINE << 'EOSSH'
/app/work/leo/ReturnFileName.sh #This script returns a filename like: ADDRESS_BR_RECIFE_20170913.txt
ARQ_END="`/app/work/leo/ReturnFileName.sh`"
EOSSH
echo $ARQ_END #Returns nothing! Expected to return: ADDRESS_BR_RECIFE_20170913.txt
Setting a variable in a subshell isn't visible in the parent shell. You need to set the variable directly in the parent shell. The way to do that is to pass the output of ReturnFileName.sh up through the ssh session and to the parent shell and capture it there.
ARQ_END=$(ssh user#MACHINE /app/work/leo/ReturnFileName.sh)
echo "$ARQ_END"
Thanks, it works!
I used the case as you posted:
ARQ_END=$(ssh user#MACHINE /app/work/leo/ReturnFileName.sh)
echo "$ARQ_END"

Passing bash shell variables into a mongo shell command

I am trying to execute a bash shell that calls the mongo shell with a command created dynamically. The bash shell looks like this:
#!/bin/bash
TODAY=`date '+%Y-%m-%d'`
CMD=" 'printjson(db.collection.aggregate([{$match:{processedtime:{$gte:\"$TODAY"}}},{$project:{_id:$field",count:{$sum:1}}}]))'"
echo "CMD: $CMD"
mongo host/mdb --eval $CMD
Note the processedtime field in the collection is a sting value formatted as an ISODate object.
When executed as a bash shell I get an "Unexpected token ILLEGAL" error. If I execute the command echoed to the screen I get the desired results.
My question is, Is there a way to pass in shell defined variables into the mongo shell and if there is what do I need to change to do this?
You're note escaping enough, and I believe you don't want the literal single quotes:
CMD="printjson(db.collection.aggregate([{\$match:{processedtime:{\$gte:\"$TODAY\"}}},{\$project:{_id:\$field",count:{\$sum:1}}}]))"
# ...^.. single quote unneeded ..........^.......................^.....^.......&......^..............^...............^............^
mongo host/mdb --eval "$CMD"
# ....................^....^ crucial double quotes here

Difference between $0 and $SHELL

I would like to know the exact difference between $0 and $SHELL. I know that these two are used to know the shell info.
It would be great if some one explain with examples.
What does it indicate if both show different values as below ?
# echo $0
ksh
# echo $SHELL
/sbin/sh
#
SHELL is just an environment variable, while $0 is the path of the currently running program. The user should set SHELL to the value of the preferred shell, similar to the way the user sets PAGER and EDITOR. Any program that needs to spawn a shell should check the value of SHELL to determine which shell to invoke. SHELL is not the path of the shell you get when you login. It will not change when a new shell is run any more than PAGER will change if it is set to less but the user invokes more, or if EDITOR is set to vi and the user runs emacs. For instance:
$ echo $0 $SHELL
bash /bin/bash
$ exec csh
% echo $0 $SHELL
csh /bin/bash
$SHELL gives the full path to your default shell.
$0 gives the name of your current shell.

Resources