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.
Related
This question already has answers here:
Are shell scripts sensitive to encoding and line endings?
(14 answers)
Closed 1 year ago.
I am trying to paste two files together
file ip.txt
10.32.216.15
10.23.134.8
10.33.2.37
10.33.84.20
10.33.17.38
file obj.txt
obj-10.32.216.15
obj-10.23.134.8
obj-10.33.2.37
obj-10.33.84.20
obj-10.33.17.38
and I use the command like this:
paste ip.txt obj.txt
However I get this truncated output:
10.32.21obj-10.32.216.15
10.23.13obj-10.23.134.8
10.33.2.obj-10.33.2.37
10.33.84obj-10.33.84.20
10.33.17obj-10.33.17.38
The two files are created by grep command in my script earlier. This behavior is also present when I used variables with the command.
Also when I try to specify a delimiter only the second file gets pasted.
Does anyone know what might be the issue? Thanks
The issue was with line endings, for some reason Windows were set up but Unix were needed.
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.
This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 6 years ago.
I've been attempting to write a Bash script that automates everything needed to add a new piece of equipment to our MRTG graphs. Part of that requires me to edit a cfg file which I've read can be done with the sed command. The lines pasted below are where the error occurs when running the script giving me a "unexpected EOF while looking for matching `"' " error. town, tower, equipment, and direction are declared above. Any help in narrowing down what the problem might be would be a huge help!
newpattern="WorkDir: /var/www/html/mrtg/$town/$tower/$equipment$direction"
pattern="WorkDir: "
sudo sed -e "s/$pattern/$newpattern/" ~/MRTGconfigs/mrtg-BeatriceBSWT2960.cfg
You need to use something other than slashes in the s/// command because there are slashes galore in the replacement text:
newpattern="WorkDir: /var/www/html/mrtg/$town/$tower/$equipment$direction"
pattern="WorkDir: "
sudo sed -e "s%$pattern%$newpattern%" ~/MRTGconfigs/mrtg-BeatriceBSWT2960.cfg
I used % symbols instead; you can use any other character that appears neither in $pattern nor $newpattern. If need so be, you can use a control character such as Control-A; that works fine too.
This question already has an answer here:
Why would a correct shell script give a wrapped/truncated/corrupted error message? [duplicate]
(1 answer)
Closed 7 years ago.
I am trying to run this simple bash script:
file_name=deploy
echo "Init File NAme $file_name"
current_time=$(date "+%Y.%m.%d-%H.%M.%S")
echo "Current Time : $current_time"
new_fileName="${file_name}${current_time}.zip"
echo "New FileName: $new_fileName"
#echo $new_fileName ./app/code/community ./app/code/local ./app/design/frontend/indigo ./app/design/frontend/default
#zip $new_fileName ./app/code/community ./app/code/local ./app/design/frontend/indigo ./app/design/frontend/default
But for some reason I receive:
Init File NAme deploy
Current Time : 2015.10.01-16.04.02
./ManualPack.sh: line 5: $'\r': command not found
.zip5.10.01-16.04.02
I know its very very simple to any beginner in bash, but I tried for a decent amount of time, with many Stack Overflow threads to make it work, but the output remains the same.
You seem to have Windows styled line terminators in your file (\r\n). Converting the file with dos2unix should help.
This question already has answers here:
How can I debug a Bash script? [closed]
(12 answers)
Closed 8 years ago.
For example, I'm writing a bunch of iptables rules in a bash script. When I run the script, shell says iptables: No chain/target/match by that name. I don't know what's going on so I copy and paste every line into shell and run them separately to figure out which line is causing trouble. BTW. it turns out that I put "OUTUT" instead of "OUTPUT" in one rule.
Is there anyway that shell can tell me like [line 53]: iptables: No chain/target/match by that name., so I know where the problem is?
I'm not bash expert but what I was doing is adding echo with information regarding the progress and read (wait until keypressed) that will let me do the process step by step.
Nearly all programs return success and error conditions. You can include error checking for each program your script calls, and take appropriate action on error (like undoing previous work, exiting out, etc). This is particularly useful if line 4 should never execute if line 3 fails.
The exit status of the program you just called is stored in $? .
Example (pseudocode - you'll need to modify the syntax to be correct)
Iptables foo bar baz; if ($? != 0) echo 'failed to update iptables' && exit 1; fi
additionally, you can turn on various levels of tracing with set -f , set -v , and set -x . See the links below for full details.
http://tldp.org/LDP/abs/html/exit-status.html
http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html