We’re rewarding the question askers & reputations are being recalculated! Read more.

Questions tagged [test]

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.

Filter by
Sorted by
Tagged with
253
votes
6answers
95k views

What is the difference between the Bash operators [[ vs [ vs ( vs ((?

I am a little bit confused on what do these operators do differently when used in bash (brackets, double brackets, parenthesis and double parenthesis). [[ , [ , ( , (( I have seen people use them on ...
2
votes
1answer
748 views

I want to compare two list and print the difference output from list one

I have two variables input: VAR1="abc red blue cat empty dummy rummy" VAR2="rummy zero empty rat cat reverse" output: I want output as follows: (remove the common ones from $VAR2 in $VAR1) abc red ...
-2
votes
1answer
2k views

Redirect error message to /dev/null in if condition [closed]

I have the following if statement: if [ "$sourcelast" = "$maxhostlast" ] || [ "$sourcelast" = "$minhostlast" ] || [ "$sourcelast" < "$maxhostlast" ] || [ "$sourcelast" > "$minhostlast" ] ;then ....
0
votes
2answers
1k views

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

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 ...
4
votes
2answers
11k 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 looks ...
2
votes
2answers
6k 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 ...
5
votes
1answer
390 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 work ...
13
votes
1answer
748 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?
6
votes
2answers
31k 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 ...
-3
votes
3answers
499 views

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

My code need compare "stop" with stop this is stand bash string. pi@raspberrypi:~/Voice $ ./test.sh | more "stop" stop My code: #!/bin/bash command=stop while : do QUESTION=$(cat stt.txt) #stt,...
0
votes
1answer
38 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
3answers
749 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 ...
1
vote
4answers
568 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
351 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
3k 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 0 ...
9
votes
3answers
13k 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 ...
3
votes
1answer
718 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
71 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. ...
6
votes
2answers
21k 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 ...
2
votes
3answers
452 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 ...
19
votes
3answers
48k 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 arg1 is equal ...
2
votes
1answer
2k 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=...
3
votes
2answers
381 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; ...
2
votes
3answers
505 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
1k 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
181 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 the ...
11
votes
1answer
640 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 [ reliable ...
1
vote
1answer
132 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
57 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 ... ]...
2
votes
1answer
3k 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
398 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
55 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
31 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
425 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 ...
1
vote
1answer
3k 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 ...
-1
votes
1answer
93 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 ...
27
votes
3answers
6k views

What is the purpose of square bracket executable

I see there is an executable called "[" in /usr/bin. What is its purpose?
8
votes
4answers
2k 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 (/usr/...
1
vote
2answers
1k 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 ...
9
votes
2answers
1k 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
898 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
4k 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
945 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
3k 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 ...
2
votes
2answers
293 views

What's wrong with my file check script?

I don't understand why the last part of my script does not display anything when no file or directory is found. echo -n "Please enter a filename or directory: " read filename if [ -z "$filename" ]; ...
5
votes
4answers
10k 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
759 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
294 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
4k 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
108 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 "...