Bash string concatenation producing weird results [duplicate] - bash

This question already has answers here:
Concatenating strings in bash overwrites them
(4 answers)
Closed 5 years ago.
My bash code file.sh :
username=$1
pkgpath="/home/${username}_tmp.txt"
echo $username
echo $pkgpath
Now running the script with the command bash file.sh abc should produce the result :
abc
/home/abc_tmp.txt
But the output I'm getting is :
abc
_tmp.txtc
Can someone explain why is this behavior occurring and how to obtain the desired result ?
EDIT
I'd like to mention that using pkgpath="/home/${username}" gives me /home/abc (desired) but running pkgpath="${username}_tmp.txt"gives me _tmp.txt(weird).

Looks like you are somehow inserting a carriage return character after abc when you run the command bash file abc. The culprit is probably either your terminal, or you are copy pasting the command and are including ^M without realizing.
So what bash is outputting on the second line is really /home/abc^M_tmp.txt, which gets rendered as _tmp.txtc. You can easily verify this by piping the output of your command to less -R.

Related

Wierd bash behaiviour with vim [duplicate]

This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Why does shell ignore quoting characters in arguments passed to it through variables? [duplicate]
(3 answers)
Closed 7 months ago.
To recreate this you will need to create a file named "l l.tex" in your home directory. Then make a an executable script with the following code
alacritty -e vim l\ l.tex
This opens the file, as I desired. Now create the following script
var="/home/l\ l.tex"
dar=$(echo $var | sed 's/\/home\//\#/g')
bar=$(echo $dar | sed 's/#//g')
alacritty -e vim $bar
This script, however, makes two new documents. However, they should be the same. I think there is an issue with how $bar is read. I am novice at this, but I reckon that this has to do with how strings are stored.
Can someone help me fix the second code so that I can use a variable?

How can I simply write all console output of a command to a file, in the terminal? [duplicate]

This question already has answers here:
print the output of strace command in a text file
(1 answer)
How to redirect and append both standard output and standard error to a file with Bash
(8 answers)
Closed 1 year ago.
I am trying to send all printed output of a strace command to a file. straces have a lot of console output since they print all the system calls of a command.
I have tried the most popular ways that I can find or think of, such as command > file.txt which does print output but then the output is not written to a file, same with command | tee file.txt Output is always printed but never written to a file. Is it because there is too much output or something? The first command definitely suceeds.
But my real question is what is the best way to do this and how can I get it done?
Many many thanks,
Milan

Composing a string in script shell [duplicate]

This question already has answers here:
How to concatenate string variables in Bash
(30 answers)
Closed 1 year ago.
So I'm trying to compose a string using script shell
#!/bin/sh
blNumber=1111619832
echo '*'$blNumber+='*.xlsx'
I expect the output to be: *1111619832*.xlsx
but as a result I get: *+=*.xlsx
Btw I tried to figure out why so I tried this code
#!/bin/sh
blNumber=1111619832
output="*${blNumber}"
and whenever I add something after *${blNumber} it get concatenated at the begging of the string
Why are you using += in the first place?
$ echo '*'$blNumber'*.xlsx'
*1111619832*.xlsx
Or put it inside double-quotes. It's best practice to quote all variables anyway.
$ echo "*$blNumber*.xlsx"
*1111619832*.xlsx

BASH echo showing some unusual behaviour [duplicate]

This question already has answers here:
Strange "echo" behavior in shell script
(3 answers)
Variables overwriting text problem with "echo" in Bash
(2 answers)
Closed 3 years ago.
This is the simple command that is giving me problems -
echo hafsda sfsdfdsfs $ymn $ymx $range
The output of this command is coming -
2.568 sfsdfdsfs 86.72
Where ymn = 86.72 ymx = 89.28 and range = 2.56. This only happens when I am using variables. The following command works fine -
echo hafsda sfsdfdsfs 1 2 $range
Also, the same command (the first one) works fine if I try running it directly in the terminal. This is only happening is a script. I also tried to use printf but encountered similar results.
I don't even understand what to google for to resolve this. I am unable to understand what is happening at all. So, what is happening here? Is this reproducible or is this just some error on my system, and if it is, what might be the problem?
Your script probably has DOS-style CRLF line endings. I suspect you actually have ymn="86.72\r" ymx="89.28\r" and range="2.56\r". You can test this in your script with
echo hafsda sfsdfdsfs $ymn $ymx $range | od -c
You can fix your script with dos2unix or sed -i 's/\r$// script.sh`.
Make sure you change the settings of your text editor do use unix line endings.

Run Shell Script from Ruby File and Capture the Output [duplicate]

This question already has answers here:
Getting output of system() calls in Ruby
(18 answers)
Closed 8 years ago.
I would like to run a shell script from my ruby file. Then capture its output and analyze it.
This is the scenario:
- Within my ruby script I need to execute the my_script.sh
- The shell script produce the followinf output in the terminal
> my_script.sh
xxxxx 1111
yyyyy 2222
zzzzz 3333
I need to capture and analyse this ouput inside the ruby script in order to find if a keyword is displayed (e.g., yyyy).
I'm using the following comand:
my_script = "/home/script.sh"
system("sh #{my_script}")
I'm not able to capture the output produced in the terminal for parsing
Don't use system, it does not capture STDOUT. Use backticks (or %x()):
output = %x( #{my_script} )

Resources