bash printf not working - decimals [closed] - linux

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
Why did printf didn't WORK for the final command printf "%s,%.2f\n" "$s","$a" and what's that extra 0.00 coming from?
When I ran them individually, they worked as expected but not in the final command.
$ s="giga,fifa"; a="8309.18694444444444444444"; echo "$s"; printf "%s\n" "$s"; echo -e "\n"; echo "$a"; printf "%.2f\n" "$a"; echo -e "\n"; echo "$s,$a"; printf "%s" "$s,"; printf "%.2f\n" "$a";echo;printf "%s,%.2f\n" "$s","$a"
giga,fifa
giga,fifa
8309.18694444444444444444
8309.19
giga,fifa,8309.18694444444444444444
giga,fifa,8309.19
giga,fifa,8309.18694444444444444444,0.00
How can I get this output: giga,fifa,8309.19 with just one printf command showing both variables?

You don't use a , in bash printf, you delimit with space. The 0.00 comes from trying to parse the "$s","$a" at once, and has odd results - everything is considered one argument and printed as the first string, so no argument exists for the second and a 0 is substituted as default. This works as expected:
>printf "%s,%.2f\n" "$s" "$a"
giga,fifa,8309.19

Related

update variable string value in bash script [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 6 months ago.
Improve this question
I need to update the value of a variable in a bash script and write the changes to the file. My attempts have either replace the key without strings or deleted the value.
Here's what ive tried :
sed -ri 's|^(VARIABLE_KEY\s*=\s*).*|\new_java-microservice.jar|' path_to_file/bashscript.sh
sed -i "s#\(.*VARIABLE_KEY =\)\( .*\)#\1 "new_java-microservice.jar"#" path_to_file/bashscript.sh
sed -i 's/VARIABLE_KEY =.*/VARIABLE_KEY ="new_java-microservice.jar"/' path_to_file/bashscript.sh
script
#!/bin/bash
COMMAND="$1"
HOME="/root/service/"
BASE="${HOME}/javaservice"
VARIABLE_KEY="existing-java.jar"
case "${COMMAND}" in
"start")
nohup java -jar ${BASE}/${VARIABLE_KEY} start > /dev/null 2>&1 &
;;
"stop")
#....
;;
*)
echo "Usage: $0 {start|stop}"
;;
esac
sed -i 's/^VARIABLE_KEY=.*/VARIABLE_KEY="new_java-microservice.jar"/' path_to_file/bashscript.sh

How to pipe the result of awk into a netcat? [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 process a file content with awk. I would like to pipe the output of awk into netcat, that communicates with a socket.
The awk script is very simple, it just prints $1.
When I pipe the result of awk into netcat, I have no result printed (except the value of addresses_file).
echo "$addresses_file"
echo $(awk -f http-awk-check.awk $addresses_file | netcat -U /home/hduser/socket/rtop12)
Output
../keys/addresses-2021-01-26-17-44.txt
However, I have a result printed when I try with one line by command line:
$ echo 'info' | netcat -U /home/hduser/socket/rtop12
{"jsonrpc":"2.0","id":1,"result":"0x0"}
The awk script:
BEGIN {
FS = ","
}
{
print $1 "\n" # same result with and without \n
}
How can I get a result similar to the manual command line with my little script ?
I must have made a mistake somewhere during my tests.
With the following, it works like a charm (but the netcat connexion is not closed thougH...)
BEGIN {
FS = ","
}
{
print $1
}
awk -f http-awk-check.awk $addresses_file | netcat -U /home/hduser/socket/rtop12 > toto

A bash loop to echo all possible ASCII characters [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 4 years ago.
Improve this question
I know how to print all letters
{a..z} and {A..Z} and {0..9}
But is there a way to print all possible ASCII Characters via a bash loop?
You don't need a loop
echo -e \\x{0..7}{{0..9},{A..F}}
It prints all chars from 0 to 127.
If it is okay to use awk:
awk 'BEGIN{for (i=32;i<127;i++) printf("%c", i)}'
Or using printf:
for((i=32;i<127;i++)) do printf "\x$(printf %x $i)"; done
use this:
for ((i=32;i<127;i++)) do printf "\\$(printf %03o "$i")"; done;printf "\n"

Variable quoting difference between bash 4.3.43 and 4.4.19 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 4 years ago.
Improve this question
Out of curiosity I wonder which change in bash (or head?) made this the following behaviour change,
In 4.4.19 we have the following behaviour,
# Assign "foo <new line> bar" to variabe 'var'
$ > var="foo
bar"
# Echo with and without quotes,
$ > echo "${var}"
foo
bar
$ > echo ${var}
foo bar
# Read all lines (1) except the last,
$ > head -1 <<< ${var}
foo
$ > head -1 <<< "${var}"
foo
Doing the exact same thing in bash 4.2.46 and 4.3.43 results in different output when reading the variable with head.
# Assign "foo <new line> bar" to variabe 'var'
$ > var="foo
bar"
# Echo with and without quotes,
$ > echo "${var}"
foo
bar
$ > echo ${var}
foo bar
$ > head -1 <<< ${var}
foo bar
$ > head -1 <<< "${var}"
foo
So it seems to me (with 4.4.19) that no matter if you quote the variable or not, head's input will be both lines. And with versions 4.2.46 and 4.3.43 the input actually differs depending on if you quote the variable or not.
The earlier behaviour makes sense to me, where you would have to quote the variable if you want the new line. I'm genuinely interested in this behaviour change and the reasoning behind it. I tried looking through the bash-changelog, but nothing obvious stood out to me that would lead to this change (although I have some very vague feeling that I've stumbled on this before).
Thanks in advance!
The behavior in bash 4.3 is a bug which is fixed in 4.4. See the original bug report and
Chet's reply (2015/09).
And the most recent posts to bash mailing list regarding this: the question and Chet's reply (2017/11).

How to hide the command when using command repetition with the exclamation mark? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
When I use ! to execute a previous command it automatically prints the command as well. Is there a way to hide this?
Example:
This is what happens:
ralgrad:~$ echo test
test
ralgrad:~$ !!
echo test
test
This is what I would want:
ralgrad:~$ echo test
test
ralgrad:~$ !!
test
I have looked at the bash source and there is no way to disable this automatic printing of the expanded command. You would have to compile your own version of bash!
If it is particularly important to you for whatever reason, look in bashhist.c in the pre_process_line function and comment out/remove the following line:
printf (stderr, "%s\n", history_value);
You cannot do that with !!, because it repeats the last command not the last output. As far as I know, there is no single command that allows to achieve what you are asking. However, you can try a simple hack:
result=`echo test`
echo "$result"
Well, you can simulate what you want with the following batch file:
if [ -z $1 ]; then
exe=`fc -lrn | awk 'NR==2 {print;}'`
else
exe=`fc -lrn | cut -f2- | awk 'NR>=2 {print;}' | grep "^ $*" | head -n1 `
fi
eval $exe
Let h be the name of the file (or, if you want it, even !). Then you can do:
# echo Foo
Foo
# echo Bar
Bar
# h
Bar
# h echo F
Foo

Resources