We’re rewarding the question askers & reputations are being recalculated! Read more.

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.

Filter by
Sorted by
Tagged with
12
votes
3answers
4k views

What does [ -t 1 ] check?

I just found a way to start zsh when I start the bash on Windows from https://www.howtogeek.com/258518/how-to-use-zsh-or-another-shell-in-windows-10/. It recommended to add following code at the ...
4
votes
1answer
1k views

if condition always evaluating true [duplicate]

if [[ ${fin[2]} -eq OK && ${fin[7]} -eq NA ]] then echo "<tr id="green">" >> /tmp/mailt.txt elif [[ ${fin[2]} -eq OK && ${fin[7]} -lt 0 ]] ...
1
vote
3answers
7k views

Nmap check if port is open in bash

I want to make a script which will check if a port is open on a server. If not open stay in a while. If open continue. The break conditions is use are if "host is up" is present and not "closed". I ...
1
vote
1answer
1k views

Command substitution in if statement [duplicate]

I'm trying to compare the result of a command substitution to a string, like this: if [$(ping $1)=="ping: unknown host localhosts"] then echo "no"; else echo "yes"; fi What am I doing wrong here?...
0
votes
2answers
885 views

Script to check existence of and compare directories file count

I'm still pretty new to unix and scripting and I'm trying to write a script that can take an input directory from the user, check if it exists, then compare the number of files in the given directory ...
0
votes
1answer
36 views

