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

-1
votes
1answer
23 views

script for automount, using if statement. cannot got the result [duplicate]

This script should auto mount a file system on a Linux server. testcheck=`df -h | awk '{print $6}' | grep "/test"`; if [$tescheck -ne "/test"] then mount /test else echo "failed"; fi I'm ...
14
votes
3answers
1k views

What is the purpose of square bracket executable

I see there is an executable called "[" in /usr/bin. What is its purpose?
5
votes
4answers
612 views

Why does `==` behave differently inside `[ … ]` in zsh and bash?

I get what I expected when doing this in bash: [ "a" == "a" ] && echo yes It gave me yes. But when I do this in zsh, I get the following: zsh: = not found Why does the same command ...
1
vote
2answers
45 views

Understanding redirected if-statement in bash

if [ -z "$OPTION" ] # if option is not given(empty) then: then command1 --defaultOption else command1 $OPTION fi \ 2> >( function1 "$DETAILS" ) \ < <( command2 ...
7
votes
2answers
485 views

Why does this bash conditional check work with [[ -n .. ]] but not [ -n .. ]? [duplicate]

I have a script that fails to detect zero length strings,the script uses [ -n $value ] in a bash conditional expression, i.e. #!/usr/bin/env bash value="" if [ -n $value ] then echo "value is ...
0
votes
1answer
47 views

Linux “stress” tool

I have seen one of the trick to use 90% of the available system memory with following command : stress --vm-bytes $(awk '/MemFree/{printf "%d\n", $2 * 0.9;}' < /proc/meminfo)k --vm-keep -m 1. ...
2
votes
2answers
65 views

Comparing strings with special characters using if-loop does not work

I want to compare each line of a particular file with the following string #orb_plugins = ["local_log_stream", "iiop_profile", "giop", "iiop"]; ("file.txt" contains this particular line) I tried ...
2
votes
1answer
265 views

shell: The =~ operator does not work as expected

Consider the following script: #! /bin/bash line="confusing" if [[ $line =~ [[:lower:]]* ]] then echo "matches!" else echo "does not match." fi While the above produce the expected ...
2
votes
2answers
61 views

Help with script reading numbers from a file and determining if they are even or odd numbers

So I want my script to test for the existence of a file which I already populated with numbers. It does that, but after it does that I want it to run a while look checking to see if the numbers in the ...
0
votes
2answers
62 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 ...
2
votes
2answers
53 views

Bash [[ pattern matching not working

The Bash reference section on conditional constructs says: When the ‘==’ and ‘!=’ operators are used, the string to the right of the operator is considered a pattern and matched according to the ...
4
votes
2answers
258 views

Bash Logic backwards on one system, but not another

I have two linux systems. I am testing for the existence of a program on each system. One system has the program installed, another does not. Somehow, each system evaluates the following example the ...
0
votes
1answer
49 views

If and else statement using diff -q

So I'm trying to write an if else statement which involves the use of diff -q. So, let's say I have two files hi.txt and hello.txt and I store them into variables called hi and hello respectively. I ...
0
votes
1answer
273 views

linux /dev/root does not exist after custom ISO build of CentOS 7

I am building a custom ISO for CentOS 7 and for now I am just intending for this to be an absolute minimal install (a proof of concept basically). I am re-creating the ISO via using mkisofs, the only ...
0
votes
2answers
56 views

Problem with data comparison in if loop

I have 2 variables in bash. Using test i found out that one of them is integer and other one is string. I want to compare them and see if they are equal. I tried the following options. if [ "$a" -eq ...
2
votes
2answers
103 views

while-loop doesn't seem to remember variable update - no piping involved

I am running LMDE 2 ( 'Betsy') with the standard Mate desktop and I have the following script: #!/bin/bash # This script starts a specified terminal-binary in "Always on Top"-mode # The assumption ...
3
votes
1answer
304 views

How to use user input as a while loop condition

I can do this in bash: while read -n1 -r -p "choose [y]es|[n]o" do if [[ $REPLY == q ]]; then break; else #whatever fi done which works but seems a bit redundant, ...
1
vote
1answer
423 views

How do I test if a file exists using ZSH? [duplicate]

This question is about ZSH, not bash. I have the following lines in my .zshrc file. Whenever I open a terminal I get a no matches found error referencing the line with the if statement. if [[!( -a ...
7
votes
3answers
231 views

What does `[ EXPRESSION ], [ ] and [OPTION` mean in `man test`?

Examining man test, I see that under synopsis for test are the possibilities test EXPRESSION and test. What does [ EXPRESSION ], [ ] and [OPTION mean below ? Why are the brackets empty and why does ...
0
votes
4answers
217 views

If condition with ssh command inside

I'm doing a bash script to backup my computer to a local server. I need to compress the archives but I can't find a way to make this if condition work with an ssh command inside: if [ ssh ...
-1
votes
3answers
156 views

Why does the builtin `test` fail with 'less than' (<) and 'greater than' (>)?

This code: #!/bin/bash if [ "4" = "a" ]; then echo "equal"; fi if [ "4" > "a" ]; then echo "yes 1"; fi if [ "4" < "a" ]; then echo "yes 2"; fi echo "exit" prints this three lines: yes 1 yes ...
3
votes
1answer
114 views

Bash test on successful subshell command returns false

I don't quite understand this behaviour of bash test operator. I used this line to determine if vagrant is installed. if [ $(vagrant --version > /dev/null) ]; then echo "HELLO"; fi I now know ...
2
votes
1answer
262 views

arithmetic syntax error in string compare

I have written a shell script which takes file name as parameter e.g user/test.txt. I want to make this file parameter optional if user does not wish to provide file name he/she can give as "None" ...
0
votes
1answer
42 views

Print the values from files

I have two files $TMP_RPT_FILE and $TMP_RPT_FILE1 . In $TMP_RPT_FILE - Meter readings come and $TMP_RPT_FILE1 - Meter Id come . I want if any data came in any of those files it should print . I tried ...
1
vote
2answers
294 views

What does [[ -t 1 ]] do?

To my understanding, the script below runs if the user is not root by comparing $EUID and 0. Then, it uses [[ -t 1 ]] to decide if the script is running in a terminal or not. If it is, it will use ...
3
votes
1answer
43 views

Condition on finding a file

I'm trying to write a script which looks for a file and if it is found, saves the full path to a variable. Current version: if [[ -n FOUND_PATH=$(find "$PATH" -name "file.exe") ]]; then echo ...
0
votes
1answer
148 views

Numeric test in bash: integer expression expected

I created a simple script in bash language to do cleaning of the recycle bin in Ubuntu. here is the script "recycle.sh" #!/bin/bash PUBLIC_PATH="/home/fileserv/.recycle/Public" ##### Clean public ...
6
votes
3answers
197 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
519 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
2k 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
45 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
157 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
39 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
188 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
146 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 ...
1
vote
2answers
82 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
19 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
125 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
160 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
371 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
482 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
28 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
76 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
234 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
2k 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
129 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
476 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
484 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
108 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 ...