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.

learn more… | top users | synonyms

4
votes
0answers
86 views

Peculiar Solaris integer comparison behaviour

This one caught me unawares! We have a Solaris machine (version: SunOS 5.8) hosting a pivotal script/process which has been running for many years without complaint. We recently had cause to have a ...
1
vote
3answers
94 views

String comparison in single brackets in zsh [duplicate]

Bash code to print all folders: for f in ~/*; do if [ $f == '/home/sk/.' -o $f == '/home/sk/..' ]; then true else echo "$f" fi done It works on bash. When i ran the code on z ...
1
vote
2answers
103 views

How to see if the directory exists?

How can I check if /bin/x86_64/bin/ls is a directory in a bash shell script Here is what I tried: #!/bin/bash if [ $# -eq 2 ]; then if [[ "$1" = /* ]] then cd ./bin/x86_64/bin/ls ...
0
votes
2answers
43 views

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

I am very new to bash, just playing around in my free time off work. 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 ...
1
vote
2answers
85 views

works as a command but not as a shell script. Why?

$ grep "apple" fruits.txt apple $ if [ $? == 0 ] ; then echo "A"; else echo "B"; fi A When I execute the above commands it works fine but when I run these commands as a shell script it throws error ...
0
votes
0answers
38 views

How do I write a script for testing username/password? [duplicate]

I have about 500+ hosts I need to test a username/password combo to make sure there aren't any false positives in the list. I just need to make sure I can login and log out, no need to run anything ...
1
vote
1answer
46 views

Upgraded to test branch by mistake in debian… how can I go back to stable?

I wanted to upgrade my GLIBC to a newer version on my remote server (to which I only have SSH access), so I added the test branch of Debian to my apt sources. I followed this by the mistake of ...
1
vote
2answers
34 views

How to check if a group of files is readable?

I have to check if a group of files is readable. I know how I would go about checking one file by using [ -r file.txt ], but how would I go about writing a loop that would check multiple files ...
0
votes
1answer
37 views

Congestion Control algorithm performance testing in FreeBSD

I am working on implementation of a proposed modification of TCP Congestion Control algorithm that would allow to improve it (the CC) by adding a dynamic bandwidth estimation. I have been able (with ...
0
votes
0answers
18 views

Bash Scripts arguments and G++ static and dynamic library generation [duplicate]

I have a library named matrix.cpp that is used by a program named test.cpp. Now, I want to write a BASH script which takes matrix.h and matrix.cpp as input and generates matrix.a and matrix.so ...
1
vote
1answer
73 views

bash: -eq vs. == and `bc` output type

This snippet of code has been taken from a larger script, (humbly) simulating the non-existing abs() function: [[ $(echo "$val < 0" | bc) -eq 1 ]] && val=$(echo "$val * -1" | bc) So we ...
1
vote
3answers
55 views

Unix - ksh test if multiple variables are 0

So basically I want to test if 3 variables are 0. If one of them is not it should report it. This is what I got: if [[ $result -ne 0 && $resultmax -ne 0 && $resultmin -ne 0 ]] ...
4
votes
5answers
365 views

Script tries to create files even though it shouldn't have to?

I have a folder which contains some folders, these folder are moved very often so I made a script to see if they exist, and if not then create them. This is what I did to (which I though would) ...
15
votes
2answers
428 views

Bracket expression (without ranges) matching unexpected character in bash

I'm using bash on Linux. I am getting a success from the following if statement, but shouldn't this return a fail code? if [[ ■ = [⅕⅖⅗] ]] ; then echo yes ; fi The square does NOT equal any of the ...
0
votes
1answer
25 views

Why testing return value from fgrep always false in below code?

I have the below script to find missing items from fileA compared to fileB and write to fileC script.sh fileA fileB fileC script.sh: rm $3 while IFS="" read -r inputline; do fgrep -q ...
3
votes
1answer
46 views

Trailing colons and dash's builtin test command

What's the meaning of a trailing colon to dash's builtin test (or [) command? I was surprised when I found that if [ ... ]: ; then .. ; fi works (at all) in dash, but not bash, and it seems to be ...
1
vote
1answer
60 views

Using the test command to create a directory if it doesn't exist

I was just wondering whether or not this script code would properly test for the existence of a dir2, and if it doesn't exist, then to create it. Dir1 already exists. [ ! -d /dir1/dir2 ] && ...
0
votes
1answer
386 views

“too many arguments in [ (test) statement”

this is my first "if-else" script on linux. It gives the error [: too many arguments Code: n=0 if [ $n % 2 -eq 0 ] then echo "even number" fi How to correct it.
1
vote
0answers
51 views

How to simulate power outage for a single NUT client?

I've changed the behaviour of a single server (configured as netclient) when on UPS, but I'm not sure how to test it in isolation. dummy-ups seems related, but even the configuration looks completely ...
1
vote
3answers
225 views

How do I make an output for grep fail?

I am using if-else statement to search for keywords and displaying the results in the terminal, here's an example of my code. read finding if ["$finding" != "" ]; then grep $finding ...
1
vote
2answers
425 views

Can I put more than 1 condition in if?

Is it possible to put more than 1 condition in an if statement? if [ "$name" != "$blank" && "$age" == "$blank" ]; then Is it possible? If not how am I supposed to do that?
0
votes
1answer
54 views

How do i create a function to test if the input contains any character? [duplicate]

I have this set of example here to test if the user has keyed in any data after he presses enter. echo -n "Type a digit or a letter > " read character blank="" if [ "$character" != "$blank"]; ...
0
votes
0answers
27 views

ksh if/elif statement not working with || and && [duplicate]

NOTE: Pertaining to the duplicate referenced - I already tried that prior to posting here and got the exact same results. I've posted an additional section below with this so that you can see what I ...
2
votes
2answers
311 views

“If variable does not contain” not working

This is my script if [[ ! $url == *.txt ]] then exit fi I have also tried: if [[ ! "$url" == *.txt ]] then exit fi and: if [[ "$url" !== *.txt ]] then exit fi But even though $url does ...
1
vote
3answers
218 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
969 views

Bash File existence test is always true

I have the following lines of bash script to test whether a file exists: MYAPPPATH=$(find $APPDIR -iname myapp* -print) if [ -e $MYAPPPATH ] then echo "File exists" fi However when file ...
3
votes
3answers
212 views

Why is my script calling multiple functions? [duplicate]

I am new to writing scripts in bash. My script is simple, I have a few sets of scripts that I've written to make setting up linux very simple. So I have made those scripts into separate functions ...
2
votes
1answer
76 views

What is the most portable syntax for `if` statements?

(NOTE: There is a very similar question, asked yesterday oddly enough, but it doesn't cover the main syntaxes I'm concerned with. If I were to update the other question to include them it might ...
2
votes
4answers
151 views

unix test when to use eq vs = vs == in test commands?

When should I use -eq vs = vs == e.g. [[ $num -eq 0 ]] [[ $num = 'zzz' ]] I've observed a pattern of using -eq (and -ne, etc.) for numbers and = for strings. Is there a reason for this and ...
34
votes
7answers
2k 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 ...
1
vote
0answers
18 views

Command is not found [duplicate]

I have this code #!/bin/bash read num sum=0 i=1 while [$i -le $num]; do let sum=$sum+1 let i=$i+1 done echo $sum I am getting the error ./test: line 5: [1: command not found , but I am not ...
2
votes
1answer
1k views

Why am I getting “too many arguments”

Here's a part of my script that is telling me that I have too many arguments on line 3: #!/bin/bash export LC_ALL='C' if [ `echo "*file 2" | grep -o ^.` = '.' ] then echo success fi Anybody ...
0
votes
1answer
6k views

Bash if statement [: missing `]' error [duplicate]

I am having trouble with bash. I am trying to put a command in an if statement and then compare it to a string. This works perfectly. echo $(ipcs | grep Shared | awk '{print $2}') When I put it in ...
1
vote
2answers
120 views

bash question about if and then [duplicate]

I'm writing this in my script. It has other parts but I'm getting stuck on this part only. if [[$# == $year $month $day ]] ; then cal $day $month $year fi When I run this it give me this msg: ...
0
votes
2answers
78 views

Bash script: test command -o option

I have a piece of code like this: count=0 until [ -f $filename -o $count -ge 60 ] do count=$((count+1)) sleep 1 done I know [ -f $filename -o $count -ge 60 ] is a test command, "-f $filename" ...
3
votes
3answers
5k views

Checking if an input number is an integer

I'm trying to check if an input is an integer and I've gone over it a hundred times but don't see the error in this. Alas it does not work, it triggers the if statement for all inputs ...
8
votes
4answers
4k views

Why is the 'if [ $1=“1” ]' branch always selected even if $1 is not 1?

I have a shell script named 'teleport.sh' like this: if [ $1="1" ]; then shift mv "$@" ~/lab/Sun elif [ $1="2" ]; then shift mv "$@" ~/lab/Moon elif [ $1="3" ]; ...
1
vote
1answer
157 views

Expand shell variable without globbing

I want to check whether an input string refers to a file name - not a wildcard string like *.txt. This doesn't work: if [ -f "$1" ]; Because $1 gets expanded to *.txt, which gets expanded to, say ...
-1
votes
1answer
136 views

Bluehost to Digital Ocean: Building a Web Development / Test Environment on Laptop

For the past few years I've been a Bluehost / LAMP site developer, but now i intend to switch to Digital Ocean to get a competitively-priced VPS i can use to pursue other technologies (nginx, Scheme ...
2
votes
3answers
110 views

bash: regular expressions in if expression

In my script on my ubuntu machine I declare a string like this: DEBUG_PACKAGE_LIST=$(apt-cache search dbg | awk '{ print $1 }' | grep -e "-dbg") For you to help you understand my problem here is ...
10
votes
3answers
423 views

What is the difference of -a and -e in bash's conditional expressions?

From man bash: CONDITIONAL EXPRESSIONS [...] -a file True if file exists. [...] -e file True if file exists. So what is the difference between [ -a $FILE ...
1
vote
3answers
28 views

Where can I find the list of [[ ]] options?

like [[ -s file ]] [[ -z string ]] preferably on the web. I looked on the ldp site but couldn't find them The man page (man [) is helpful but I am looking for a more compact list.
0
votes
1answer
113 views

Compare two strings in a shell script [duplicate]

My script needs two arguments. I want to hide the error message if someone calls the script with script.sh --help so I tired this: if [ $# -ne 2 ] ; then if [ "$1" -ne "--help" ]; then echo ...
6
votes
3answers
2k views

waiting for network in a bash script

I'm running a script that relies on network being up and a network share be mounted. The script runs on login (which happens automatically after boot). The problem is that by the time the script runs, ...
2
votes
2answers
176 views

How to write a function in bash?

I am drawing blank here. How should I have written this script? #!/bin/sh XZY_NODES="\ vl-ocnt-1 \ vl-ocnt-2 \ vl-onet-1 \ vl-onet-2 \ vl-ocmp-1 \ vl-oomp-2 \ " is_xyz_node () { host=$1 ...
14
votes
3answers
22k views

Comparing two strings in Bash

I have the following if block in my bash script: if [ ${PACKAGENAME} -eq kakadu-v6_4-00902C ]; then echo "successfully entered if block!!" fi The script execution is not entering my if block even ...
3
votes
2answers
194 views

Comparing an string by value of a floating number to a number

I wrote the code below: for j in `cat output_10.txt` do dri=`echo $j |cut -d':' -f1` nu=`echo $j |cut -d':' -f2` tot=`echo $j |cut -d':' -f3` fre=`echo $j |cut -d':' -f4` if [ $fre > 2 ] ...
13
votes
3answers
2k views

Is there any major difference when comparing a variable as a string or as an int

Out of curiosity, when doing a bash variable comparison (its value being an integer) it's possible to test it against some predefined value either declared as an int or as a string. Sample script: ...
1
vote
3answers
444 views

what does the -z flag mean here

What does the -z flag mean in the code below: if [ -z $TEST_PARAM ]; then And is there a list of such flags? For a flag like ls -l, I know where to find it, but for a single flag, I didn't get any ...
5
votes
1answer
228 views

Test -n gives unexpected result

I understand test -n <expression> to return false if the expression evaluates to a string length of greater than 0. Why then does the following happen? Macbook:~ echo ${#undeclared_variable} 0 ...