The Stack Overflow podcast is back! Listen to an interview with our new CEO.

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
2
votes
0answers
36 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
85 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
53 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
72 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
59 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
97 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
70 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
47 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
68 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
63 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
98 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
85 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
150 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
86 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
60 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
45 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
57 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
78 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
146 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
176 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
129 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
397 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
146 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
60 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
210 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
158 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
81 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
107 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
67 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
837 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
515 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
90 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
299 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 ...
1
vote
3answers
273 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
877 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
215 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
850 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 ...