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
1
vote
1answer
758 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
33 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.
1
vote
1answer
115 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 ...
1
vote
3answers
126 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 ...
1
vote
1answer
69 views

Get a boolean from test expression

I have this bash code: local r2g_keep_temp=$(r2g_match_arg "--keep" "${my_args[@]}"); local r2g_multi_temp=$(r2g_match_arg "--multi" "${my_args[@]}"); local r2g_multi=[ "$r2g_multi_temp" || ...
1
vote
1answer
158 views

What does the capital -N option in an if statement mean?

man test only explains what -n means, wit a lowercase n. How does the capital -N work in this script? #!/bin/bash # Check for an altered certificate (means there was a renew) if [[ -N '/etc/...
1
vote
1answer
621 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 ...
1
vote
3answers
236 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
1answer
133 views

Difference in conditions /test

Lets say i have to test if file contains a string "mail" , what is the difference between if grep -q "mail" "file"; then ... if [ $( grep -l "mail" "file") ]; then ... [ grep -l "mail" "file" ] &...
1
vote
1answer
292 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
349 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 ...
1
vote
3answers
893 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 ]] ...
1
vote
1answer
1k 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 ] && ...
1
vote
0answers
97 views

What is the difference between test, [, and [[ in terms of binary vs. builtin? [duplicate]

It is often written: /usr/bin/test is a synonym for /usr/bin/[ [[ is a bash improvement for [ test and [ are also builtins I understand the difference between [ and [[ but I am confused with the ...
1
vote
0answers
24 views

bc command unexpected results [duplicate]

I am trying to compare a decimal and it seems like bc is ignoring it. The OS is CentOS release 6.9 (Final) and bash is bash-4.1.2-48.el6.x86_64, below is the code #!/bin/sh i=32.39 IP_LOW_2=32.0 ...
1
vote
0answers
2k views

Why use a variable in a bash regex match inside [[? [duplicate]

There is not a simple answer to this question at this site yet. This question aims to give a simple and clear answer. It is usually recommended to use a variable ($regex) in this construct: if [[ $...
1
vote
1answer
63 views

Is it possible to monitor each cell memory for a specific process

Saying that I have a very simple C program test.c, which just prints "hello world", its name is a.out coming from gcc test.c. I'm thinking if it's possible to monitor exactly what happened while ...
1
vote
0answers
731 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 ...
1
vote
0answers
57 views

problems with running tcsh file

I can't get the following program to run. When I try running it, it says set goal = `head -c 15 < /dev/urandom | base64 | sed 's/[^0-9]//g' | sed' Usage: tcsh [ -bcdefilmnqstvVxX ] [ argument ... ]...
1
vote
0answers
55 views

Conditional expressions in shell: What are the differences between [ vs [[ [duplicate]

I just wondered about some people writing if [[ <expr> ]]; then ... in answers here when I out of habit would use if [ <expr> ]; then ... Doing a quick research reveals that [[ ]] ...
1
vote
2answers
1k 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
0answers
20 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 ...
1
vote
0answers
38 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 ...
1
vote
2answers
77 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 ...
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 $...
0
votes
3answers
70 views

I have if else question but I am not sure the answer is true? [duplicate]

I am running this on Linux: $ number=4 $ test $number -eq 4 $ echo $? 0 $ test $number -lt 2 $ echo $? 1 Are the results (0 and 1) true ?
0
votes
2answers
4k views

Determining if the first string starts with second string

JavaScript has a function for this: 'world'.startsWith('w') true How can I test this with shell? I have this code: if [ world = w ] then echo true else echo false fi but it fails because it is ...
0
votes
4answers
294 views

Count number of files that are not symbolic links in output from “locate”

I'm trying to count the number of non-symbolic linked files passed by the locate command. I've tried a number of options and think this is the most promising: locate -r "$PWD.*\.c$" | xargs -0 -I{} ...
0
votes
3answers
3k views

How to check multiple conditons in a IF statement

I am trying to test for multiple conditions in the following if statement: If [[[ "$var1" = "$var2" || "$var1" = "$var3" || "$var1" = "$var4" ]]]; However, when I execute the above mentioned syntax ...
0
votes
1answer
53 views

Why does -n appear in my bash test [[ condition debug output?

I would like to test if a condition is true or false. Here is a sample bash script: #!/bin/bash set -x var=foo if [[ $var==bar ]] then echo $var is bar else echo $var is not bar fi ...
0
votes
3answers
102 views

I would like to check if a file name matches a certain pattern

I would like to check if a file name matches a certain pattern start with an alphanumeric character ([a-zA-Z0-9]) and will only contain alpha-numeric characters plus '.', '-' and '_' characters. ...
0
votes
2answers
51 views

Print all subdirectories in the current directory that do not contain a .git

I'm trying to write a bash line that will look at all the subdirectories in my current folder and tell me if any do NOT contain a ".git". Pseudo: for subdir in currentdir if .git does not exist ...
0
votes
1answer
140 views

Shorten if statement check for matching argument

I have this bit of code which does what it's supposed to do: first_arg="$1"; if [ "$first_arg" != "patch" -a "$first_arg" != "major" -a "$first_arg" != "minor" -a "$first_arg" != "prerelease" ]; then ...
0
votes
2answers
2k views

RegExp - Optional Capture group in Bash?

Currently working on some RegExp to parse an input file for correct content. I'm using the below RegExp to parse some input: cell-(90|855|80|70)-(DEV|DEVL|SANDP|CAT|(SIT[a-z]|SIT[1-9])|TAT|PROD)(?:-(...
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 ...
0
votes
1answer
82 views

Why does “if tty -s” work but “if [ tty -s ]” does not?

Note, I am using ksh and running in CentOS. I have a shell script where a logger function is set like: if tty -s then echo "${FOO}" | tee -a ${BAR_LOG_FILE} else echo "${FOO}" >> ${...
0
votes
2answers
162 views

java -version In Bash Script. Giving integer error

I am trying to make this if..fi case work for the Java Version from wrapper.conf configuration file./ and from the system, If the output from wrapper.conf is java I would have two options; 1. ...
0
votes
1answer
2k views

Empty string is a file? ( if [ ! -f “” ] )

The script is called isFile.sh and looks like this: #!/bin/sh echo $1 echo $2 if [ ! -f $1 ]; then echo "$1 (arg1) is not a file" fi if [ ! -f $2 ]; then echo "$2 (arg2) is not a file" fi ...
0
votes
1answer
249 views

Bash - issue with grep conditional statement

Why does this code work correctly, while the other version of the same condition doesn't? if grep -q string file; then echo found else echo not found fi This doesn't work: if [ ! `grep -q ...
0
votes
3answers
6k 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
3answers
2k 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 ...
0
votes
1answer
343 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
4k 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
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
2answers
61 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 ${...
0
votes
1answer
1k views

[: =: unary operator expected [duplicate]

I need to write a script to test if a service inside of an Android emulator is ready for apk installation or not using this command adb -s emulator-5554 shell pm list package | grep package:com....
0
votes
1answer
99 views

If statement works with [ but not with [[ when comparing strings with -eq [duplicate]

My code: #!/bin/bash x=${#} for (( a=0; a<x; ++a)); do if [[ $1 -eq $1 ]] 2> /dev/null then variable[a]=${1} shift else echo "The argument $1 is NOT an integer. ...
0
votes
1answer
1k views

Ksh script no parent error

I want the script to open a file and read the file line by line then count the number of commas per row/line. If that is more than the $2 parameter value, then write the offending line number (from ...
0
votes
2answers
978 views

Bash: test strings including dollar sign

Can someone explain the strange behavior of dollar sign $ in test command? [ "$a" != "$b" ] echo $? 1 [ "$a" != "b" ] echo $? 0 Why test "$a" != "$b" returns ONE instead of a ZERO?
0
votes
2answers
365 views

test (AIX & ksh93)

I'm confused on one of the ksh93 test qualifiers that we use. Twenty+ years ago we took a "Unix Shell Programming - featuring the KornShell" course. In the manual that accompanied that course it ...