We’re rewarding the question askers & reputations are being recalculated! Read more.

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
1
vote
3answers
292 views

Alternative to Bash double bracket glob match

Given hasArgs=a:b:c: x=b if [[ ${hasArgs} = *$x:* ]] ; then … I wish I could replace the above double brackets test with a less Bash specific alternative (meaning no subshell/command etc.) ...
0
votes
0answers
19 views

Command substitution, assignment, tests and spaces [duplicate]

I know the whole language is confusing, but I am having trouble understanding assignments with command substitutions: x=Example sentence #1 fails due to space x='Example sentence' ...
4
votes
3answers
959 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, ...
1
vote
2answers
239 views

Similar test expressions using or operator?

Just wondering if this: if [ "$first_arg" == "major" ] || [ "$first_arg" == "minor" ]; then exit 1; fi is the same as this: if [ "$first_arg" == "major" || "$first_arg" == "minor" ]; then ...
11
votes
3answers
873 views

Performing -nt/-ot test in a POSIX sh

The built-in test and [ utilities have the -nt ("newer than") and -ot ("older than") tests in most shells, even when the shell is running in "POSIX mode" (also true for the external utilities of the ...
0
votes
1answer
138 views

Shorten if statement check for matching argument

I have this bit of code which does what it's supposed to do: first_arg="$1"; if [ "$first_arg" != "patch" -a "$first_arg" != "major" -a "$first_arg" != "minor" -a "$first_arg" != "prerelease" ]; then ...
1
vote
1answer
482 views

Capturing return code with test []

If I run this: #!/usr/bin/env bash simple_return_zero(){ return 0; } simple_return_one(){ return 1; } if [ simple_return_zero ]; then echo "we have 0000"; fi if [ ! simple_return_zero ]; ...
1
vote
2answers
520 views

bash: [: missing `]' when the test stored in variable [duplicate]

Why this works: [ -r /tmp ] && echo "tt" >/tmp/taa cat taa tt But the following doesn't and how to fix that, preserving variable for reusability? COMD='[ -r /tmp ] && echo "tt"' $...
1
vote
0answers
2k views

Why use a variable in a bash regex match inside [[? [duplicate]

There is not a simple answer to this question at this site yet. This question aims to give a simple and clear answer. It is usually recommended to use a variable ($regex) in this construct: if [[ $...
6
votes
3answers
9k 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 # ...
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) -...
1
vote
1answer
2k views

The meaning of [ $? == 1 ] && return 0

I need to understand this command: [ $? == 1 ] && return 0
0
votes
2answers
2k views

RegExp - Optional Capture group in Bash?

Currently working on some RegExp to parse an input file for correct content. I'm using the below RegExp to parse some input: cell-(90|855|80|70)-(DEV|DEVL|SANDP|CAT|(SIT[a-z]|SIT[1-9])|TAT|PROD)(?:-(...
2
votes
1answer
335 views

Dash “-nt” behavior

The use case I can think of for -nt, would be updating a file if it is too old, or if it does not exist. Bash handles this just fine: $ bash -c 'touch 2.txt; sleep 1; touch 1.txt; [ 1.txt -nt 2.txt ];...
4
votes
1answer
69 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: ...
1
vote
1answer
62 views

Get a boolean from test expression

I have this bash code: local r2g_keep_temp=$(r2g_match_arg "--keep" "${my_args[@]}"); local r2g_multi_temp=$(r2g_match_arg "--multi" "${my_args[@]}"); local r2g_multi=[ "$r2g_multi_temp" || ...
5
votes
3answers
989 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:- ...
10
votes
3answers
5k views

Why is a file with 400 permissions seen writable by root but read-only by user?

If I create a file as an unprivileged user, and change the permissions mode to 400, it's seen by that user as read-only, correctly: $ touch somefile $ chmod 400 somefile $ [ -w somefile ] && ...
1
vote
1answer
130 views

Error when compare big numbers

Trying to use this #!/bin/bash SIZE=$(redis-cli info | grep used_memory: | awk -F':' '{print $2}') MAX=19000000000 if [ "$SIZE" -gt "$MAX" ]; then echo 123 fi But always getting: "Ganzzahliger ...
1
vote
1answer
152 views

What does the capital -N option in an if statement mean?

man test only explains what -n means, wit a lowercase n. How does the capital -N work in this script? #!/bin/bash # Check for an altered certificate (means there was a renew) if [[ -N '/etc/...
0
votes
4answers
268 views

Count number of files that are not symbolic links in output from “locate”

I'm trying to count the number of non-symbolic linked files passed by the locate command. I've tried a number of options and think this is the most promising: locate -r "$PWD.*\.c$" | xargs -0 -I{} ...
0
votes
1answer
46 views

Empty test handling

Am learning bash and it's confusing how (why) this line of code if [$1 = ]; then works. Can someone please explain. From bash tuturial here under File re-namer (6th block) # a quick check to see if ...
1
vote
1answer
2k views

-eq: unary operator expected, shell argument parsing

I am getting this error while comparing shell argument to a string. If the code look like this: online=true if [ "$2" -eq '-o' ] then online=false fi echo $online Then for e.g. input I am ...
0
votes
1answer
1k views

Ksh script no parent error

I want the script to open a file and read the file line by line then count the number of commas per row/line. If that is more than the $2 parameter value, then write the offending line number (from ...
2
votes
1answer
1k views

Compare command output inside if statement without subshell

According to https://www.gnu.org/software/bash/manual/bashref.html#Command-Grouping Placing a list of commands between curly braces causes the list to be executed in the current shell context. But ...
-2
votes
1answer
273 views

string comparison not working with sqlplus output

Code: iIMSI=`sqlplus -s ${APP_ORA_USER}/${APP_ORA_PASS}@${APP_DB_INST} << EOF SET HEAD OFF PAGESIZE 0 LINESIZE 1000 TRIMOUT ON TIMING OFF select resource_value from agd1_resources where ...
0
votes
0answers
113 views

-d foo-* within single and double square brackets [duplicate]

I just discovered that in bash,[[ -d foo-* ]] returns false ($? set to 1) even if a directory named foo-bar exists while [ -d foo-* ] returns true ($? set to 0) I have to admit that I was surprised ...
0
votes
1answer
218 views

How to make nested ifs [closed]

I'm trying to make my own command using a script but I'm having a bit of doubt about the proper way to create an if inside another one. The code below shows how I'm trying to do it, but I guess it's ...
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.
0
votes
2answers
937 views

Bash: test strings including dollar sign

Can someone explain the strange behavior of dollar sign $ in test command? [ "$a" != "$b" ] echo $? 1 [ "$a" != "b" ] echo $? 0 Why test "$a" != "$b" returns ONE instead of a ZERO?
1
vote
1answer
61 views

Is it possible to monitor each cell memory for a specific process

Saying that I have a very simple C program test.c, which just prints "hello world", its name is a.out coming from gcc test.c. I'm thinking if it's possible to monitor exactly what happened while ...
1
vote
2answers
83 views

variable printed the same value but actually value is diff

the follwing syntax used in order to capture the word between in xml file I also use xargs to remove any spaces var=` find /tmp -name '*.xml' -exec sed -n 's/<Name>\([^<]*\)<\/Name>/\...
22
votes
3answers
8k views

Does the syntax of not equal matter?

When scripting, I usually write my ifs with the following syntax as it is easier for me to understand that what comes next is not true. if [ ! "$1" = "$2" ]; then Others say that the way below is ...
0
votes
2answers
338 views

test (AIX & ksh93)

I'm confused on one of the ksh93 test qualifiers that we use. Twenty+ years ago we took a "Unix Shell Programming - featuring the KornShell" course. In the manual that accompanied that course it ...
3
votes
1answer
718 views

Test multiple file conditions by combining flags—does it work?

My answer over at StackOverflow, which received at least two upvotes, is what worked on the shell of a workstation I used at my previous workplace. On that workstation, which is probably some old ...
1
vote
1answer
3k views

Bash Integer Comparison [duplicate]

While reading through a bash style guide I found the following guideline: Math / Integer Manipulation Use ((...)) and $((...)). a=5 b=4 # wrong if [[ $a -gt $b ]]; then ... fi # right ...
1
vote
1answer
1k views

Bash epoch date comparison not working [duplicate]

Need some help in fixing simple bash script below. What it does is to compare dates in epoch format using if else. The script does not work as I intended because it always goes to the first condition ...
-3
votes
4answers
8k views

-eq: unary operator expected [closed]

x=$? if [ ${x} -eq '0' ]; then echo "something missing" exit 1 else echo "all present" fi Why I am getting -eq: unary operator expected when executing above? A more complete version of what I am ...
-2
votes
2answers
51 views

Need to delete logs from different locations using shell script

Need to delete logs with different name from different locations and need to delete the logs more than 5 days. Ex: /abc/bcd/fgh/log/log1.txt /abc/bcd/fgh/test/log2.log /test/urc/mhg/event.log /hjy/...
0
votes
1answer
38 views

Shortening a long if/test string comparison

I am comparing multiple strings in bash. Currently I have a script that looks like this: if [ "$f" != "A-C" ] && [ "$f" != "D-F" ] && [ "$f" != "G-I" ] && [ "$f" != "J-L" ]...
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
1answer
59 views

“[ - abc=10 ]” vs “[ – abc = 10 ]” [closed]

How are they different? I am using the [ command. I know we need a space after [ and before ], and that the test command is equivalent. But I can't find any information about the difference ...
1
vote
1answer
585 views

bash: test: no: integer expression expected [duplicate]

I believe this line if test "$suman_inspect" -eq "yes"; then is causing this error (verbatim): bash: test: no: integer expression expected I formulated the above test expression because I saw ...
0
votes
2answers
67 views

two variables pattern check at once

Is it possible to check if two variables match regex pattern at once and not one by one? Here is how it works now: # A=1.1 # B=1.2 # [[ $A =~ ^([0-9]+(\.([0-9]+(\.([0-9]+))?))?)$ && $B =~ ^([...
0
votes
2answers
2k views

bash + compare variable with spaces

I want to print the firewall-cmd --state status in variable - $STAT and then compare the variable $STAT with "not running" string but Comparison get false , it is because spaces inside the variable ...
0
votes
1answer
226 views

Bash - issue with grep conditional statement

Why does this code work correctly, while the other version of the same condition doesn't? if grep -q string file; then echo found else echo not found fi This doesn't work: if [ ! `grep -q ...
0
votes
3answers
3k views

How to check multiple conditons in a IF statement

I am trying to test for multiple conditions in the following if statement: If [[[ "$var1" = "$var2" || "$var1" = "$var3" || "$var1" = "$var4" ]]]; However, when I execute the above mentioned syntax ...
3
votes
4answers
377 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 ...
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 ...
8
votes
2answers
2k views

Why do [[ -z ]] and [[ -v ]] have different syntax?

If I want to perform some commands given variables aren't set I'm using: if [[ -z "$a" || -z "$v" ]] then echo "a or b are not set" fi Yet the same syntax doesn't work with -v, I have to use: if [...