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.
370 questions
0
votes
0
answers
22
views
Testing postfixmailserver with POP3 fails
PostfixAdmin installed successfully. To test whether internal sending and receiving mail work, I used SMTP to send mail:
telnet mail.example.com 25
Trying 94.284.174.110...
Connected to mail.example....
5
votes
2
answers
267
views
Why are there so many shell scripts using "x$var" = "xabc" instead of "$var" = "abc"? [duplicate]
In the old times of DOS batch scripts, the only way to check for a variable to be empty or to allow an empty variable was to prepend (or append) a known letter, commonly the letter x, because there ...
2
votes
1
answer
170
views
Semicolon in conditional structures after the closing double bracket in a bash/zsh script?
Continuing Semicolon in conditional structures (which handles single brackets), what's the point of having a semicolon after the closing DOUBLE bracket ]]?
In my tests, running
#!/bin/zsh --
if [[ &...
3
votes
1
answer
76
views
Why can't I have a single quote in a bash associative array index when testing with `test -v` [duplicate]
When I run the following code
declare -A X # get an associative array
index="a'b" # a somewhat weird index
X[$index]="this is set"
echo "<<${X[$index]}>>"
if ...
0
votes
2
answers
221
views
How to stop a running memtester (without risking to have to reboot)?
Extremely "noob" question: I have a running sudo memtester ... on an Ubuntu 22.04 machine, but I gave it too much memory to test and it's taking too much time (I see it running and updating, ...
0
votes
2
answers
237
views
How to check value inside a file using bash?
I'm trying to check if the value inside a file is "0".
COUNT_EXECUTION=$(< /tmp/count)
if [ "$COUNT_EXECUTION" == "0" ]; then
echo "Try restart service."
...
1
vote
1
answer
92
views
I fail to match the standard output of a posix shell function with the text I want to test it against
I want to test if the output of a function matches the text I envision it should write to standard output. But the code below never prints test PASS and I can't figure out why. I also tried putting ...
1
vote
2
answers
730
views
Is my "escaping" wrong - or is it something else?
Here's what I'm trying to do (in a script):
#!/usr/bin/env bash
if [[ ! $("/usr/bin/scp [email protected]:/Users/seamus/Downloads/imgutils/image-utils* /home/pi/testscp") ]]; then
...
0
votes
0
answers
223
views
Linux From Scratch 12.1 GCC testsuite failing and timing out
I'm currently building Linux From Scratch 12.1 with the book, I've followed the instructions line by line. I'm struggling with the GCC testsuite, I have lots of FAIL and timeout. In addition to that, ...
2
votes
2
answers
89
views
Bash complaints about missing ], while it is there [duplicate]
I have following in a bash script:
echo "status: [$status] and message: [$message]"
if [ "${status}" -eq 0 && "$message" = "ERROR" ];
then
echo &...
1
vote
1
answer
153
views
Why does -n with unquoted variable containing empty string return true? [duplicate]
From man bash:
-n string
True if the length of string is non‐zero.
Examples:
# expected
$ var=""; [ -n "$var" ]; echo $?
1
# unexpected?
$ var=""; [ -n $var ]; echo ...
0
votes
0
answers
103
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 ...
0
votes
1
answer
223
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 ...
0
votes
2
answers
917
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 ...
0
votes
1
answer
44
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 ...
0
votes
1
answer
93
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-...
0
votes
1
answer
67
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 ...
0
votes
1
answer
131
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 ...
1
vote
1
answer
405
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" == ...
0
votes
1
answer
226
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 ...
2
votes
3
answers
6k
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 ...
0
votes
1
answer
3k
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.
...
13
votes
6
answers
7k
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 ...
2
votes
2
answers
154
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) ]...
4
votes
2
answers
2k
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 ...
0
votes
1
answer
462
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 ...
8
votes
3
answers
1k
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 ...
17
votes
2
answers
4k
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 ...
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 &...
2
votes
1
answer
421
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
}
...
8
votes
1
answer
2k
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 ...
0
votes
1
answer
2k
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 ...
6
votes
3
answers
29k
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 ...
0
votes
3
answers
634
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
[ $(...
0
votes
1
answer
1k
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 ...
2
votes
1
answer
312
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 "$...
-3
votes
3
answers
217
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
4
votes
1
answer
1k
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 ...
1
vote
1
answer
1k
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
./...
3
votes
2
answers
7k
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 ...
1
vote
2
answers
3k
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 ]] ...
0
votes
0
answers
796
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 ...
0
votes
0
answers
359
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 ...
-1
votes
1
answer
838
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 [ $...
0
votes
3
answers
690
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 ...
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 ...
12
votes
1
answer
6k
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?
0
votes
1
answer
791
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 ...
0
votes
3
answers
4k
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 ...
3
votes
1
answer
2k
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=""
$...