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
2 votes
2 answers
32 views

Test fails (correctly) on command line, but succeeds (incorrectly) in script

For the sake of this question, assume that the glob /a/b/c/* produces no matches. This means that the following test should fail (in other words, it should produce a non-zero $?): [[ -n /a/b/c/*(#qN) ]...
user avatar
  • 12.8k
4 votes
2 answers
309 views

Meaning of the statement - test -x /usr/bin/find || exit 0

I just wanted to understand the meaning of the following statement and whether they seem correct. test -x /usr/bin/find || exit 0 Command 1 Command 2 Command 3 The output of test -x /usr/bin/find is ...
user avatar
0 votes
1 answer
50 views

test -R True if the shell variable VAR is set and is a name reference

First I follow this answer then I search about test -v then https://linuxcommand.org/lc3_man_pages/testh.html shows that there is an R option. test -R seems related to name preference. Then I search ...
user avatar
  • 411
7 votes
1 answer
292 views

Unary test -v on an array element

In bash, the conditional expression with the unary test -v myvariable tests wether the variable myvariable has been set. Note that myvariable should not be expanded by prefixing it with a dollar, so ...
user avatar
  • 277
16 votes
2 answers
1k views

Bash negation of -a (file exists) does not change result, whereas for ! -e changes result

I'm perplexed but still guess I misunderstood Bash somehow. /$ if [ -e /bin/grep ]; then echo yea; else echo nay ; fi yea /$ if [ ! -e /bin/grep ]; then echo yea; else echo nay ; fi nay /$ if [ -a ...
user avatar
0 votes
0 answers
20 views

PHY (Backplane) doesn't link to my NIC

I need to find out something about the auto-negotiation because the PHY VSC8489 doesn't build a link with my NIC. Maybe this is because the auto-negotiation isn't successful regarding the supported ...
user avatar
0 votes
1 answer
833 views

"if pgrep" no longer works if I use [[ or [

I'm trying to detect if a process (goland.sh) is running. I used this: #!/bin/bash if pgrep "goland.sh" >/dev/null 2>&1 ; then echo "running" exit 1 fi echo &...
user avatar
  • 304
2 votes
1 answer
154 views

Difference between if [ ... and test ... statement in bash

Consider the following: echo "hello" > file.txt is_match1 () { local m m=$(cat "file.txt" | grep -F "$1") if [ -z "$m" ]; then return 1 fi } ...
user avatar
  • 367
8 votes
1 answer
702 views

Why is Bash not evaluating the executable bit correctly in Alpine 3.14.2?

In a dockerized Alpine 3.14.2 running Bash 5.1.4(1), I encountered a strange behaviour in a Bash script. The essential line is a test for executability of a file, which always fails. This is the if ...
user avatar
  • 151
0 votes
1 answer
75 views

how to nest conditional script operators -a -o in an if statement in bash

If i want to combine the -a and -o script operators in an if statement, how could I do that? For example: if [ ( -e file.txt -a -r file.txt ) -o ( -e file2.txt -a -r file2.txt ) ]; then .... fi Can I ...
user avatar
  • 11
0 votes
0 answers
34 views

Why is the test command output backwards? [duplicate]

If I run help test , I get this: Evaluate conditional expression. Exits with a status of 0 (true) or 1 (false) depending on the evaluation of EXPR. Why is this so counterintuitive? Why does it ...
user avatar
  • 101
2 votes
3 answers
5k views

Test if a command's output is an empty string and print its results if its not empty [duplicate]

I am using solutions from Test if a command outputs an empty string. In addition to those solutions, I want to print command's output if its not empty. I was wondering would it be possible to print ...
user avatar
  • 230
0 votes
0 answers
80 views

SMART Did extended test run succesfully?

Problem I have a brand new WD Elements 4 TB disk. Before starting to use the disk, I ran an extended SMART test. To my understanding, this should examine the complete disk surface. However, SMART ...
user avatar
  • 1
0 votes
3 answers
114 views

Is parameter expansion in test expression possible?

When I try the following expression in bash I get a strange error message: [: -lt: unary operator expected First the function definition some_func () { (( 3 + 5 )) } And the expression [ $(...
user avatar
  • 303
0 votes
1 answer
282 views

How to limit the length of a user input in a bash script?

I am trying to create a simple bash script to read user input and use this input as a variable within my script. It currently looks like this : echo "Please specify file:" read FILE What I ...
user avatar
2 votes
1 answer
179 views

Why is not 'test -z =' an error after all?

I have a book that stated: "....For example, if the shell variable symbol contains an equal sign, look at what happens if you try to test it for zero length: $ echo $symbol = $ test -z "$...
user avatar
  • 161
-3 votes
3 answers
145 views

How can i test something like if it's the format of car plate?

I need to test if an input it has the fromat of a car plate (0000-XYZ) and from the japanese ZIP wich the format is 000-0000
user avatar
4 votes
1 answer
473 views

Grep command not returning expected results for testing

I am attempting to do a comparison before I append conditions to a profile.d file A grep -F 'TMOUT' /etc/profile.d/sh.local shows me exactly what I expect however the test is always showing true. In ...
user avatar
  • 43
1 vote
1 answer
513 views

bash can't find closing bracket?

I am writing a bash script to makup the MariaDB instance on a Synology box. When I run it, it works, but I get an error: admin@synology:/volume1/@appstore/MariaDB10/usr/local/bip$ ./mysql_backup ./...
user avatar
  • 4,225
1 vote
2 answers
117 views

Using test and date to compare current hour ro range

I am new to Gnu/Linux and bash, and I am trying, unsuccessfully, to write a simple bash script to test if date +%H is within a predefined range of hours. Example: hour='date +%H' if [[ $hour -ge 12 ]] ...
user avatar
  • 13
0 votes
0 answers
185 views

How to inject command into test command

I have a self written script like (./myscript) and I want to call some commands in that script. So I want to learn how to inject commands into a shell script like this one: #!/bin/bash set -x if ...
user avatar
0 votes
0 answers
112 views

How can I make a dummy directory tree with dummy files that contain realistic data?

I'm looking for preferably a one-liner that drops a directory tree a few layers deep with files that contain "realistic" data, as in, not /dev/zero nor /dev/urandom. Data that noticeably ...
user avatar
  • 293
-1 votes
1 answer
195 views

variable regex test in dash?

I got plenty possible variables in a form of [a-z][0-9], eg 1 letter and 1 number. Is there a way to test them in dash? Something like: myvar=o7 if [ $myvar=o[0-4] ]; then "do this" elif [ $...
user avatar
0 votes
3 answers
176 views

How do i use the && and || operators with more than two variables correctly?

I'm trying to compare the state of more than two variables. The use-case is a script that, among other options, should select (or auto-select) only one of several "modes" based on available ...
user avatar
4 votes
3 answers
742 views

Is it possible to have conditionals in /etc/rc.local?

Is it possible to have a conditional within /etc/rc.local? I've checked many Q&As and most people suggest running chmod +x on it, but my problem is different. It actually does work for me without ...
user avatar
  • 471
2 votes
1 answer
472 views

What is the difference between square brackets and test command on bash?

Single square brackets [ and testcommand both are built in the bash shell. Why are there two separate commands if both square brackets [ and test commands have the same function?
user avatar
  • 1,082
0 votes
1 answer
70 views

one line definition of a variable conditional on the output of a logical test

In bash programming I've always defined conditional variables in the following long hand way dog=1 if [[ $dog -eq 1 ]] ; then cow=1 else cow=0 fi This is obviously incredibly inefficient. In ...
user avatar
  • 423
0 votes
3 answers
813 views

How to test if one or more files with a prefix exists in bash? e.g. [[-f foo*]]

Is there a way to use a Filename Expansion within a test expression, more specifically, a bash conditional expression? For example: [[ -f foo* ]] && echo 'found it!' || echo 'nope!'; ... will ...
user avatar
  • 111
1 vote
1 answer
99 views

What's the difference between test command and individual conditional expression surrounded by [[]]? [duplicate]

I am learning the zsh in the conditional expressions part, and I found some unreasonable output. For example: $ mkdir emptyspace && cd emptyspace $ var_assigned_with_emptystring="" $...
user avatar
  • 355
0 votes
1 answer
669 views

Why does using double brackets "[[" to check if wildcard matched files exists fail, while single brackets "[" work?

Let's say I generate a two directories with text files in each like so mkdir "Directory1" mkdir "Directory2" touch "Directory1/fileclass1_"{1..5}".txt" touch &...
user avatar
0 votes
2 answers
91 views

Using command test with $_1/

I'm trying to understand the command below, and particularly the parts that are in bold. I understand what the second line is. It's testing if a file exists and if it doesn't, it creates one, but what ...
user avatar
0 votes
4 answers
863 views

How do I evaluate a variable with a string in the POSIX shell?

I have a script that I am running inside a ubuntu container: #!/bin/sh name=$(cat < /etc/os-release | grep "^NAME" | cut -d "=" -f 2) if [ $name = "Ubuntu" ] then ...
user avatar
  • 17
28 votes
1 answer
3k views

These two Bash lines are functionally equivalent. Can someone explain why one is faster?

$ time if [[ $dir2 -ot $dir1 ]] ; then open $dir2 ; else open $dir1 ; fi real 0m0.055s user 0m0.023s sys 0m0.018s $ time [[ $dir2 -ot $dir1 ]] && open $dir2 || open $dir1 real ...
user avatar
  • 297
2 votes
1 answer
197 views

Testing if a string is a number

That should be easy, just use [[ "$var" =~ '^[1-9][0-9]*$' ]]. But I don't get the behavior I expect excepted with zsh. I don't control the machine where the script will be run, so portability along ...
user avatar
  • 2,198
3 votes
1 answer
2k views

Bash while condition

I have two conditions wait some time and check if within directory is more than one file. When one of these conditions is true loop has to be done so it is normal OR logical statement. The expected ...
user avatar
  • 31
0 votes
3 answers
67 views

Less than operator in script from piped command

I'm using Ubuntu 18.04 and working with ansible. I've got a test to see if somethings changed. Essentially, if the number of lines after grepping are "less than 2". I can retrieve the number using ...
user avatar
3 votes
1 answer
2k views

Check if symlink is broken or doesn't exist

Trying to make an if statement that checks if a file is a valid symlink (meaning it also exists). I tried: [ -h "$1" -a ! -e "$1" ] ... buut it doesn't work. I want to execute code if the file isn'...
user avatar
  • 1,035
1 vote
0 answers
42 views

How to curl multiple URL's from a text file containing URL's [duplicate]

I have a script on a linux server which connects to a URL refines the data and returns a text file with multiple links, please see below: Script: #!/bin/bash /usr/bin/curl -k -s URL | cut --...
user avatar
  • 53
1 vote
0 answers
26 views

string comparison with [ ] fails [duplicate]

#!/bin/bash echo "Enter any string" read string string1 = $(echo $string | rev) if ["$string" == "$string1"] then echo "String is palindrome" else echo "String is not Palindrome" fi I am ...
user avatar
1 vote
1 answer
1k views

Missing space after bash test bultin - legal syntax or undefined?

I made a small mistake today when writing a bash script. The following code #!/bin/bash if [ -n "${UNDEFINED_VAR}"]; then echo 'string not empty' echo "$UNDEFINED_VAR" else echo 'string ...
user avatar
  • 220
3 votes
1 answer
1k views

How to check if all arguments are files?

How do you check if all arguments passed to a shell script are files? I tried to use the $@ parameter and -f with an if statement.
user avatar
  • 95
5 votes
1 answer
7k views

conditional binary operator expected

var="$(command1 -l '$var2' -c 'command2|grep -c "search"')" if [[ var !=0 ]]; then fi Why am I getting "conditional binary operator expected". I searched already and. I. see that [[]] is a test ...
user avatar
0 votes
2 answers
49 views

Test size of newest file

I want to check if the size of the newest file is greater than 2 MB: test $(ls -st | head -n2 | tail -n1 | awk '{print $1}') -gt 2097152 && echo "true" Is there a more efficient or elegant ...
user avatar
  • 338
-3 votes
1 answer
941 views

Check for non-empty string in the shell (instead of ! -z)

I know there is a way to do this but internet search results always suck with this search - I want to do: if [[ ! -z "$var" ]]; then # .... fi but I can never remember, I think it's like this: ...
user avatar
0 votes
2 answers
321 views

sh (not bash) does not recognize single [ or double [[ brackets?

I have this script: #!/usr/bin/env sh # note: we must use sh instead of bash, it's more cross-platform set -e; if [[ "$skip_postinstall" == "yes" ]]; then # TODO rename 'skip_postinstall' to ...
user avatar
0 votes
3 answers
110 views

Why test -n returns true even when a variable is empty? What am I doing wrong? [duplicate]

according to the test(1) manpages: -n STRING the length of STRING is nonzero so I expected this to run fine: [ -n ${var} ] && echo "var is not empty" I used this ...
user avatar
  • 117
6 votes
1 answer
800 views

What's the difference between `-h` and `-L` in bash conditional expressions?

According to the man page: -h file True if file exists and is a symbolic link. ... -L file True if file exists and is a symbolic link. They both seem to ...
user avatar
  • 1,099
0 votes
1 answer
27 views

bash script for checking to see .AWK extension

if [ ! $var = " *.awk " ] ; then echo "$var contains awk file" fi this is what I have but when I run it and put a file name without .awk it still says contains awk file
user avatar
10 votes
2 answers
13k views

Pattern matching in a zsh conditional expression

I am having trouble with pattern matching in zsh's hook function precmd. I have the following: precmd(){ local x='test' if [ $x = '*test*' ]; then echo 'hello' fi } which does not ...
user avatar
2 votes
1 answer
189 views

How to confirm if pathname exists?

How do I test whether a pathname exists? Previously I was using: realpath -e /some/path Which works except if /some/path is a broken symlink, in which case it reports that it does not exist. [ -e /...
user avatar

1
2 3 4 5
7