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.
331
questions
1
vote
2answers
41 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
0answers
68 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
0answers
25 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
1answer
28 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
3answers
103 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
3answers
441 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 ...
0
votes
1answer
54 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
2answers
40 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
3answers
66 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 ...
1
vote
1answer
39 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=""
$...
-1
votes
1answer
112 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 &...
0
votes
2answers
86 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 ...
0
votes
4answers
364 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
...
28
votes
1answer
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 ...
2
votes
1answer
79 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 ...
3
votes
1answer
905 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 ...
0
votes
3answers
44 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 ...
3
votes
1answer
290 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'...
1
vote
0answers
35 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 --...
1
vote
0answers
25 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 ...
1
vote
1answer
352 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 ...
3
votes
1answer
370 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.
1
vote
1answer
2k 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 ...
0
votes
2answers
32 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 ...
-5
votes
1answer
162 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:
...
0
votes
2answers
84 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 ...
0
votes
3answers
53 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 ...
0
votes
0answers
49 views
How should I setup an LXD linux-container?
The following documentation contains the necessary infomation to setup LXC.
https://linuxcontainers.org/lxd/introduction/
https://linuxcontainers.org/lxc/getting-started/
After that, I wanna use LXD ...
0
votes
0answers
70 views
Find hidden directory under user profile and echo result
#!/bin/sh
USERLIST=$(ls /Users | grep -v Guest \
| grep -v .localized \
| grep -v Shared \
| grep -v administrator \
...
5
votes
1answer
178 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 ...
0
votes
1answer
19 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
6
votes
2answers
5k 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 ...
1
vote
1answer
74 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 /...
1
vote
1answer
353 views
Bash * directory wildcard not working in if statement using double brackets
This works:
if [ /a/*/b/file.asd -nt /c/d/file.asd ]; then echo "found new file"; fi
This doesn't work:
if [[ /a/*/b/file.asd -nt /c/d/file.asd ]]; then echo "found new file"; fi
Can someone ...
-1
votes
1answer
31 views
test for existing directory passes for directory that does not exist
Action
I have set an environment variable in a user profile
export DATADIR=<absolute path set with `readlink -e target`>
Expected outcomes
After sourcing the profile file again, I checked that ...
0
votes
1answer
156 views
Why does -n appear in my bash test [[ condition debug output?
I would like to test if a condition is true or false. Here is a sample bash script:
#!/bin/bash
set -x
var=foo
if [[ $var==bar ]]
then
echo $var is bar
else
echo $var is not bar
fi
...
0
votes
2answers
461 views
Bash: Globbing and file conditions [duplicate]
I am trying to check wether a file exists or not:
This works as expected:
if [ -e "/tmp/test.*.lock" ]; then
echo "exists!"
fi
This does not:
if [[ -e /tmp/test.*.lock ]]; then
echo "exists!"
...
2
votes
0answers
40 views
Testing whether an executable can be run on Linux? [duplicate]
In my Ubuntu packages, I install a hook with a one liner script:
if test -x /usr/bin/snapsignal
then
/usr/bin/snapsignal "snapmanagerdaemon/DPKGUPDATE hook_action=\"$DPKG_HOOK_ACTION\""
fi
(...
1
vote
0answers
168 views
What is the difference between test, [, and [[ in terms of binary vs. builtin? [duplicate]
It is often written:
/usr/bin/test is a synonym for /usr/bin/[
[[ is a bash improvement for [
test and [ are also builtins
I understand the difference between [ and [[ but I am confused with the ...
1
vote
1answer
83 views
What does the -a unary operator check?
I have just found a couple of examples using [ -a some_dir ] or [ -a some_file ] but can't find what the -a operator is for. It seems it should be described in the man page for test/[ but there it is ...
2
votes
1answer
139 views
How to determine if result of process substitution is a file path
If I do this:
echo <(cat)
I get:
/dev/fd/63
so say at the command line I have:
myapp -f <(cat)
when I run it I get this error:
You need to pass a file after the -f flag. The resolved ...
0
votes
1answer
136 views
Bash script to compare ASCII values [closed]
what is the wrong?
#!/usr/local/bin/bash
REF_FILE_NUM="000000010000001900000052"
for id in `ls -1 dir` ;
do
FILE_NUM=${id:0:24}
echo "FILE_NUM: $FILE_NUM ; REF_FILE_NUM: $REF_FILE_NUM"
if [[ "$...
2
votes
3answers
83 views
Shortcutting in a loop
Shortcutting: (expr1 && expr2), rather than an if ... fi statement. If expr1, expr2,... are to be generated by a loop, can you do it?
Here's a toy example:
$ for cond in {0,0,0}; do if [[ $...
0
votes
2answers
487 views
Testing multi-valued exit status (not just success or failure)
I have two files, say file1="/scratch/abc/osx.xrts" and file2="/scratch/unzip/abc/osx.xrts".
I am executing the diff command, ignoring the line endings. Command and code is as below:
diff --strip-...
0
votes
3answers
1k views
I would like to check if a file name matches a certain pattern
I would like to check if a file name matches a certain pattern start with an alphanumeric character ([a-zA-Z0-9]) and will only contain alpha-numeric characters plus '.', '-' and '_' characters.
...
3
votes
1answer
230 views
Comparing exit code: unary operator expected [duplicate]
I have a list of git commits in a .txt file and I want to iterate over the commits. For each COMMIT_ID I want check them using a git command whose exit code is 0 or 1.
Depending on the result I want ...
0
votes
2answers
93 views
Print all subdirectories in the current directory that do not contain a .git
I'm trying to write a bash line that will look at all the subdirectories in my current folder and tell me if any do NOT contain a ".git". Pseudo:
for subdir in currentdir
if .git does not exist
...
0
votes
3answers
71 views
I have if else question but I am not sure the answer is true? [duplicate]
I am running this on Linux:
$ number=4
$ test $number -eq 4
$ echo $?
0
$ test $number -lt 2
$ echo $?
1
Are the results (0 and 1) true ?
0
votes
1answer
164 views
Why does “if tty -s” work but “if [ tty -s ]” does not?
Note, I am using ksh and running in CentOS.
I have a shell script where a logger function is set like:
if tty -s
then
echo "${FOO}" | tee -a ${BAR_LOG_FILE}
else
echo "${FOO}" >> ${...
-1
votes
2answers
2k views
How to test file permissions using shell script?
I am testing if my file has read permissions, by running this script as root:
#!/usr/bin/env bash
FILE="/etc/shadow"
if ! [ -r $FILE ]; then
echo "Hello"
fi
exit 0
Ideally, script has to print ...