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 2 years ago.
Improve this question
A simple question :
What is the advantage of a test using a test operator versus one directly testing the variable?
As follows :
[ -n "$foo" ] && echo "that's some variable :"$foo || echo '$foo is blank.'
versus
[ $foo ] && echo "that's some variable :"$foo || echo '$foo is blank.'
The second is more elegant. But I suspect there must be a reason, because someone went to the trouble of making "-z" and "-n" exist.
I suspect [ "$foo" ] (quote the parameter expansion) is supported for historical reasons; once upon a time, saving two bytes by omitting the explicit, commonly used, -n could be significant.
Today, I would always use -n explicitly for clarity.
Note that you should not combine && and || for a "one-line" conditional statement. Use an if statement.
if [ -n "$foo" ]; then
echo "that's some variable: $foo"
else
echo "foo is blank."
fi
Related
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 1 year ago.
Improve this question
I've been working on some bash scripts and kind of hit a wall.
I need to ask a user to enter a bunch of different integers, and when they press 'control+a'
it'll sum up every integer they entered, like this:
10
10
2
"control+a"
22
I'm not sure where to even start on this.
I really appreciate the help, thank you.
If your terminal supports it:
#!/bin/bash
stty eof ^A
back2default(){ stty eof ^D; }
trap back2default EXIT
declare -i sum=0
while true; do
read -r foo
[[ -z $foo ]] && break
sum+=$foo
done
echo "sum: $sum"
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 2 years ago.
Improve this question
I want to check in Bash a Variable, if it contains a IP-Adress, otherwise the Script should exit with an errormessage.
For example :
The var "10.01.24.10" should pass, the var "hello" not, but the var "hello, my ip is 10.03.24.44" should pass not too.
My Idea would to use grep but I dont know how to build the grep command at this point.
Any Ideas?
You can incorporate the grep command in an if statement as below
ip="192.168.240.2"
if grep -Eq '^([0-9]{1,3}\.){3}([0-9]{1,3})' <<< "$ip"
then
echo "PASS"
else
echo "FAIL"
fi
You can also write the conditional statements as:
grep -Eq '^([0-9]{1,3}\.){3}([0-9]{1,3})' <<< "$ip" && echo "PASS" || echo "FAIL"
This uses && to signify success and || to signify failure
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 am trying to understand bash syntax a bit better, and I need some help with a while loop:
The following script works:
#!/bin/bash
#
counter=2
mystring=testdir
while [ $counter -le 5 ]; do
echo Making dir $mystring$counter
mkdir $mystring$counter
ls *.slurm > $mystring$counter/testfile.$counter.slurm
counter=$((counter+1))
done
Question 1: what is -le ? Google didn't seem to help, showing me any page with the word 'linux' in it
I literally copied the following form the while manual, and it does not work:
#!/bin/bash
set x 0
while {$x<10} {
puts "x is $x"
incr x
}
#test3.sh: line 6: syntax error near unexpected token `}'
#test3.sh: line 6: `}'
Question 2: What am I doing wrong? Thank you for your attention :)
The [ is a synonym for test builtin function and -le is one of the possible tests, lower or equal. In bash try this for more details:
help [
help test
Re. your second question, it doesn't look like bash syntax at all.
what is -le ?
it means "less than or equal".
What am I doing wrong?
Almost everything. Your script should look like this in bash
x=0
while [ $x -lt 10 ]; do
echo "x is $x"
((x++))
done
I think you really should read the bash manual.
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 years ago.
Improve this question
So I'm learning Bash and I have no idea why my if statement isn't working (working in putty if it makes any difference). I've tried looking online for how an if statement is supposed to be built and followed it making sure I got all the details correct. When I run it both of the if's come out positive and both of them get executed rather than just 1.
echo -n "Enter file name: "
read x
echo "file $x with numbers (Y/N)?"
read y
if [ "$y"="n" ];
then
cat $x
fi
if [ "$y"="y" ];
then
cat -n $x
fi
exit
can anyone help please?
Remember there needs to be a whitespace in your if condition else it becomes an assignment. https://uvesway.wordpress.com/2013/03/11/some-whitespace-pitfalls-in-bash-programming/
"$y"="n" is neither a conditional test nor an assignment. It's just a single string e.g, if $y is 'y', then "$y"="n" is the same as the literal 'y=n' and this literal string is being treated as a single parameter to the test ([) command.
A condition like below is always evaluated to true:
if [ abcd ]; then echo true; fi
while an empty string makes it false:
if [ '' ]; then echo true; else echo false; fi
White spaces aren't optional (everywhere) in bash. Even though the [ abcd thingy looks like a syntax, it's not; [ is a command (test) and abcd is its first positional parameter.
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 8 years ago.
Improve this question
I keep getting a "command not found" error after checking a variable for the substring .txt.
Here's a simple version of my script.
myscript.sh
#!/bin/sh
if [["$1" == *.txt]]
then
echo $1
fi
Result:
> ./myscript.sh argument.txt
./myscript.sh: line 2: [[argument.txt: command not found]]
The error is because of a space needed around the brackets [[ and ]]:
#!/bin/sh
if [[ "$1" == *.txt ]]
then
echo $1
fi
That is, instead of:
if [["$1" == *.txt]]
use
if [[ "$1" == *.txt ]]