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

2
votes
3answers
67 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
1k 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
41 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
60 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
50 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 ...
8
votes
3answers
337 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
23 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
55 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 ...
2
votes
3answers
95 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
73 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 ...
12
votes
3answers
755 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
43 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 ] ...
11
votes
3answers
635 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
68 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
173 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 ...
4
votes
3answers
330 views

Which regular expression methods to validate input could be used in shell scripting?

#!/bin/sh re="\/$" if [ $1 =~ $re ]; then echo "${ATTENTION_PREFIX}$1 DIRECTORY MAY NOT CONTAIN A \"/\" OR LITERAL SLASH!${ATTENTION_POSTFIX}" exit 1 fi Executing sudo file.sh ...
5
votes
1answer
61 views

Testing whether a file is local

I need to test in a script, whether a file is from a network mount or truly local. To be more clear: I need to test, whether parsing the contents will be fast or slow, but for my case local vs network ...
1
vote
0answers
40 views

Some errors have occured when I installed lucene, a perl module!

Some errors have occured when I installed lucene, a perl module: [xlwang@localhost Lucene-0.18]$ make test Running Mkbootstrap for Lucene () chmod 644 Lucene.bs PERL_DL_NONLAZY=1 ...
1
vote
3answers
120 views

operator in if condition

I am using the below script to move two days back when script runs at starting two days of the year and also check first and second days of every month and move two days back. if [$month="01"] ...
1
vote
1answer
48 views

How to Test if a file has characters in it?

So i have a part of this code and U want it to output "No such player" if only the names file is empty, i've tried using the test -s command but it gives an output of 0 even if the file is empty or ...
0
votes
1answer
92 views

How to use booleans in fish shell?

I switched to fish shell and quite happy with it. I didn't get how can I handle booleans. I managed to write config.fish that executes tmux on ssh (see: How can I start tmux automatically in fish ...
1
vote
4answers
736 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 ...
0
votes
1answer
90 views

How to write simple if condition in ash?

I would like to write a small script that would check every once in a while if I have the internet connection. I would like to run it in crontab on my router where I have ash. I am having a hard ...
1
vote
3answers
86 views

bash howto script - if no answer or mutliline answer

I have a script, that reads some input foo, and based on that input tries to calculate the value for the variable bar. If it is successful, then it shall set bar. If not, the user should be able to ...
0
votes
1answer
65 views

Check multiple directory properties

I'd like to make this test as fast as possible: (Bash) if [[ -d $directory_path && -r $directory_path && -x $directory_path ]]
-5
votes
2answers
70 views

z in bash script returns zero [closed]

#[ -z "" ] #echo $? returns 0 #[ -a "" ] #echo $? returns 1 except z all other alphabets throw 1 What might be the reason?
3
votes
3answers
142 views

Why is my condition always false?

I tried to write a script which creates a user. First it needs to check if: root is running if user maybe already exists I Also tried to set the password equal to the username which was typed ...
0
votes
1answer
204 views

ksh if with several conditions

I'd like to know what's the syntax for this kind of if in ksh : if [[ $tmpEngine != "a" || $tmpEngine != "b" || $tmpEngine != "s" ]]; then ... fi Actually, my code doesn't work, what's the ...
0
votes
2answers
244 views

Test if number from range <0,infinity)

I have a problem If [[ * ]] then continue else exit 1 fi I want to test that the argument to my switch (for example -d 3) is a valid positive decimal integer number (a sequence of one or more of ...
0
votes
1answer
330 views

Test variable if its string or not

I'm writing a script which will have some arguments and so I am using getopts but i want to solve the problem with one argument. I use a switch, for example -d, and I want the argument for -d will ...
16
votes
4answers
2k views

Why does while [ 0 ] go into infinite loop?

I see the same behaviour for below loop as the loop with while [ 1 ]. Why is that so? while [ 0 ]; do echo "hello" done
4
votes
2answers
6k views

How do I check if a file is a symbolic link to a directory?

I can check, if a file exists and is a symbolic link with -L for file in *; do if [[ -L "$file" ]]; then echo "$file is a symlink"; else echo "$file is not a symlink"; fi done and if it is a ...
1
vote
2answers
119 views

Is there a problem if I add a non-existing path to `PATH` environment variable?

I have a following lines in my .bash_profile. if [ -d "$HOME/bin" ] ; then PATH="$HOME/bin:$PATH" fi I think I found it somewhere in the internet. Is it also ok, if I write only ...
2
votes
2answers
4k views

bash: syntax error near unexpected token `}'

I tried using this: $ if [$a == 1] then { echo 'yes'; } fi; but I get an error: -bash: syntax error near unexpected token `}' What is the correct format? I tried several with no luck.
5
votes
6answers
17k views

