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
votes
1answer
33 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
73 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
190 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
66 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
350 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
41 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
57 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
34 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
18 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
154 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.
0
votes
1answer
328 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
29 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
96 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: ...
1
vote
0answers
51 views

Measuring Unix / Linux skill level [closed]

Is there a test (online or pencil & paper) to measure a persons skill level in Unix / Linux (command line stuff). I’m thinking of basic and intermediate level, not professional guru level. It ...
0
votes
2answers
74 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
0answers
31 views

Creating global variable from bash script [duplicate]

In the terminal, set ran1=1 run the script Why does ran1 not = 3 when thw script is run again? The code in the loop, should only run once, not matter how many times the script is run. #!/bin/bash ...
0
votes
3answers
49 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
32 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
65 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 \ ...
4
votes
1answer
104 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
17 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
2
votes
2answers
2k 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
63 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
176 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
30 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
127 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
215 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
39 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
141 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
69 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
121 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
83 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
81 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
463 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
372 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
181 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
66 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
136 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
281 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
744 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
495 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
462 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
180 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
87 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
33 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
177 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
102 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
2 3 4 5
7