Podcast #128: We chat with Kent C Dodds about why he loves React and discuss what life was like in the dark days before Git. Listen now.

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
1answer
48 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 /...
0
votes
1answer
47 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
21 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 ...
0
votes
1answer
52 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
69 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
38 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
91 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
56 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
87 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
61 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
74 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
3answers
101 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
83 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
48 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
69 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
75 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
99 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 ...
2
votes
1answer
152 views

How to check for presence of named pipe on the file system [duplicate]

I tried using the -f flag to test if a named pipe is present if [[ ! -f "$fifo" ]]; then echo 'There should be a fifo.lock file in the dir.' > /dev/stderr return 0; fi this check does not ...
2
votes
1answer
229 views

Bash: Regex for comparing file path

I want to check if an input string matches whitelisted file paths, then only I want to perform some operation. I am doing something like this path1="mydir/**" path2="mydir/testing" [ [ "$path2" =~ "...
3
votes
2answers
103 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
1
vote
1answer
64 views

Grep to check filesystem mount and test to check for file path is providing incorrect information

Even though the path exists, this script reports that it does not. What is the flaw in the logic? #!/bin/bash mount="/fileserver" if grep -qs "$mount" /proc/mount && { test -d '/fileserver/...
1
vote
1answer
47 views

Shell script to test ls output for directories

I am trying to make a BASH script to test the files in the working directory and return the files that are directory files. One approach that I can think of is, starting with ls and feeding the ...
-1
votes
1answer
30 views

why does test command startswith fail (exit with 1)?

I have this test command: test "ssh://hg@bitbucket.org/teros/moodwire_demo" == ssh*; echo $? why does it exit with 1? I am trying to see if the first string starts with the second string.
0
votes
1answer
78 views

if statement not working inside while loop

I don't understand why comparing strings is not working here: while IFS=, read -r ELMS_SIZE ELMS_NAME ELMS_ENV ELMS_DB do if [ "${ELMS_DB}" = "NOMATCH" ]; then echo "done" fi done < <(tac ${...
5
votes
2answers
81 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 ->...
1
vote
0answers
24 views

bc command unexpected results [duplicate]

I am trying to compare a decimal and it seems like bc is ignoring it. The OS is CentOS release 6.9 (Final) and bash is bash-4.1.2-48.el6.x86_64, below is the code #!/bin/sh i=32.39 IP_LOW_2=32.0 ...
3
votes
2answers
61 views

Exit statuses of comparisons in test constructs

I was writing some "if then" statements and found what seemed to me an odd behavior. Upon investigation I realized that it boiled down to the exit code of the comparison I was making. I illustrate my ...
1
vote
3answers
171 views

“==” logical operator and zsh version 5.7.x (installed using Homebrew)

Been getting errors on my scripts when using this operator. I've some online documentation and double equals should supposedly work. Any ideas? Zsh reference guide: http://zsh.sourceforge.net/Doc/...
23
votes
5answers
2k views

Why use double quotes in a [[ ]] test?

Let's say we have 2 integers in a bash script: value1=5 value2=3 Then why do we need to use double quotes in case of a test ? For example: if [[ "$value1" -eq "$value2" ]] Why not just use the ...
1
vote
2answers
190 views

Solaris script - modified date file age

I have a bash script on a Solaris server to provide an alert if a file is in a folder location for longer than 5 minutes. if [ -f $1 ] then a=0 else a=1 However, it is throwing a lot of false ...
0
votes
0answers
30 views

Solaris - modified date file age [duplicate]

I have a bash script on a Solaris server to provide an alert if a file is in a folder location for longer than 5 minutes. It seems the -mmin command doesn't work in Solaris. if [ -f $1 -mmin +15 ] ...
3
votes
2answers
138 views

A Unix AIX variable without a $ symbol?

I'm trying to debug an existing program and found an if condition without $ symbol prefixed to it. Values are: dt_val=1234 prev_dt_val=1234 If condition goes like: if [ dt_val -eq prev_dt_val ] ...
1
vote
1answer
2k views

Error [: -lt: unary operator expected in shell script Linux [closed]

I get error [: -lt: unary operator expected after run below script, anyone can share ideas? temp=0 while [ $temp -lt 25 ] do sleep 1 echo "running.." if [ $temp -eq 5 ] then ...
3
votes
3answers
414 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 ...
4
votes
2answers
150 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 ...
0
votes
2answers
63 views

auto install minecraft script

So I was working on this script to auto install minecraft and i began to wonder is there a better way to do this? #!/bin/bash sudo apt-get upgrade sudo apt-get update sudo apt-get autoclean echo " ...
1
vote
1answer
233 views

-w flag in bash if statement not working

I have a simple script that checks if a file has write permission on it or No. If yes, then it allows the user to append some text data to end of file. If No, it kicks user back out. I am following a ...
0
votes
2answers
160 views

java -version In Bash Script. Giving integer error

I am trying to make this if..fi case work for the Java Version from wrapper.conf configuration file./ and from the system, If the output from wrapper.conf is java I would have two options; 1. ...
0
votes
1answer
1k views

[: =: unary operator expected [duplicate]

I need to write a script to test if a service inside of an Android emulator is ready for apk installation or not using this command adb -s emulator-5554 shell pm list package | grep package:com....
2
votes
1answer
84 views

Running a Test with expression in a string

I am trying to run the test command from a string. The string contains the expression. TEST="! -e ~/bin/xyz" if [ `echo "$TEST"` ]; then echo running "$TEST"; fi However, the above if ...
10
votes
3answers
3k views

Schedule the last day of every month

I read from an instruction to schedule a script on the last day of the month: Note: The astute reader might be wondering just how you would be able to set a command to execute on the last day of ...
1
vote
2answers
113 views

bash script get result from test into variable

I'm trying to learn Bash scripting and for fun I would like to write a script which will monitor the httpd.service. The basic construct looks like this: #!/bin/bash SERVICE=httpd.service if [ "...
0
votes
2answers
3k views

Determining if the first string starts with second string

JavaScript has a function for this: 'world'.startsWith('w') true How can I test this with shell? I have this code: if [ world = w ] then echo true else echo false fi but it fails because it is ...
2
votes
2answers
72 views

Why does test -e fail on a file in a directory with only read permissions?

From what I understand about the read permission on directories, it allows listing of which files are in a directory and that's about it. Given a directory with 0744 permissions, owned by userA: [...
1
vote
3answers
933 views

check whether the user has a password set for his account in a script

I'm writing a script and need to check $user if his account has a password set, how can I do this? I know about this: passwd --status username Display account status information. The status ...
0
votes
1answer
97 views

If statement works with [ but not with [[ when comparing strings with -eq [duplicate]

My code: #!/bin/bash x=${#} for (( a=0; a<x; ++a)); do if [[ $1 -eq $1 ]] 2> /dev/null then variable[a]=${1} shift else echo "The argument $1 is NOT an integer. ...
4
votes
3answers
533 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. ...
-1
votes
1answer
94 views

Bash: How to escape minus in `if [ … ` (alias “test”)?

I need to test this: kill -0 $LAST_PID; So I wrote if [ kill -0 $LAST_PID ] ; then ... But this results in an error - unknown operator after - or so. how do I escape the options within if?
0
votes
1answer
1k views

Empty string is a file? ( if [ ! -f “” ] )

The script is called isFile.sh and looks like this: #!/bin/sh echo $1 echo $2 if [ ! -f $1 ]; then echo "$1 (arg1) is not a file" fi if [ ! -f $2 ]; then echo "$2 (arg2) is not a file" fi ...
2
votes
1answer
391 views

How to check if there are “other” permissions in home directories

Right now, I have a script that runs daily that chmod's the home directories, removing all "other" permissions from the directories and the "group" write permission. See below. #Removing all other ...