Access bash special variable from makefile - linux

I am running:
» make --version
GNU Make 4.1
Built for x86_64-pc-linux-gnu
Copyright (C) 1988-2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
This works, from bash:
$ echo $RANDOM
14522
This does not work:
$ make echo-random
With Makefile:
echo-random:
echo $(RANDOM)
Some questions:
Does make use a shell to run commands?
Is it possible to tell shell to use bash?
Can make somehow access bash special env variables?

You can invoke bash with the -c argument (that tells it the next argument is a command it has to run and exit):
echo-random:
#bash -c 'echo $$RANDOM'
This way, each invocation of make echo-random starts a new bash instance that runs the command echo $RANDOM and it produces the outcome you expect.

The answer by #axiac is good. This is an alternative:
SHELL = /bin/bash
random := $(shell echo $$RANDOM)
echo-random:
echo $$RANDOM
echo $(random)
Output:
» make
echo $RANDOM
18826
echo 16300
16300
See here and here

Related

bash script is stuck at tee

I am using two scripts say script1 and script2. I call script2 from script1 which generates lots of text data. I am redirecting this text to a file using tee. But tee is stuck and doesn't move forward.
I cannot give the exact example as scripts are way too long to be included here. So giving a snippet of what's going on.
# script1
# Do some task
/bin/bash script2.sh 2>&1 | tee script2.log
capture=$(cat script2.log | grep "Successfully completed" | wc -l)
# Do some more work
#script2
# Execute some code
# Some python scripts
echo "Script End here"
Now when I execute script1.sh I am getting Script End here indicating second script completed successfully. But script1 is stuck at tee. When I removed tee and didn't capture output it worked out fine.
Did anyone face this before. Any idea what might be going on.
# System details
$ uname -a
Linux jetson-nx-jetpack461 4.9.253-tegra #1 SMP PREEMPT Sat Feb 19 08:58:27 PST 2022 aarch64 aarch64 aarch64 GNU/Linux
# Bash version
$ bash --version
GNU bash, version 4.4.20(1)-release (aarch64-unknown-linux-gnu)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
From script2
When the script has a large number of lines (1000/2000/...), it is difficult to debug using set -x.
Either proceed based on comment from Barmar, or you include:
echo read1
read
command12
echo read2
read
command23
View the location where it is hanging.
If it is hanging exactly due to tee, use the following command:
echo read5
read
echo type tee
type tee
echo read6
read
Using this we can identify exactly where the script is hanging.
You can also use read1.1 read1.0 read1.-1 ... when debugging.

Why am I getting the same number when I call $RANDOM in this context?

I wrote a simple bash alias to create a sort of daily tmp directory to work in:
alias datdir="mkdir $(date +'%m_%d_%Y')___$RANDOM"
When I call it repeatedly on the same day, I get this:
mkdir: cannot create directory ‘02_04_2022___24499’: File exists
However, when I simply run echo $RANDOM on the terminal, I get different numbers.
I'm running Git Bash on Microsoft Windows 10 Business, Version 10.0.19044 Build 19044
$ bash --version
GNU bash, version 4.4.23(1)-release (x86_64-pc-msys)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

How do you enable declare -A on MacOS Mohave? [duplicate]

This question already has answers here:
$BASH_VERSION reports old version of bash on macOS, is this a problem that should be fixed?
(4 answers)
Associative arrays: error "declare: -A: invalid option"
(10 answers)
Mac: Virtual Shell Bash Version does not Match Installed Version [duplicate]
(2 answers)
Closed 3 years ago.
I've been working with bash more and more on my Mac and have discovered that for some reason the declare -A command does not work.
myname$ declare -A
-bash: declare: -A: invalid option
declare: usage: declare [-afFirtx] [-p] [name[=value] ...]
This answer says that support for associate arrays was added to bash in version 4.
When I run bash --version I get 5.0.11.
myname$ bash --version
GNU bash, version 5.0.11(1)-release (x86_64-apple-darwin18.6.0)
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
[1]: https://unix.stackexchange.com/a/428205/379464
I ran bash --version and echo $BASH_VERSION as #chepner recommended and they give different answers.
bash --version
GNU bash, version 5.0.11(1)-release (x86_64-apple-darwin18.6.0)
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
SEGON207127:gnubin nberkowitz$ echo $BASH_VERSION
3.2.57(1)-release
I've already run brew install gnutils and added it to PATH in .bashrc.
Anything else I should try?

Does Yum update running process?

I am running Fedora 20 on my desktop. When I saw the advisory about the vulnerability in bash, I checked my system out and turns out, it was vulnerable. Today, I updated my system and a new version of bash was installed. I was expecting to restart bash for the changes to take effect. But to my surprise, the update process somehow fixed running copy of bash.
How?
Log:
shows 2 invocations of the same command. Between the two, upgraded the system from a different xterm window.
bash$ env x='() { :;}; echo vulnerable' bash -c "echo This is a test"
vulnerable
This is a test
bash$ env x='() { :;}; echo vulnerable' bash -c "echo This is a test"
bash: warning: x: ignoring function definition attempt
bash: error importing function definition for `x'
If you split the command into two it's clearer:
env x='() { :;}; echo vulnerable'
bash -c "echo This is a test"
You're running a new instance of bash when checking for the bug, so it's using the updated code. You should make sure to restart all your shells to ensure that you're safe.

What will happen when I edit a script while it's running?

What will happen when I a script file and save it when it's still running, and will it print my needed results.
Let's test it.
Create a script test.sh:
#!/usr/bin/env bash
sleep 1
echo 'echo "executed overwritten"' >> "$0" # append to self
sleep 1
echo 'executed original'
and execute it:
$ bash --version
GNU bash, version 4.2.24(1)-release (i686-pc-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
$ chmod +x test.sh
$ ./test.sh
executed original
executed overwritten
$
Notice that bash continued reading the modified file. It maintains its current position in the file (in bytes) when the file changes.
As a demonstration, the script
#!/usr/bin/env bash
sleep 1
dd if=/dev/urandom bs=1024 count=1 of="$0" &>/dev/null # overwrite self
sleep 1
echo 'executed original'
gives the output
$ ./test.sh
./test.sh: line 6: syntax error near unexpected token `$'\311\262\203''
./test.sh: line 6: `��z�eп9)�v��▒y�a��44'{�d��4\:�A����˷���&�$�����l�
#(ɲ��4��OϹI�n>��7��P�M�a��X.�S�a���V�m�~O<��{}������J��$��TOtRd��Nw�&��B�Dz�▒��-��<`�P<?N��▒rT�Jq�L����JY�*hz���M�����i�⫣��S+�����\��c���m�NKV�8|��xvX}�׉V����PTd䊄�9��7���|��/��X��
��0¤k��_�R���e�*���(qu:UUɭp/j��n��bŇ_�UR?3▒�▒�%Rn�|DE$8�QbaK)A�{ ��O>9��A�����lt�����g)s��O��M��#���w��|�����N��,W'
Notice that it attempted to execute the random gibberish.
(This is Ubuntu 12.04. Behavior may vary with other shells.)

Resources