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
0 votes
0 answers
83 views

Shell script: check for file doesn't work if there is a symlinked directory involved?

If I have /mydir/myfile.txt this code echos file already exists if [ ! -f "/mydir/myfile.txt" ]; then touch "/mydir/myfile.txt" echo "created file" else echo ...
d-b's user avatar
  • 1,713
0 votes
1 answer
60 views

"while" loop is not reading reading a variable's contents

I have setup a little script to illustrate my problem. I have set up a while loop that I would like to keep looping as long as $output is equal to No screen session found #!/bin/bash echo Stopping ...
Kuba0040's user avatar
0 votes
2 answers
159 views

Syntax error: "(" unexpected (expecting "fi") when using calculations

I am relatively new to shell, and I am getting a syntax error where I am still confused. #!/bin/dash ls="ls -l test" small=0 medium=0 large=0 for i in $(seq 11 9 56) do filename=$(echo ...
user avatar
0 votes
1 answer
28 views

Why does the test command apparently choke on a for loop variable?

I want to determine all broken symlinks within a directory. In ignorance of a better solution, I tried this simple bash one-liner (bash version is 4.2.46, which may be worth mentioning because it's a ...
andreee's user avatar
  • 193
0 votes
1 answer
74 views

test -r contradicts ls -l

I'm struggling with files and directory permissions. ls -l is telling me something that test -w contradicts. $ ls -l total 1792 -rw-r--r-- 1 root www-data 168 Jan 29 23:53 CODE_OF_CONDUCT.md -rw-...
rhommet's user avatar
0 votes
1 answer
53 views

Calling the status pages in linux

I am currently working in a project which is Linux based. From Linux terminal I have connected to our web application. The web application consists of pages and data in it so from Linux terminal I use ...
Jeevan Jitu's user avatar
0 votes
1 answer
62 views

Shell: why [ x"$myvar" = x0 ] instead of [ "$myvar" = 0 ] / why add a letter character to tested variable? [duplicate]

There may be a duplicate somewhere (I guess), but I didn't find it so: Why do I see in some scripts those kind of tests: if [ x"$quiet_boot" = x0 ] with an x added before the actual ...
Totor's user avatar
  • 19.4k
1 vote
1 answer
180 views

test returns wrong value (but no errors) depending on the presence of spaces around the operator

If there are spaces around the operator, test returns the correct value of the expression. But if there are no spaces, it doesn't throw any syntax error and always return true. $ test "A" == ...
robertspierre's user avatar
0 votes
1 answer
96 views

Bash - What h[aeiou] does in the [[ =~ ]] expression?

I am new at unix, I'm trying to learn the bash language, and when I went to the "Testing expression", I found this one: [[ "whatever" =~ h[aeiou] ]] I already read the answer to ...
andr.lex's user avatar
1 vote
3 answers
671 views

What is a good way to test watchdog (script or command to deliberately overload system)

I have a hardware watchdog, what is a good way to test that it actually works? Is there a standard script or such to set my system in an infinite loop to hog all resources or such to the point of ...
Matt Welander's user avatar
0 votes
1 answer
1k views

Multiple conditions in if statement

I am doing a basic question for adding, product, subsraction, division by taking user input in first and second I don't understand where am I going wrong because it's not passing any test case. ...
Prince's user avatar
  • 3
12 votes
6 answers
4k views

How to XOR two expressions of `test`?

I need to check that a directory (let's call it dir) contains one of two files (let's call them filea and fileb), but neither none nor both. The ideal solution would be to use a XOR operation between ...
joH1's user avatar
  • 898
2 votes
2 answers
115 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) ]...
kjo's user avatar
  • 14.8k
4 votes
2 answers
1k 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 ...
Dawson Smith's user avatar
0 votes
1 answer
220 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 ...
jian's user avatar
  • 539
8 votes
2 answers
710 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 ...
db-inf's user avatar
  • 313
16 votes
2 answers
3k 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 ...
Martian2020's user avatar
  • 1,083
0 votes
1 answer
2k 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 &...
Dean's user avatar
  • 384
2 votes
1 answer
353 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 } ...
fuumind's user avatar
  • 409
8 votes
1 answer
1k 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 ...
gendergap's user avatar
  • 151
0 votes
1 answer
1k 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 ...
zak's user avatar
  • 11
4 votes
3 answers
19k 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 ...
alper's user avatar
  • 449
0 votes
3 answers
371 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 [ $(...
von spotz's user avatar
  • 405
0 votes
1 answer
772 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
226 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 "$...
Jacky's user avatar
  • 161
-3 votes
3 answers
187 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
Zeninツ's user avatar
4 votes
1 answer
781 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 ...
MikeG's user avatar
  • 143
1 vote
1 answer
977 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 ./...
symcbean's user avatar
  • 5,110
2 votes
2 answers
3k views

What does if [ -r "$1" ] ; mean?

I am relatively new to shell (just now the basics of it). I am trying to make sense of a script in order to fix a bug and I stumble upon something that I can't find an answer through Google(probably ...
joeCarpenter's user avatar
1 vote
2 answers
1k 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 ]] ...
tigger's user avatar
  • 13
0 votes
0 answers
526 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 ...
MrPython's user avatar
0 votes
0 answers
239 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 ...
Daffy's user avatar
  • 375
-1 votes
1 answer
541 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
466 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 ...
Nordine Lotfi's user avatar
4 votes
3 answers
1k 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 ...
aderchox's user avatar
  • 631
8 votes
1 answer
2k 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?
testter's user avatar
  • 1,310
0 votes
1 answer
232 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 ...
Leo Simon's user avatar
  • 443
0 votes
3 answers
2k 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 ...
Ricardo's user avatar
  • 111
1 vote
1 answer
884 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="" $...
Anon's user avatar
  • 385
1 vote
1 answer
2k 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 &...
user321627's user avatar
0 votes
2 answers
98 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 ...
Daniele's user avatar
0 votes
4 answers
1k 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 ...
chris's 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 ...
cp10111's user avatar
  • 297
2 votes
1 answer
658 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 ...
AProgrammer's user avatar
  • 2,278
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 ...
adamo89's user avatar
  • 31
1 vote
3 answers
182 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 ...
Black Dynamite's user avatar
3 votes
1 answer
5k 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'...
Freedo's user avatar
  • 1,215
1 vote
0 answers
46 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 --...
Nikhil's 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 ...
Adil Ali's user avatar
1 vote
1 answer
2k 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 ...
Max1's user avatar
  • 210

1
2 3 4 5
8