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
1answer
35 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?
2
votes
4answers
241 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
66 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 ...
2
votes
1answer
30 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
29 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
85 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
95 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
74 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
50 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
42 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
49 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
72 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
72 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
564 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
31 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
145 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
82 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
19 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
65 views

Catch unexpected input on single character read in bash

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
77 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
25 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
642 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
135 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
529 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
100 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
217 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
318 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
124 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
151 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
82 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
58 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
126 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
941 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, ...
1
vote
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
262 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 ...
0
votes
4answers
522 views

If condition with ssh command inside

I'm doing a bash script to backup my computer to a local server. I need to compress the archives but I can't find a way to make this if condition work with an ssh command inside: if [ ssh user@192....
-1
votes
3answers
192 views

Why does the builtin `test` fail with 'less than' (<) and 'greater than' (>)?

This code: #!/bin/bash if [ "4" = "a" ]; then echo "equal"; fi if [ "4" > "a" ]; then echo "yes 1"; fi if [ "4" < "a" ]; then echo "yes 2"; fi echo "exit" prints this three lines: yes 1 yes ...
3
votes
1answer
209 views

Bash test on successful subshell command returns false

I don't quite understand this behaviour of bash test operator. I used this line to determine if vagrant is installed. if [ $(vagrant --version > /dev/null) ]; then echo "HELLO"; fi I now know ...
2
votes
1answer
658 views

arithmetic syntax error in string compare

I have written a shell script which takes file name as parameter e.g user/test.txt. I want to make this file parameter optional if user does not wish to provide file name he/she can give as "None" ...
0
votes
1answer
43 views

Print the values from files

I have two files $TMP_RPT_FILE and $TMP_RPT_FILE1 . In $TMP_RPT_FILE - Meter readings come and $TMP_RPT_FILE1 - Meter Id come . I want if any data came in any of those files it should print . I tried ...
1
vote
2answers
362 views

What does [[ -t 1 ]] do?

To my understanding, the script below runs if the user is not root by comparing $EUID and 0. Then, it uses [[ -t 1 ]] to decide if the script is running in a terminal or not. If it is, it will use ...
3
votes
1answer
46 views

Condition on finding a file

I'm trying to write a script which looks for a file and if it is found, saves the full path to a variable. Current version: if [[ -n FOUND_PATH=$(find "$PATH" -name "file.exe") ]]; then echo "...
0
votes
1answer
185 views

Numeric test in bash: integer expression expected

I created a simple script in bash language to do cleaning of the recycle bin in Ubuntu. here is the script "recycle.sh" #!/bin/bash PUBLIC_PATH="/home/fileserv/.recycle/Public" ##### Clean public ...
6
votes
3answers
233 views

Peculiar Solaris integer comparison behaviour

This one caught me unawares! We have a Solaris machine (version: SunOS 5.8) hosting a pivotal script/process which has been running for many years without complaint. We recently had cause to have a ...