Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
The results are in! See what nearly 90,000 developers picked as their most loved, dreaded, and desired coding languages and more in the 2019 Developer Survey.

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.

5
votes
2answers
66 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
23 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
57 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
51 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
90 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
76 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
746 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
261 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
121 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
45 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
152 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
127 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
499 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
65 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 ...
9
votes
3answers
2k 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
74 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
1k 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
54 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
410 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
95 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
362 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
50 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?
-1
votes
1answer
651 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
60 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 ...
0
votes
3answers
172 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
18 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' ...
0
votes
0answers
82 views

Creating a script to record CPU, MEM, and TEMP of RPI

The main goal of this script is to record the CPU, memory, and temperature of a Raspberry Pi 3 B. I have an outline of my script: #!/bin/bash #repeat test 20 times using for loop for i in {1...20} ...
4
votes
3answers
477 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
134 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
703 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
123 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
326 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
335 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
1k 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
5k 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 # ...
2
votes
2answers
3k 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
760 views

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

I need to understand this command: [ $? == 1 ] && return 0
0
votes
2answers
803 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
171 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
49 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
704 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
4k 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
86 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
121 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
164 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
45 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
1k 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 ...