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.
302
questions
7
votes
5answers
623 views
“test -n && echo not empty” prints “not empty”. Is that expected behaviour?
When I execute the following line
test -n && echo not empty
it prints "not empty". This occurs when you have an empty variable, and test for non-emptyness while forgetting to quote the value:...
7
votes
1answer
972 views
What's the point of using “x$START_DAEMON” = “xyes” here? (the leading x)
This was part of a fetchmail daemon script,
if [ ! "x$START_DAEMON" = "xyes" -a ! "$1" = "status" ]; then
What's the point of using x$START_DAEMON = "xyes"; why not just use $START_DAEMON = "yes" ?
6
votes
3answers
10k views
What does the operator `-gt` in shell scripts mean? [duplicate]
Hi I have this sentence and I want to know what does it means please.
if [[ -z "$1" ]]; then # --> this is if the value of the parameter $1 is zero
PASO=1
elif [[ "$1" -gt 1 ]] ; then # ...
6
votes
2answers
33k 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 ...
6
votes
3answers
7k views
what does the -z flag mean here
What does the -z flag mean in the code below:
if [ -z $TEST_PARAM ]; then
And is there a list of such flags?
For a flag like ls -l, I know where to find it, but for a single flag, I didn't get any ...
6
votes
2answers
23k 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 ...
5
votes
3answers
5k views
Bash File existence test is always true
I have the following lines of bash script to test whether a file exists:
MYAPPPATH=$(find $APPDIR -iname myapp* -print)
if [ -e $MYAPPPATH ]
then
echo "File exists"
fi
However when file ...
5
votes
3answers
2k views
Which regular expression methods to validate input could be used in shell scripting?
#!/bin/sh
re="\/$"
if [ $1 =~ $re ]; then
echo "${ATTENTION_PREFIX}$1 DIRECTORY MAY NOT CONTAIN A \"/\" OR LITERAL SLASH!${ATTENTION_POSTFIX}"
exit 1
fi
Executing sudo file.sh hello/...
5
votes
3answers
361 views
Why doesn't the conditional work when I use it directly without a variable?
This gives me an error that says too many arguments:
if [ $( file -b $i ) == "directory" ]
But when I tried this
name=$( file -b $i )
if [ name == "directory" ]
It seems to work just fine.
Can ...
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 ...
5
votes
3answers
1k views
Calling function in Shell script
I have written one simple function in shell that returns 0 or 1 based on some condition.Let me call that function name foo
foo(){
...
...
}
Now i am trying to call foo in if condition as follow:-
...
5
votes
1answer
391 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 ...
5
votes
1answer
517 views
Test -n gives unexpected result
I understand test -n <expression> to return false if the expression evaluates to a string length of greater than 0. Why then does the following happen?
Macbook:~ echo ${#undeclared_variable}
0
...
5
votes
3answers
7k views
shell test whether multiple lines string contains specified pattern in last line
I want to determine whether a multi-line string ends with a line containing specified pattern.
These code failed, it doesn't match.
s=`echo hello && echo world && echo OK`
[[ "$s" =~ ...
5
votes
2answers
969 views
[ vs [[ : which one to use in bash scripts? [duplicate]
The zsh man page, in its section on test (aka [), explicitly advises against using it at all, and urges readers to use [[ whenever possible.
The relevant section states:
The command attempts to ...
5
votes
2answers
6k views
bourne shell if [ -e $directory/file.$suffix ]
#!/bin/sh
CONFIG_DIR="/var/opt/SUNWldm/"
read option
if [ $option -eq 9 ]; then
ret=1
elif [ -e ${CONFIG_DIR}file.xml.${option} ]; then
echo "TRUE"
fi
I have the above code in a while ...
5
votes
3answers
568 views
Peculiar Solaris integer comparison behaviour
This one caught me unawares!
We have a Solaris machine (version: SunOS 5.8) hosting a pivotal script/process which has been running for many years without complaint. We recently had cause to have a ...
5
votes
2answers
84 views
How do I test for link to a link?
I want to test whether a file is a link to another link. I tried readlink but it doesn't work the way I need it:
ralph@bash4.4.12,1:~/subdir1 $ ll
lrwxrwxrwx 1 pi pi 13 Apr 10 14:34 hellolink ->...
5
votes
1answer
75 views
Testing whether a file is local
I need to test in a script, whether a file is from a network mount or truly local. To be more clear: I need to test, whether parsing the contents will be fast or slow, but for my case local vs network ...
4
votes
4answers
2k views
Error with if [ -n $diffCurr ]: Too many arguments
I have been using the below code ok for some time, but now I get an error
java Editor < "input/editor$i.in" > "tmp/editor$i.out"
diffCurr="$(diff "tmp/editor$i.out" "output/editor$i.out")"
if [ ...
4
votes
5answers
409 views
Script tries to create files even though it shouldn't have to?
I have a folder which contains some folders, these folder are moved very often so I made a script to see if they exist, and if not then create them. This is what I did to (which I though would) ...
4
votes
1answer
1k views
if condition always evaluating true [duplicate]
if [[ ${fin[2]} -eq OK && ${fin[7]} -eq NA ]]
then
echo "<tr id="green">" >> /tmp/mailt.txt
elif [[ ${fin[2]} -eq OK && ${fin[7]} -lt 0 ]]
...
4
votes
3answers
1k views
Clarification on differences and usage of test -f vs test -e in a shell script
In a code review
I am being pointed out that I should change such a test structure:
[ ! -f "$1" ] &&
print_error_and_exit "The given argument is not an existing file."
to something similar, ...
4
votes
2answers
4k views
Why would I get integer expression expected?
So, I'm trying to create a menu system in bash as a learning experience. I'm sure there are countless ways, and even "better" ways to do this, but what I've got is something like this...
echo "
2nd ...
4
votes
2answers
154 views
What's the purpose of adding a prefix on both sides of a shell variable comparison to a string literal?
I have encountered comparisions of variables to string literals multiple times over the years which had one character prefixing the variable and the literal, e.g.
if [ "x$A" = "xtrue" ]; then
in ...
4
votes
2answers
6k views
Testing a string containing only spaces (tabs, or “ ”)? [duplicate]
My code below doesn't work:
stringZ=" "
if [[ "$stringZ" == ^[[:blank:]][[:blank:]]*$ ]];then
echo string is blank
else
echo string is not blank
fi
Result:
string is not blank # wrong
...
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 ...
4
votes
2answers
6k views
bash - True if the length of string is (non)zero?
# touch $$
# gzip $$
# gzip --test $$.gz
# echo $?
0
# OUT=$(gzip --test $$.gz)
# echo $OUT
# if [ -z $OUT ] ; then echo $$ ; fi
26521
# if [ -n $OUT ] ; then echo $$ ; fi
26521
#
from bash(1)
-...
4
votes
2answers
298 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 ...
4
votes
2answers
1k views
Is there a problem if I add a non-existing path to `PATH` environment variable?
I have a following lines in my .bash_profile.
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi
I think I found it somewhere in the internet.
Is it also ok, if I write only PATH="$HOME/bin:...
4
votes
3answers
548 views
Mixing if ..; with { command-grouping} and [[ test ]] and logical operators [closed]
Edit: I've tried again and now it works fine in bash. I don't know what changed. My editor (VS Code with Bash IDE extension) still gives me a failed to parse error, but the script runs properly. ...
4
votes
1answer
10k views
How to use user input as a while loop condition
I can do this in bash:
while read -n1 -r -p "choose [y]es|[n]o"
do
if [[ $REPLY == q ]];
then
break;
else
#whatever
fi
done
which works but seems a bit redundant, can ...
4
votes
1answer
2k views
How to use booleans in fish shell?
I switched to fish shell and quite happy with it. I didn't get how can I handle booleans. I managed to write config.fish that executes tmux on ssh (see: How can I start tmux automatically in fish ...
4
votes
1answer
70 views
Bash: Is '[[' also a program like '['?
The test condition [ is a program, which is residing on debian based distributions in /usr/bin/[. I would like to know if the other condition tester [[ is also a program?
The reason why I am asking: ...
4
votes
2answers
160 views
Shell: why [ -n “${VAR-}” ] instead of [ -n “$VAR” ]?
I see a lot of shell scripts (for example, this one) checking for a variable's presence/absence like:
[ -n "${VAR-}" ]
As far as I can tell, using the ${VAR-fallback} form without providing a ...
4
votes
2answers
2k views
bash - What do the brackets in if-statements do? [duplicate]
Perhaps this applies to more than just bash, but I am confused about the role of brackets in if-statements. Most examples seem to have the following format
if [ expression ]; then
#do stuff
fi
...
4
votes
2answers
947 views
How to test list of proxy servers?
I have list of HTTP/HTTPS/Socks proxy servers like this:
1.2.3.4:80
5.6.7.8:8080
...
I need to test each server if it works and if it is anonymous or not. I plan to put these servers to my ...
4
votes
1answer
2k views
Bash test on successful subshell command returns false
I don't quite understand this behaviour of bash test operator. I used this line to determine if vagrant is installed.
if [ $(vagrant --version > /dev/null) ]; then echo "HELLO"; fi
I now know ...
3
votes
3answers
428 views
Union commands after the || (OR) operator
When I write the code the way below, I am able to run several commands after the else statement:
if [ "$?" -eq 0 ]
then
echo "OK"
else
echo "NOK"
exit 1
...
3
votes
4answers
378 views
Multiple statement which includes special characters using if causing errors
I have been trying to write a script that will checks if the specific content is not found in specific files/folders then it should print "failed". else it should print "success".
However, the ...
3
votes
2answers
2k views
What does [[ -t 1 ]] do?
To my understanding, the script below runs if the user is not root by comparing $EUID and 0. Then, it uses [[ -t 1 ]] to decide if the script is running in a terminal or not. If it is, it will use ...
3
votes
4answers
7k views
Bash or condition in a while statement
I want to write a script in bash which is checking the user input if it is equal to "stringA" or "stringB" and in case it is equal to one of these strings it should print the user input. My code is:
...
3
votes
3answers
7k views
How do I make an output for grep fail?
I am using if-else statement to search for keywords and displaying the results in the terminal, here's an example of my code.
read finding
if ["$finding" != "" ]; then
grep $finding information....
3
votes
1answer
98 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 ...
3
votes
1answer
8k views
arithmetic syntax error in string compare
I have written a shell script which takes file name as parameter e.g user/test.txt. I want to make this file parameter optional if user does not wish to provide file name he/she can give as "None"
...
3
votes
1answer
105 views
Specific string hangs [, [[
While fuzz testing I've found that it's possible, by defining
var=$'\365\\'
, to make both of the following commands (used to remove everything up to the last newline) hang indefinitely, consuming ...
3
votes
2answers
116 views
Bash difference or preferred negation statement
Is there a difference in bash, or a preferred usage of negation of a statement?
if ! [[ -z "${var}" ]]; then
do_something
fi
Versus
if [[ ! -z "${var}" ]]; then
do_something
fi
3
votes
3answers
6k views
Compare bash variable to see if divisible by 5
Here is my code; I want to compare $COUNTER to various multiple times.
if [ "$COUNTER" = "5" ]; then
It's okay, but I want it do it for dynamic times like 5,10,15,20 etc.
3
votes
1answer
218 views
What is the purpose of testing $DISPLAY with [ -z $DISPLAY ]?
In the very beginning of a script stands:
if [ -z $DISPLAY ]
then
DIALOG=dialog
else
DIALOG=Xdialog
fi
I understand, that
$DISPLAY holds the value of which pseudo-terminal one is attached to
[ -...
3
votes
2answers
402 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 ...