How to compare two dates in a shell

How to compare two dates in a shell script? For example something like todate=2013-07-18 cond=2013-07-15 if [ $todate -ge $cond ]; then break fi It doesn't work. ...
3
votes
1answer
84 views

Specific string hangs [, [[

While fuzz testing I've found that it's possible, by defining var=$'\365\\' , to make both of the following commands (used to remove everything up to the last newline) hang indefinitely, consuming ...
1
vote
0answers
29 views

difference between non-builtin 'test' and '[' [duplicate]

I am aware that bash builtin test and [ commands are exactly the same, but what about /usr/bin/test and /usr/bin/[? I guess those two are not exactly the same as then one would be just a symlink ...
2
votes
2answers
2k views

Testing a string containing only spaces (tabs, or “ ”)?

My code below doesn't work: stringZ=" " if [[ "$stringZ" == ^[[:blank:]][[:blank:]]*$ ]];then echo string is blank else echo string is not blank fi Result: string is not blank # wrong ...
2
votes
2answers
1k views

How do I use an If-Else query based on the day of the week?

Problem: I need to check if today is Thursday and perform different actions based on the result of this condition. I tried two different approaches: Getting the Day Name: DAYOFWEEK=$(date +"%a") ...
5
votes
2answers
152 views

[ vs [[ : which one to use in bash scripts? [duplicate]

The zsh man page, in its section on test (aka [), explicitly advises against using it at all, and urges readers to use [[ whenever possible. The relevant section states: The command attempts to ...
1
vote
2answers
172 views

Nested read fails

I have a list of filenames in a file and want to do let the user decide what to do with each. In bash, iterating over filenames is not trivial in itself, so I followed this answer: #!/bin/bash while ...
8
votes
2answers
203 views

Difference between [[ $variable ]] and [[ -n $variable ]]?

In bash, are [[ $variable ]] and [[ -n $variable ]] completely equivalent? It appears to be the case judging by the output below, but I see both forms of usage prevalent in shell scripts. $ z="abra" ...
5
votes
2answers
2k views

bourne shell if [ -e $directory/file.$suffix ]

I've been looking inclusive I even checked the if manual for bash and tried in cli mode. #!/bin/sh CONFIG_DIR="/var/opt/SUNWldm/" read option if [ $option -eq 9 ]; then ret=1 elif [ -e ...
3
votes
2answers
133 views

POSIX test and -a

I checked a script of mine with checkbashisms and I got the following warnings: possible bashism in check_ssl_cert line 821 (test -a/-o): if [ -n "${ALTNAMES}" -a -n "${COMMON_NAME}" ] ; then In ...
4
votes
3answers
6k views

How to test if a variable is defined at all in Bash prior to version 4.2 with the nounset shell option?

For Bash versions prior to "GNU bash, Version 4.2" are there any equivalent alternatives for the -v option of the test command? For example: shopt -os nounset test -v foobar && echo foo || ...
15
votes
3answers
735 views

What is the difference between [[ $a == z* ]] and [ $a == z* ]?

Is there is any difference between these two. [[ $a == z* ]] and [ $a == z* ] Can I have an example where they would have different outputs? Furthermore, how does the working of [[ ]] differs ...
5
votes
3answers
1k views

Test command in unix doesn't print an output

When I type this in the terminal test 4 -lt 6 I don't get any output. Why not? I need that 0 or 1
6
votes
1answer
254 views

What's the point of using “x$START_DAEMON” = “xyes” here? (the heading x)

This was part of fetchmail deamon script, if [ ! "x$START_DAEMON" = "xyes" -a ! "$1" = "status" ]; then What's the point of using x$START_DAEMON = "xyes", why not just use $START_DAEMON = "yes" ?
12
votes
4answers
12k views

How can I use bash's if test and find commands together?

I have a directory with crash logs, and I'd like to use a conditional statement in a bash script based on a find command. The log files are stored in this format: /var/log/crashes/app-2012-08-28.log ...
2
votes
2answers
576 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 ...