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
3
votes
1answer
49 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
2answers
995 views

Script to check existence of and compare directories file count

I'm still pretty new to unix and scripting and I'm trying to write a script that can take an input directory from the user, check if it exists, then compare the number of files in the given directory ...
2
votes
1answer
885 views

I want to compare two list and print the difference output from list one

I have two variables input: VAR1="abc red blue cat empty dummy rummy" VAR2="rummy zero empty rat cat reverse" output: I want output as follows: (remove the common ones from $VAR2 in $VAR1) abc red ...
0
votes
1answer
15 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
295 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
55 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
68 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
23 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
65 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 ...
29
votes
8answers
81k views

Test if there are files matching a pattern in order to execute a script

I am trying to write an if statement to test whether there are any files matching a certain pattern. If there is a text file in a directory it should run a given script. My code currently: if [ -f /*...
0
votes
2answers
89 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 (...
4
votes
2answers
1k 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 ...
8
votes
2answers
7k views

Shell logical not

what is preferred between if ! [ ... ]; then and if [ ! ... ]; then actually they do the same result, is there a preferred syntax? in the former syntax the evaluated not is the shell builtin, ...
1
vote
0answers
109 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
59 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 ...
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 ...
2
votes
1answer
103 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 ...
25
votes
4answers
224k views

Starting with bash: -lt and -gt arguments

I'm starting with bash and I found the following: if test $first -lt $second then echo $first is lower than $second else if test $first -gt $second then echo $first is higher than $second ...
44
votes
7answers
9k views

Is test or [ or [[ more portable both between bash shells and between other shells?

I see I can do $ [ -w /home/durrantm ] && echo "writable" writable or $ test -w /home/durrantm && echo "writable" writable or $ [[ -w /home/durrantm ]] && echo "writable" ...
2
votes
3answers
77 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 [[ $...
5
votes
2answers
12k 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 ...
0
votes
1answer
66 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 [[ "$...
1
vote
3answers
8k views

Nmap check if port is open in bash

I want to make a script which will check if a port is open on a server. If not open stay in a while. If open continue. The break conditions is use are if "host is up" is present and not "closed". I ...
0
votes
3answers
107 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
124 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 ...
1
vote
1answer
4k views

Difference between '>' and '-gt'

What's the difference between these: SEC=$(mysql -hxxx -Pxxx -uxxx -pxxx -se "SELECT Seconds FROM tablename") SEC=100 Both return the same result (100). But when I test it with: if [ ${SEC} > 1 ...
0
votes
2answers
54 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
96 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
113 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
359 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 ...
3
votes
2answers
141 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
2
votes
1answer
335 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" =~ "...
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 ...
1
vote
1answer
81 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/...
2
votes
0answers
77 views

test fails to find files with processed path

I have backup of /home/kuba/mp3 on external usb hard drive /media/kuba/JCUBIC/mp3 and I move some files to different directories on usb drive and now I want to sync the files from usb to my home ...
20
votes
3answers
51k views

Comparing integers: arithmetic expression or conditional expression

In Bash, two integers can be compared using conditional expression arg1 OP arg2 OP is one of -eq, -ne, -lt, -le, -gt, or -ge. These arithmetic binary operators return true if arg1 is equal ...
1
vote
1answer
51 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 ...
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
votes
1answer
31 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
106 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 ${...
70
votes
12answers
171k views

How do I check if a variable exists in an 'if' statement?

I need to check a variable's existence in an if statement. Something to the effect of: if [ -v $somevar ] then echo "Variable somevar exists!" else echo "Variable somevar does not exist!" ...
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 ...
0
votes
2answers
62 views

Can't get if statement to run correctly [duplicate]

I want to check if a file exists, and if it doesn't exists, I want to ask the user if they'd like to create it. Whether the user inputs Y or N, only "Whatever you say" appears on the screen. #!/bin/...
0
votes
3answers
6k views

What does -o mean in an “if”?

What is -o after -eq in the mentioned code: ...[ $sorszam -eq 0 ] && min1=$ertek; [ $sorszam -eq 1 -o $sorszam -eq 2 -o $sorszam -eq 3 ] && [ $ertek -lt $min1 ] && min1=$ertek....
5
votes
2answers
87 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 ...
1
vote
3answers
3k views

0 byte file created in a simple script

I have a script as below: #!/bin/bash df -k | tr -s " " "," | awk 'BEGIN {FS=","} {print $1,$5}'|sed 1d > file1.txt while read partition percentUsed do if [ $percentUsed > 75 ] then echo ...
2
votes
3answers
3k views

How can I create a bash script which runs irb, then some ruby code?

I'm trying to do this to make unit testing a breeze for myself. The contents of an example script in question are as follows: irb require 'random_utils.rb' a = SuccessChecker.new Right now this ...