Linux bash scripts [duplicate] - bash

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.

Related

BASH: for _i in /location/file*.log is looping when it should not? [duplicate]

This question already has answers here:
How to iterate files in directory with for loop in Bash
(4 answers)
Closed 8 months ago.
OK, this is blowing my mind a little. It's a stupid little nuance of BASH that is obviously eluding me. I write a logfile and I'm trying to increment it each time I run the program. There are probably easier and more practical ways to do this, but at a core level, I need to understand why the following code is doing what it is doing:
$ function stupid() { local lc=0; for i in /drvsoft/shred*.log; do (( lc+=1 )); done; echo $lc; }
$ stupid
1
$ ls /drvsoft/shred*.log
ls: cannot access '/drvsoft/shred*.log': No such file or directory
Is there something I don't understand about for i in loops? There is literally no i in; why is this incrementing?
If /drvsoft/shred*.log doesn't match any files, the word remains unchanged, i.e. the loop iterates once with i='/drvsoft/shred*.log'.
To change the behaviour, you can
shopt -s nullglob

Error in string comparison in if condition in bash shell [duplicate]

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.

Why If statement raises [[: not found error only when run indipendently? [duplicate]

This question already has answers here:
Bash syntax error: "[[: not found" [duplicate]
(3 answers)
Closed 2 years ago.
I am creating a script using bash. However during the IF statement it raises this error: [[: not found.
I read this topic in other posts but it seems that my predecessors were writing bad their code (e.g. forgetting spaces or else). My question is a little bit different because THE SAME code if run within other parts does not work but if I launch it totally alone it correctly works.
Why does this happens? I permit that the variable used is the only one along the whole code.
echo "Digit how many codon positions do you want to use for your partition. [2-3]"
read codonpos
echo $codonpos
[[ "$codonpos" = "2" ]] && echo im here
I also tried:
echo "Digit how many codon positions do you want to use for your partition. [2-3]"
read codonpos
echo $codonpos
if [[ "$codonpos" = "2" ]]
then
echo im here
fi
I repeat you that if launch independently it works but, if this is embedded in a larger code it doesn't.
I solved the error myself. The issue was due to the presence of an error in the shebang.
I wrote #!/bin/sh instead of #!/bin/bash.

Shell script variable reflection [duplicate]

This question already has answers here:
What is indirect expansion? What does ${!var*} mean?
(6 answers)
Closed 8 years ago.
I am writing a shell script (#!/bin/sh) which has a variable VAR which contains the name of another variable FOO, which in turn is set to BAR.
FOO=BAR
VAR=FOO
I want to get the value of the variable named in VAR, so something like:
echo "${$VAR}"
But that does not seem to work. Suggestions?
In Bash:
echo "${!VAR}"
Without Bash (though it works in Bash too):
eval echo "\${$VAR}"
Beware: eval is a very general mechanism that can run you into problems very easily. It works fine here, but be cautious about using it more generally.

What is $? in bash? [duplicate]

This question already has answers here:
What are the special dollar sign shell variables?
(4 answers)
Closed 8 years ago.
I am trying to understand a script that was written a few years ago by someone that is no longer available here. The script references $? a few times. What is this?
P.S. Google couldn't help since it seems to strip the $? from the search term
You really should read the GNU bash manual. $? is a special parameter which
expands to the exit status of the most recently executed foreground pipeline.
Read also the advanced bash scripting guide.

Resources