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
2
votes
3answers
702 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 the ...
2
votes
3answers
3k 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 ...
2
votes
3answers
77 views

Shortcutting in a loop

Shortcutting: (expr1 && expr2), rather than an if ... fi statement. If expr1, expr2,... are to be generated by a loop, can you do it? Here's a toy example: $ for cond in {0,0,0}; do if [[ $...
2
votes
1answer
1k 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
0answers
38 views

Testing whether an executable can be run on Linux? [duplicate]

In my Ubuntu packages, I install a hook with a one liner script: if test -x /usr/bin/snapsignal then /usr/bin/snapsignal "snapmanagerdaemon/DPKGUPDATE hook_action=\"$DPKG_HOOK_ACTION\"" fi (...
2
votes
1answer
323 views

Bash: Regex for comparing file path

I want to check if an input string matches whitelisted file paths, then only I want to perform some operation. I am doing something like this path1="mydir/**" path2="mydir/testing" [ [ "$path2" =~ "...
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 ...
2
votes
0answers
123 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 /home/xlwang/local/...
2
votes
2answers
647 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 is,...
1
vote
4answers
15k 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
1
vote
3answers
371 views

Alternative to Bash double bracket glob match

Given hasArgs=a:b:c: x=b if [[ ${hasArgs} = *$x:* ]] ; then … I wish I could replace the above double brackets test with a less Bash specific alternative (meaning no subshell/command etc.) ...
1
vote
2answers
4k 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?
1
vote
3answers
1k views

check whether the user has a password set for his account in a script

I'm writing a script and need to check $user if his account has a password set, how can I do this? I know about this: passwd --status username Display account status information. The status ...
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 && $...
1
vote
4answers
578 views

How to match “4.4” from `uname -r`?

Pseudocode [ `uname -r` =~ ^4\.4.*$ ] && echo "yes" which is, however, unsuccessful in all cases. The uname -r command gives things like 4.6.0-040600-generic as an output.
1
vote
2answers
183 views

How to understand this POSIX description about `(`, `)` and `test`?

From POSIX 2013: The XSI extensions specifying the -a and -o binary primaries and the '(' and ')' operators have been marked obsolescent. (Many expressions using them are ambiguously defined by the ...
1
vote
1answer
59 views

What does the -a unary operator check?

I have just found a couple of examples using [ -a some_dir ] or [ -a some_file ] but can't find what the -a operator is for. It seems it should be described in the man page for test/[ but there it is ...
1
vote
2answers
277 views

Similar test expressions using or operator?

Just wondering if this: if [ "$first_arg" == "major" ] || [ "$first_arg" == "minor" ]; then exit 1; fi is the same as this: if [ "$first_arg" == "major" || "$first_arg" == "minor" ]; then ...
1
vote
2answers
581 views

bash: [: missing `]' when the test stored in variable [duplicate]

Why this works: [ -r /tmp ] && echo "tt" >/tmp/taa cat taa tt But the following doesn't and how to fix that, preserving variable for reusability? COMD='[ -r /tmp ] && echo "tt"' $...
1
vote
2answers
87 views

variable printed the same value but actually value is diff

the follwing syntax used in order to capture the word between in xml file I also use xargs to remove any spaces var=` find /tmp -name '*.xml' -exec sed -n 's/<Name>\([^<]*\)<\/Name>/\...
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....
1
vote
4answers
7k 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 user@192....
1
vote
3answers
13k 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 ...
1
vote
1answer
49 views

Shell script to test ls output for directories

I am trying to make a BASH script to test the files in the working directory and return the files that are directory files. One approach that I can think of is, starting with ls and feeding the ...
1
vote
3answers
8k 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
3answers
536 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 -...
1
vote
2answers
18k 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=$(...
1
vote
1answer
432 views

Catch unexpected input on single character read in bash [duplicate]

I have a bash script, which, at one point asks the user for confirmation. I do this by reading a single character, which is then transformed to lower case and checked if it is 'y'. If not, the script ...
1
vote
1answer
4k views

Difference between '>' and '-gt'

What's the difference between these: SEC=$(mysql -hxxx -Pxxx -uxxx -pxxx -se "SELECT Seconds FROM tablename") SEC=100 Both return the same result (100). But when I test it with: if [ ${SEC} > 1 ...
1
vote
1answer
8k 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
1answer
2k views

-eq: unary operator expected, shell argument parsing

I am getting this error while comparing shell argument to a string. If the code look like this: online=true if [ "$2" -eq '-o' ] then online=false fi echo $online Then for e.g. input I am ...
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?...
1
vote
1answer
334 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 ...
1
vote
1answer
113 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 ...
1
vote
3answers
3k 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 ...
1
vote
2answers
2k 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: ...
1
vote
2answers
135 views

bash script get result from test into variable

I'm trying to learn Bash scripting and for fun I would like to write a script which will monitor the httpd.service. The basic construct looks like this: #!/bin/bash SERVICE=httpd.service if [ "...
1
vote
1answer
616 views

bash: test: no: integer expression expected [duplicate]

I believe this line if test "$suman_inspect" -eq "yes"; then is causing this error (verbatim): bash: test: no: integer expression expected I formulated the above test expression because I saw ...
1
vote
2answers
118 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
1answer
52 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 '...
1
vote
1answer
4k 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
1answer
54 views

How to confirm if pathname exists?

How do I test whether a pathname exists? Previously I was using: realpath -e /some/path Which works except if /some/path is a broken symlink, in which case it reports that it does not exist. [ -e /...
1
vote
1answer
78 views

Grep to check filesystem mount and test to check for file path is providing incorrect information

Even though the path exists, this script reports that it does not. What is the flaw in the logic? #!/bin/bash mount="/fileserver" if grep -qs "$mount" /proc/mount && { test -d '/fileserver/...
1
vote
2answers
217 views

Solaris script - modified date file age

I have a bash script on a Solaris server to provide an alert if a file is in a folder location for longer than 5 minutes. if [ -f $1 ] then a=0 else a=1 However, it is throwing a lot of false ...
1
vote
1answer
261 views

-w flag in bash if statement not working

I have a simple script that checks if a file has write permission on it or No. If yes, then it allows the user to append some text data to end of file. If No, it kicks user back out. I am following a ...
1
vote
1answer
531 views

Capturing return code with test []

If I run this: #!/usr/bin/env bash simple_return_zero(){ return 0; } simple_return_one(){ return 1; } if [ simple_return_zero ]; then echo "we have 0000"; fi if [ ! simple_return_zero ]; ...
1
vote
1answer
149 views

Error when compare big numbers

Trying to use this #!/bin/bash SIZE=$(redis-cli info | grep used_memory: | awk -F':' '{print $2}') MAX=19000000000 if [ "$SIZE" -gt "$MAX" ]; then echo 123 fi But always getting: "Ganzzahliger ...
1
vote
1answer
3k views

Bash Integer Comparison [duplicate]

While reading through a bash style guide I found the following guideline: Math / Integer Manipulation Use ((...)) and $((...)). a=5 b=4 # wrong if [[ $a -gt $b ]]; then ... fi # right ...
1
vote
1answer
1k views

Bash epoch date comparison not working [duplicate]

Need some help in fixing simple bash script below. What it does is to compare dates in epoch format using if else. The script does not work as I intended because it always goes to the first condition ...
1
vote
2answers
1k 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 ...