This is about the Unix utility "test," also invoked as "[", or its shell syntax [[ … ]] variant. For questions about testing software and setups, use the "testing" tag.

learn more… | top users | synonyms

0
votes
2answers
29 views

Preform operation in bash only if a variable is less than a second variable

I am building an OS for a virtual machine I made, and while progress has been coming along swimmingly, I have been stuck for a good several hours on what should be a very simple issue: I am designing ...
2
votes
2answers
35 views

how to check variable existence and compare with the string in busybox?

I want to use something like that if [[ ! -z "$ENV" && $ENV == 'production' ]]; then echo "production"; else echo "dev"; fi but in BusyBox it does not work :( sh: 1: [[: not found It ...
2
votes
2answers
54 views

How to assign value to input variable in shell

I am asking user for input and taking input in the variables such as $1, $2 etc. I am checking the variable for null value and wants to replace the value if null. if [ "$2" == "" ]; then 2=value fi ...
6
votes
1answer
267 views

Check if variable equals open parenthesis

I'm currently trying to figure this out. if [ $CHAR == ( ] Obviously, this doesn't work. So how do I force it to do what I obviously intended? (For bonus points, how do I get the comparison to ...
12
votes
1answer
120 views

[[ and case equivalency in bash

Does if [[ "$1" = pattern ]]; then hook fi always behave the same as case "$1" in pattern) hook;; esac or are there any gotchas?
4
votes
2answers
41 views

Bash - Integer expression expected

I am making the check for update script for my theme I have 2 text files. First one is called "current.txt" and contains the current version. There is 4.1.1 string in that text file. Second one is ...
-2
votes
2answers
59 views

How to compare “abc” with abc in a shell script?

I did not find the answer other location, so seek help here. My code need compare "stop" with stop this is stand bash string. pi@raspberrypi:~/Voice $ ./test.sh | more "stop" stop My code: #!/bin/...
0
votes
1answer
37 views

negative values are being treated as positive in RHEL suddenly. Why?

For example: # [[ -24 > 0 ]] && echo "Hi" Hi Since yesterday, I found this weird behavior. What could be the reason?
3
votes
3answers
44 views

Proper type casting in a shell script for use with while loop and modulus

I am trying to write a script to get a random, even hex number. I have found the the openssl command has a convenient option for creating random hex numbers. Unfortunately, I need it to be even and my ...
2
votes
4answers
250 views

How to match “4.4” from `uname -r`?

Pseudocode [ `uname -r` =~ ^4\.4.*$ ] && echo "yes" which is, however, unsuccessful in all cases. The uname -r command gives things like 4.6.0-040600-generic as an output.
2
votes
3answers
70 views

Why is my elif being treated as an else statement in my bash script?

I'm building a bash script that uses wget to GET information from a server using a REST api. I'm using getopts to parse options given to the script and then using an if statement to redirect the ...
14
votes
3answers
2k views

bash: I broke [[ < ]]

I was writing a bash script and suddenly this behaviour started: [[ 1 < 2 ]]; echo $? # outputs 0 [[ 2 < 13 ]]; echo $? # outputs 1 but -lt works soundly: [[ 1 -lt 2 ]]; echo $? # outputs ...
1
vote
1answer
90 views

How to compare a program's version in a shell script?

Suppose I want to compare gcc version to see whether the system has the minimum version installed or not. To check the gcc version, I executed the following gcc --version | head -n1 | cut -d" " -f4 ...
2
votes
1answer
34 views

( test -n $st ) != ( test -z $st ) right?

As I read, test -n $string ==> Exit status is 0 if $string is not null, 1 otherwise and test -z $string ==> Exit status is 0 if $string is null, 1 otherwise But in this particular example, (I ...
0
votes
1answer
33 views

Pattern matching on if block

I'm trying to match a pattern stored in a variable through an if block. As per my understanding, this should be a match and get "Match" echo statement. However, it doesn't seems to be matching. ...
1
vote
2answers
89 views

sh AND and OR in one command

Trying to check for 3 conditions in one line of code, but I'm stuck. Essentially, I need to code for the following: IF string1 is not equal to string2 AND string3 is not equal to ...
3
votes
3answers
103 views

Display files to size with lower and upper bound without using `find`

I need to create a program via vim in Linux called between. The script takes two arguments as the lower bound and upper bound of the file size, and prints out a list of file names as well as their ...
0
votes
3answers
110 views

Comparing integers: arithmetic expression or conditional expression

In Bash, two integers can be compared using conditional expression arg1 OP arg2 OP is one of ‘-eq’, ‘-ne’, ‘-lt’, ‘-le’, ‘-gt’, or ‘-ge’. These arithmetic binary operators return true if ...
3
votes
1answer
83 views

integer expression expected

I have the following script that is trying to match and IP address with a value in a file #!/usr/bin/env bash #To find the IP via ping ping=$(ping federicolivieri.noip.me -c 1) #To cut the result ip=...
2
votes
2answers
50 views

What's the difference between 'test' and evaluating with /dev/null?

I noticed different .sh scripts for homebrew which check whether it's installed. One uses this syntax: if test ! $(which brew); then The other uses this syntax: if ! which brew > /dev/null; ...
1
vote
3answers
60 views

Rename only one file in each directory, based on condition

I have 100 folders inside every folder I have one or two files named as the following: XXX_001_014_max.jpg XXX_001_024_max.jpg I saved the folders names in a file "list.txt" I ran the following ...
0
votes
2answers
84 views

Test bash if conditions in the terminal [closed]

I'm developing a bash script and am spending most of my time on debugging the if condition statements. The reference states: There exists a dedicated command called [ (left bracket special ...
1
vote
2answers
73 views

How to understand this POSIX description about `(`, `)` and `test`?

From POSIX 2013: The XSI extensions specifying the -a and -o binary primaries and the '(' and ')' operators have been marked obsolescent. (Many expressions using them are ambiguously defined by ...
12
votes
1answer
565 views

never use the `-a` or `-o` operator with `[`

Stéphane Chazelas wrote: a few rules like always quote variables never use the -a or -o operator (use several [ commands and the && and || shell operators) Make [ ...
1
vote
1answer
32 views

Difference in conditions /test

Lets say i have to test if file contains a string "mail" , what is the difference between if grep -q "mail" "file"; then ... if [ $( grep -l "mail" "file") ]; then ... [ grep -l "mail" "file" ] &...
1
vote
0answers
18 views

problems with running tcsh file

I can't get the following program to run. When I try running it, it says set goal = `head -c 15 < /dev/urandom | base64 | sed 's/[^0-9]//g' | sed' Usage: tcsh [ -bcdefilmnqstvVxX ] [ argument ... ]...
0
votes
1answer
208 views

How to use negation of a command in a while loop with grep in shell script?

Is there any way to use while loop and grep all together? See my example: while [[ !(grep -R -h "${text}" ${path}) ]]; do ... done It says: ./test_script.sh: line 1: conditional binary ...
3
votes
2answers
83 views

What does '-size +0' mean in shell 'test' command

I came across a if statement like this: if [ -f <file path> -size +0 ] I know that -f checks for file existed, but what does this -size +0 do here? I can't get help form man page also tried ...
1
vote
0answers
36 views

Conditional expressions in shell: What are the differences between [ vs [[ [duplicate]

I just wondered about some people writing if [[ <expr> ]]; then ... in answers here when I out of habit would use if [ <expr> ]; then ... Doing a quick research reveals that [[ ]] ...
-1
votes
1answer
20 views

-n Vs !(exclamation mark) behaves differently with test command

#!/bin/bash declare -A numMap numMap[1]=1 #case-one if ! [[ ${numMap[1]} ]];then echo "case-one: the key 1 for numMap array is not set" fi #case-two if [[ -n ${numMap[1]} ]]; then echo "case-two: ...
1
vote
1answer
69 views

Catch unexpected input on single character read in bash [duplicate]

I have a bash script, which, at one point asks the user for confirmation. I do this by reading a single character, which is then transformed to lower case and checked if it is 'y'. If not, the script ...
3
votes
1answer
86 views

Difference between '>' and '-gt'

What's the difference between these: SEC=$(mysql -hxxx -Pxxx -uxxx -pxxx -se "SELECT Seconds FROM tablename") SEC=100 Both return the same result (100). But when I test it with: if [ ${SEC} > ...
-1
votes
1answer
26 views

script for automount, using if statement. cannot got the result [duplicate]

This script should auto mount a file system on a Linux server. testcheck=`df -h | awk '{print $6}' | grep "/test"`; if [$tescheck -ne "/test"] then mount /test else echo "failed"; fi I'm ...
18
votes
3answers
1k views

What is the purpose of square bracket executable

I see there is an executable called "[" in /usr/bin. What is its purpose?
5
votes
4answers
643 views

Why does `==` behave differently inside `[ … ]` in zsh and bash?

I get what I expected when doing this in bash: [ "a" == "a" ] && echo yes It gave me yes. But when I do this in zsh, I get the following: zsh: = not found Why does the same command (/...
1
vote
2answers
162 views

Understanding redirected if-statement in bash

if [ -z "$OPTION" ] # if option is not given(empty) then: then command1 --defaultOption else command1 $OPTION fi \ 2> >( function1 "$DETAILS" ) \ < <( command2 ...
7
votes
2answers
536 views

Why does this bash conditional check work with [[ -n .. ]] but not [ -n .. ]? [duplicate]

I have a script that fails to detect zero length strings,the script uses [ -n $value ] in a bash conditional expression, i.e. #!/usr/bin/env bash value="" if [ -n $value ] then echo "value is ...
0
votes
1answer
120 views

Linux “stress” tool

I have seen one of the trick to use 90% of the available system memory with following command : stress --vm-bytes $(awk '/MemFree/{printf "%d\n", $2 * 0.9;}' < /proc/meminfo)k --vm-keep -m 1. ...
2
votes
2answers
267 views

Comparing strings with special characters using if-loop does not work

I want to compare each line of a particular file with the following string #orb_plugins = ["local_log_stream", "iiop_profile", "giop", "iiop"]; ("file.txt" contains this particular line) I tried ...
2
votes
1answer
330 views

shell: The =~ operator does not work as expected

Consider the following script: #! /bin/bash line="confusing" if [[ $line =~ [[:lower:]]* ]] then echo "matches!" else echo "does not match." fi While the above produce the expected result,...
2
votes
2answers
152 views

Help with script reading numbers from a file and determining if they are even or odd numbers

So I want my script to test for the existence of a file which I already populated with numbers. It does that, but after it does that I want it to run a while look checking to see if the numbers in the ...
0
votes
2answers
178 views

Print condition result directly in bash without using if

Let's say I have this simple code: echo "Are there any arguments?" if [ $# -eq 0 ]; then echo "false" else echo "true" fi As you can see it would be better to just have opportunity to ...
2
votes
2answers
91 views

Bash [[ pattern matching not working

The Bash reference section on conditional constructs says: When the ‘==’ and ‘!=’ operators are used, the string to the right of the operator is considered a pattern and matched according to the ...
4
votes
2answers
266 views

Bash Logic backwards on one system, but not another

I have two linux systems. I am testing for the existence of a program on each system. One system has the program installed, another does not. Somehow, each system evaluates the following example the ...
0
votes
1answer
60 views

If and else statement using diff -q

So I'm trying to write an if else statement which involves the use of diff -q. So, let's say I have two files hi.txt and hello.txt and I store them into variables called hi and hello respectively. I ...
0
votes
2answers
63 views

Problem with data comparison in if loop

I have 2 variables in bash. Using test i found out that one of them is integer and other one is string. I want to compare them and see if they are equal. I tried the following options. if [ "$a" -eq "...
2
votes
2answers
136 views

while-loop doesn't seem to remember variable update - no piping involved

I am running LMDE 2 ( 'Betsy') with the standard Mate desktop and I have the following script: #!/bin/bash # This script starts a specified terminal-binary in "Always on Top"-mode # The assumption is,...
3
votes
1answer
1k views

How to use user input as a while loop condition

I can do this in bash: while read -n1 -r -p "choose [y]es|[n]o" do if [[ $REPLY == q ]]; then break; else #whatever fi done which works but seems a bit redundant, ...
2
votes
1answer
1k views

How do I test if a file exists using ZSH? [duplicate]

This question is about ZSH, not bash. I have the following lines in my .zshrc file. Whenever I open a terminal I get a no matches found error referencing the line with the if statement. if [[!( -a ~...
7
votes
3answers
264 views

What does `[ EXPRESSION ], [ ] and [OPTION` mean in `man test`?

Examining man test, I see that under synopsis for test are the possibilities test EXPRESSION and test. What does [ EXPRESSION ], [ ] and [OPTION mean below ? Why are the brackets empty and why does ...