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

0
votes
0answers
26 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
50 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
86 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
265 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
118 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
52 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
76 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 ...
29
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
51 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
685 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
100 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
62 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" ...
2
votes
3answers
682 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
2k 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
65 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
100 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
81 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
358 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
27 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
80 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
424 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
115 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 ...
11
votes
3answers
7k 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
104 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
807 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
148 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
200 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
598 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
64 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
58 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 ...
2
votes
3answers
263 views

Brackets in if condition: why am I getting syntax errors without whitespace?

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
50 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
135 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
3k 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
319 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
2answers
171 views

Why are bash tests so picky about whitespace?

As a primarily Java programer, I find the bash if-then construct quite confusing, especially regarding whitespace. Can anyone explain, why the first one works but not the second and third? #works if ...
1
vote
3answers
94 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
84 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
81 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
159 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
366 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
309 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
390 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
3k 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
6
votes
2answers
10k 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
152 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
5k 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.
8
votes
7answers
26k 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
85 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 ...