cPanel scanning script error when comparing two variables: [: : integer expression expected

I'm trying to implement real time scanning on cPanel server. When I use the following script, #!/bin/bash #Maximum file size to scan in bytes that's set to 10MB MAXSIZE=10485760 if [ "$UPLOAD_SIZE" ...
1
vote
2answers
113 views

When and When not to use [ ] operators with if?

#!/bin/bash echo 'Want Task 1' read r1 ; if [ echo "${r1}" | grep -iq "^y" ] ; then echo 'Task One Done' echo 'Want Task 2 ??' read r101 if [ echo "${r101}" | grep -iq "^y" ] ; then ...
1
vote
3answers
499 views

Can't compare lsb_release result to string in Bash

I'm doing something wrong on the string comparison below. It works if I set a variable and compare that, but I can't copy the value to a string. Does anyone know what's wrong? $ if [ "$(lsb_release -...
3
votes
2answers
340 views

bash if statement different behaviour

I have done some testing of the bash if statement behavior, but I am not sure whether I understand them correctly on the reason of the output. Below are the reason I am proposing from each different ...
5
votes
3answers
7k views

shell test whether multiple lines string contains specified pattern in last line

I want to determine whether a multi-line string ends with a line containing specified pattern. These code failed, it doesn't match. s=`echo hello && echo world && echo OK` [[ "$s" =~ ...
4
votes
2answers
158 views

Shell: why [ -n “${VAR-}” ] instead of [ -n “$VAR” ]?

I see a lot of shell scripts (for example, this one) checking for a variable's presence/absence like: [ -n "${VAR-}" ] As far as I can tell, using the ${VAR-fallback} form without providing a ...
0
votes
2answers
1k views

Usage of -n and -z in test built in - Bash

Consider the following in bash: root@debian-lap:/tmp I=$(echo) root@debian-lap:/tmp echo "$I" root@debian-lap:/tmp [ -z "$I" ] && echo "TRUE" || echo "FALSE" TRUE This means that variable $...
4
votes
2answers
2k views

bash - What do the brackets in if-statements do? [duplicate]

Perhaps this applies to more than just bash, but I am confused about the role of brackets in if-statements. Most examples seem to have the following format if [ expression ]; then #do stuff fi ...
1
vote
4answers
14k views

IF statement to return true if a word contain a specific letter

I need an if statement to return true if a word contain a specific letter. For example: var="information" if [ $var contain "i" ]; then .... else ... fi
2
votes
0answers
77 views

test fails to find files with processed path

I have backup of /home/kuba/mp3 on external usb hard drive /media/kuba/JCUBIC/mp3 and I move some files to different directories on usb drive and now I want to sync the files from usb to my home ...
3
votes
1answer
215 views

What is the purpose of testing $DISPLAY with [ -z $DISPLAY ]?

In the very beginning of a script stands: if [ -z $DISPLAY ] then DIALOG=dialog else DIALOG=Xdialog fi I understand, that $DISPLAY holds the value of which pseudo-terminal one is attached to [ -...
0
votes
3answers
1k views

Bash Script, find file size and compare not running [duplicate]

Very new to bash as you will see in code below. I want to get the file size in bytes of a file, compare that to a fixed value and send an email if the latter is too small. Code: #!/bin/bash f=$(...
0
votes
3answers
5k views

What does -o mean in an “if”?

What is -o after -eq in the mentioned code: ...[ $sorszam -eq 0 ] && min1=$ertek; [ $sorszam -eq 1 -o $sorszam -eq 2 -o $sorszam -eq 3 ] && [ $ertek -lt $min1 ] && min1=$ertek....
0
votes
2answers
823 views

If condition not working in script over ssh

Below shell script on SSH mode is not working. variable end_pos is getting value "Stop", but when it comes in IF loop , it doesn't check the condition and loop is getting failed. Output of command : ...
1
vote
1answer
310 views

What is the meaning of &amp with regard to the test command?

I learned about the command line command test and read a few examples. One of them was obscure to me: test 100 -gt 99 &amp;&amp; echo "Yes, that's true." || echo "No, that's false." I ...
2
votes
1answer
467 views

If condition works for first time but not for the second time

I have defined the below for and if condition in a function. For the first time it runs well. Second time it doesn't gets executed after the for loop, it just writes the echo statement. Why does it ...
0
votes
1answer
3k views

What is the difference between square brackets and double parentheses in Bash? [duplicate]

I notice in this question that one answerer used double parentheses where the other used square brackets: if (( $(fileSize FILE1.txt) != $(fileSize FILE2.txt) )); then ... if [ $(fileSize FILE1.txt)...
0
votes
3answers
1k views

shell scripting - multiple equality tests in one if statement

So I create a script and it works perfect except at the end when I type in a beverage it executes a line its not suppose to. The Last line is only suppose to come up when I type "no" or "No"...What ...
1
vote
2answers
7k views

How to use comparison operators in bash? [duplicate]

How can I use <=, >=, >, and < in bash? Instead of: if [[ $arg1 -ge $num1 && $arg2 -le $num2 ]]; then ... fi Have something more like: if [[ $arg1 >= $num1 && $...
2
votes
2answers
128 views

How to correctly test file's extension in if statement?

Just an illustration: first cd /usr/lib and run for x in $(ls);do if [ "$x" == "*.a" ];then echo $x;fi;done What I want to do is to select files with extension .a and print them. I know there ...
1
vote
1answer
613 views

Bash reliable way to test file exists

I recently found that bash test -e doesn't work with circular symlink. ln -s ./test ./test if [[ -e ./test ]]; then echo "exists"; fi #this won't echo. so I need to use -L then -e to do it if [[ -L ...
2
votes
1answer
2k views

IF ELSE string comparison from curl

When I do: URL_CURL_CHECK="https://github.com/" VAR_A=$(curl -Is --head "$URL_CURL_CHECK" | grep "HTTP/1.1 200 OK") VAR_B="HTTP/1.1 200 OK" echo if [ "$VAR_A" != "$VAR_B" ]; then echo "Not equal ...
0
votes
1answer
394 views

Accuracy of -newer test

It appears that the-newer test is not quite as accurate as I would like. Using ls -lt I get the correct sort order: -rw-r--r--@ 1 p staff 48 Jan 26 14:42 foo.gdb -rw-r--r--@ 1 p staff 328971 ...
40
votes
2answers
91k views

Bash test: what does “=~” do?

#!/bin/bash INT=-5 if [[ "$INT" =~ ^-?[0-9]+$ ]]; then echo "INT is an integer." else echo "INT is not an integer." >&2 exit 1 fi What does the leading ~ do in the starting regular ...
3
votes
0answers
651 views

How to reliably detect stdin pipe in Bash in batch mode?

I need a way to test whether input to my Bash script has been piped to stdin when the script is run in batch mode. The method I use when I run the script interactively (the test [ -p /dev/stdin ]) ...
4
votes
2answers
909 views

How to test list of proxy servers?

I have list of HTTP/HTTPS/Socks proxy servers like this: 1.2.3.4:80 5.6.7.8:8080 ... I need to test each server if it works and if it is anonymous or not. I plan to put these servers to my ...
5
votes
3answers
360 views

Why doesn't the conditional work when I use it directly without a variable?

This gives me an error that says too many arguments: if [ $( file -b $i ) == "directory" ] But when I tried this name=$( file -b $i ) if [ name == "directory" ] It seems to work just fine. Can ...
1
vote
1answer
112 views

trouble with Else statment in Bash

I have this simple code that creates .bak files when I clean, . bak files should be removed when I clean again and there is no . bak it should echo "there's no .bak" but it only executes the if ...
0
votes
2answers
60 views

Is there a unix scripting file checking Key?

Is there a key that explains theese letters such as -n, -d, etc all mean ? Example: if [[ -d ${directory_name} ]]; then ...; fi What is the -d or where do I find a key? Example: while [[ -n ${...
50
votes
5answers
8k views

How exactly does “/bin/[” work?

I am always surprised that in the folder /bin there is a [ program. Is this what is called when we are doing something like: if [ something ]? By calling the [ program explicitly in a shell it asks ...
1
vote
2answers
73 views

Please tell how this command checks whether rnum has an integer or not

I saw a construct like this in a shell script, being used to test if $rnum is a number: if ! [ "$rnum" -eq "$rnum" ] 2> /dev/null; then echo "rnum is not a number" exit 1 fi It appears to ...
1
vote
1answer
51 views

Bash: `test` finds file but `source` wont

I use virtualenvwrapper to manage my Python environments. My global postactivate file — a Bash script that gets sourced, after a virtual environment is activated — contains the following: if [ -f '...
27
votes
2answers
1k views

Error in shell bracket test when string is a left-parenthesis

I used to be confident about the fact that quoting strings is always a good practice in order to avoid having the shell parsing it. Then I came across this: $ x='(' $ [ "$x" = '1' -a "$y" = '1' ] ...
1
vote
4answers
2k views

Linux shell script check command success [duplicate]

I need to check the command executed is success or not, I have place the below code in test.sh run it, which give the out by listing the content of /home but there is a warning ./test.sh: 3: ./test....
0
votes
1answer
91 views

Equality check seems to always be true [duplicate]

I have this function in my .zshrc: activated=false function test_toggle() { if [ $activated=false ]; then echo "WAS FALSE" activated=true else echo "WAS TRUE" activated=false ...
2
votes
2answers
8k views

Check if there is a certain file in a directory

Hi I'm trying to do an if which gets a path to a directory as an argument and checks if file.txt is in the directory.And returns 1 if it is , 0 otherwise. if [ -e $1/file.txt ]; then ...
2
votes
3answers
409 views

awk system call with inverted effect

I have a data file with multiple data blocks enclosed between specific keywords (DATA,END). I am using awk to extract the data blocks into separate files, based on a file name taken from said block. ...
1
vote
3answers
232 views

[[ 0*10%300 ]] works on AIX 6.1 but not on AIX 7.1 (ksh)

I have a ksh93 script that I'm migrating from AIX 6.1 to AIX 7.1 It's failing on 7.1 but works fine on 6.1. Here's a snippet of the important parts. integer f_count=0 . . . . . . . . . ...
1
vote
2answers
16k views

How to compare strings in ksh

I want to check the result of a job and execute an action on FAILED. First: I grep the last word of the line in my application logfile (for the recent processed file ($processedfilename)): check1=$(...
0
votes
1answer
65 views

add a character before a line with matching pattern [duplicate]

I have an array that contains line number 1 line number 2 line number 3 line number 4 line number 5 Then I ask for the user input: read userInput echo $userInput And then I try to match the user ...
6
votes
2answers
6k views

Shell logical not

what is preferred between if ! [ ... ]; then and if [ ! ... ]; then actually they do the same result, is there a preferred syntax? in the former syntax the evaluated not is the shell builtin, ...
0
votes
1answer
332 views

Difference between `[[ $a -lt 2 ]]` and `(( $a < 2 ))`

Is there any significant difference between using [[ $a -lt 2 ]] and (( $a < 2 ))? For example, is one of them faster or more POSIX compliant than the other?
0
votes
1answer
141 views

How to find whether system is using systemd and glib specific version in shell script? [duplicate]

I am trying to identify system using systemd and glibc version 2.17 and then run specific set of code. This is what I have come out with but i get error ./testing.sh: line 4: [[UNIT: command not ...
1
vote
0answers
720 views

Test/Simulate SNMP traps on disk failures

On my monitored system I configure SNMP to sent traps in the event that hardware failures occur. For example, I add the following (rather well-known) line to my snmpd.conf file: monitor -60 -o ...
0
votes
1answer
3k views

If statement giving “else” response to both cmp results

I'm getting unexpected results from the following COMPARE(){ if [ ! cmp $IPPATH/ip.old $IPPATH/ip.new >/dev/null 2>&1 ]; then echo compare going to create CREATE ...