This question already has answers here:
Bash: using the result of a diff in a if statement
(5 answers)
Closed 7 years ago.
I'm using a diff operation to check a couple of files, but am wondering if I can simultaneiously use it to trigger an event (e.g. echo-ing a line to terminal etc) without having to 'manually' check for the existence of the output report.
I had thought about using the report creation as a trigger, but as far as I know the file would be created regardless, it would just be empty in the event of no differences?
Basically, is it possible to have diff output a file, whilst inside an if statement, to keep my bash profile and the script itself as tidy as possible?
(If it helps, this is a follow on question to the wonderful help I received in this question: Notifications on next ssh login)
e.g. something to this effect?:
if [ diff filex filey > report.txt == true ]
# So the report.txt is created but the 'state' of
# the diff query is preserved and evaluated...
then echo "Files are different"
else
break
fi
Hope that makes sense.
This is in Ubuntu bash FYI.
Yes, diff exits with a status of 0 if there is no diffs (that's shell for "true"), so you can do it like this:
if ! diff <file1> <file2>
then
do the thing when there are diffs
else
do the thing when all is same
fi
This should work even if you redirect the output.
Related
This question already has answers here:
Are shell scripts sensitive to encoding and line endings?
(14 answers)
Closed 1 year ago.
I need to read some properties from a config file and execute some commands if the properties match certain values.
But when I try to compare the value with a string in if condition, it doesn't work.
Below is a small version of what I am trying to do.
#!/bin/bash
source config.file
echo "mode: $mode"
if [ "$mode" = "slow" ];
then
echo "Mode is slow"
fi
The config file looks like this.
###config###
mode=slow
user=admin
password=pwd
###end###
The echo statement prints the values as "slow" but the if condition is never satisfied.
What am I doing wrong?
It works for me. Maybe you have MSWin line ends in the config file?
Run
dos2unix config.file
or
fromdos config.file
to convert them to the *nix standard.
This question already has answers here:
Ubuntu (14 & 16) Bash errors with printf loops from input containing lowercase "n" characters
(3 answers)
Closed 5 years ago.
Travis CI seems to be misbehaving very oddly since yesterday. I know they pushed out an update on the 21st June 2017 to Travis but this issue only started happening yesterday to me causing a total mess of one of my repo's but luckily I reverted back to the last commit where everything was still OK and have done no more commits since then just been running tests on a different test repo all day today and it's still broken.
During my Travis build process I run a number of bash scripts that do various things like create files based on templates. But since yesterday the output of any echo or printf see's any "n" character as a line break \n
For example:
This simple printf in my bash script:
for line in $(cat $INPUT1); do
printf "\t\"~${line}\"\t\t$ACTION1\n" >> whateverfile.txt
My inputs are defined earlier as:
ACTION1="0;"
INPUT1=$TRAVIS_BUILD_DIR/myfile.txt
Travis breaks the output of any ${line} which contains an "n" in the output.
So for example a ${line} from the cat statement that contains the word bingbot for instance should output as follows:
"~bingbot" 0;
but Travis is doing this because it see's any n character as a line break, this below is the output of running the script with sh -x ./scriptname
bi"\t\t0;\n
+ printf \t"~gbot
and in the actual output file this is what travis prints with printf
bi" 0;
"~gbot
Anyone else seeing this behavior? I have tweeted and emailed them but no response yet. Tried various things to fix it and in some cases capitalising all my variables, inputs, filenames and directories fixes it but that is not a solution. What has Travis done to simple bash? or am I missing something. This has worked perfectly for over a year.
Note: my default scripts all run using #!/bin/bash I have changed them to use #!/bin/sh but it is still happening.
I solved this thanks to the help of a number of people in this question - Ubuntu (14 & 16) Bash errors with printf loops from input containing lowercase "n" characters
It wasn't a Travis CI issue after all. It's all explained in the link above.
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
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I test if a variable is a number in bash?
I am new to bash scripts. Needed a code to check whether a variable is a number, if YES i have to keep it as it, if NO i ll have to reassign it to some other value. How do I get this done?
Your basic if () then ... fi. Here's the format:
#!/bin/bash
var=2
if [[ $var != 1 ]]
then
var=5;
fi
echo $var
Note that the [[...]] format can actually be one of many things.
As with everything, if you want to get good at it, you'll have to work for it.
Type
man bash
man test
to get the manuals on the tools you're using. Read them top to bottom, again, again, and once more. And then tomorrow the same thing.
This question already has answers here:
Bash and Test-Driven Development
(8 answers)
Unit testing Bash scripts
(16 answers)
Closed 5 years ago.
Can someone explain how to test for a bash shell script?
For example i've got a .sh file with this code in it...
#!/bin/sh
for file in *.txt; do
mv "$file" "`basename $file .txt`.doc"
done
How do I write a test for it? Like in Java you've got unit testing where you write code like assertEquals to test the code gives the desired output.
Try this out: assert.sh
source "./assert.sh"
local expected actual
expected="Hello"
actual="World!"
assert_eq "$expected" "$actual" "not equivalent!"
# => x Hello == World :: not equivalent!
You can do asserts in Bash. Check out this from the Advanced Bash-Scripting Guide:
http://tldp.org/LDP/abs/html/debugging.html#ASSERT
I'd add an echo in front of the mv to verify that the right commands are being created, for starters. (Always a good idea with commands that make possibly difficult to undo changes.)
Some possibly useful resources:
shUnit2 — xUnit framework for script unit testing
Bash IDE for Vim
Bash debugger