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
0
votes
2answers
386 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 ...
0
votes
2answers
1k views

Preform operation in bash only if a variable is less than a second variable [closed]

I am building an OS for a virtual machine I made, and while progress has been coming along swimmingly, I have been stuck for a good several hours on what should be a very simple issue: I am designing ...
0
votes
1answer
39 views

negative values are being treated as positive in RHEL suddenly. Why?

For example: # [[ -24 > 0 ]] && echo "Hi" Hi Since yesterday, I found this weird behavior. What could be the reason?
0
votes
2answers
2k views

Test bash if conditions in the terminal [closed]

I'm developing a bash script and am spending most of my time on debugging the if condition statements. The reference states: There exists a dedicated command called [ (left bracket special ...
0
votes
1answer
8k 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
1k 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
2answers
85 views

Bash: Globbing and file conditions [duplicate]

I am trying to check wether a file exists or not: This works as expected: if [ -e "/tmp/test.*.lock" ]; then echo "exists!" fi This does not: if [[ -e /tmp/test.*.lock ]]; then echo "exists!" ...
0
votes
1answer
101 views

if statement not working inside while loop

I don't understand why comparing strings is not working here: while IFS=, read -r ELMS_SIZE ELMS_NAME ELMS_ENV ELMS_DB do if [ "${ELMS_DB}" = "NOMATCH" ]; then echo "done" fi done < <(tac ${...
0
votes
1answer
47 views

Empty test handling

Am learning bash and it's confusing how (why) this line of code if [$1 = ]; then works. Can someone please explain. From bash tuturial here under File re-namer (6th block) # a quick check to see if ...
0
votes
2answers
68 views

two variables pattern check at once

Is it possible to check if two variables match regex pattern at once and not one by one? Here is how it works now: # A=1.1 # B=1.2 # [[ $A =~ ^([0-9]+(\.([0-9]+(\.([0-9]+))?))?)$ && $B =~ ^([...
0
votes
2answers
2k views

bash + compare variable with spaces

I want to print the firewall-cmd --state status in variable - $STAT and then compare the variable $STAT with "not running" string but Comparison get false , it is because spaces inside the variable ...
0
votes
1answer
37 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" ...
0
votes
2answers
875 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 : ...
0
votes
1answer
434 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 ...
0
votes
1answer
72 views

Pattern matching on if block

I'm trying to match a pattern stored in a variable through an if block. As per my understanding, this should be a match and get "Match" echo statement. However, it doesn't seems to be matching. ...
0
votes
1answer
15 views

bash script for checking to see .AWK extension

if [ ! $var = " *.awk " ] ; then echo "$var contains awk file" fi this is what I have but when I run it and put a file name without .awk it still says contains awk file
0
votes
1answer
65 views

Bash * directory wildcard not working in if statement using double brackets

This works: if [ /a/*/b/file.asd -nt /c/d/file.asd ]; then echo "found new file"; fi This doesn't work: if [[ /a/*/b/file.asd -nt /c/d/file.asd ]]; then echo "found new file"; fi Can someone ...
0
votes
1answer
66 views

Bash script to compare ASCII values [closed]

what is the wrong? #!/usr/local/bin/bash REF_FILE_NUM="000000010000001900000052" for id in `ls -1 dir` ; do FILE_NUM=${id:0:24} echo "FILE_NUM: $FILE_NUM ; REF_FILE_NUM: $REF_FILE_NUM" if [[ "$...
0
votes
1answer
257 views

How to make nested ifs [closed]

I'm trying to make my own command using a script but I'm having a bit of doubt about the proper way to create an if inside another one. The code below shows how I'm trying to do it, but I guess it's ...
0
votes
1answer
95 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 ...
0
votes
1answer
72 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 ...
0
votes
1answer
152 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 ...
0
votes
1answer
49 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 ...
0
votes
1answer
793 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 ...
0
votes
2answers
62 views

Can't get if statement to run correctly [duplicate]

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 inputs Y or N, only "Whatever you say" appears on the screen. #!/bin/...
0
votes
1answer
43 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 \""$inputline"...
0
votes
1answer
674 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 "...
0
votes
1answer
518 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 ]]
0
votes
1answer
862 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 ...
0
votes
0answers
30 views

Solaris - modified date file age [duplicate]

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. It seems the -mmin command doesn't work in Solaris. if [ -f $1 -mmin +15 ] ...
0
votes
2answers
66 views

auto install minecraft script

So I was working on this script to auto install minecraft and i began to wonder is there a better way to do this? #!/bin/bash sudo apt-get upgrade sudo apt-get update sudo apt-get autoclean echo " ...
0
votes
0answers
19 views

Command substitution, assignment, tests and spaces [duplicate]

I know the whole language is confusing, but I am having trouble understanding assignments with command substitutions: x=Example sentence #1 fails due to space x='Example sentence' ...
0
votes
0answers
116 views

-d foo-* within single and double square brackets [duplicate]

I just discovered that in bash,[[ -d foo-* ]] returns false ($? set to 1) even if a directory named foo-bar exists while [ -d foo-* ] returns true ($? set to 0) I have to admit that I was surprised ...
0
votes
1answer
39 views

Shortening a long if/test string comparison

I am comparing multiple strings in bash. Currently I have a script that looks like this: if [ "$f" != "A-C" ] && [ "$f" != "D-F" ] && [ "$f" != "G-I" ] && [ "$f" != "J-L" ]...
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
2answers
111 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 "...
0
votes
1answer
527 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
2answers
985 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 ...
-1
votes
1answer
101 views

Bash: How to escape minus in `if [ … ` (alias “test”)?

I need to test this: kill -0 $LAST_PID; So I wrote if [ kill -0 $LAST_PID ] ; then ... But this results in an error - unknown operator after - or so. how do I escape the options within if?
-1
votes
1answer
31 views

why does test command startswith fail (exit with 1)?

I have this test command: test "ssh://hg@bitbucket.org/teros/moodwire_demo" == ssh*; echo $? why does it exit with 1? I am trying to see if the first string starts with the second string.
-1
votes
2answers
112 views

How to test file permissions using shell script?

I am testing if my file has read permissions, by running this script as root: #!/usr/bin/env bash FILE="/etc/shadow" if ! [ -r $FILE ]; then echo "Hello" fi exit 0 Ideally, script has to print ...
-1
votes
1answer
104 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 ...
-1
votes
1answer
32 views

-n Vs !(exclamation mark) behaves differently with test command

#!/bin/bash declare -A numMap numMap[1]=1 #case-one if ! [[ ${numMap[1]} ]];then echo "case-one: the key 1 for numMap array is not set" fi #case-two if [[ -n ${numMap[1]} ]]; then echo "case-two: ...
-1
votes
1answer
23 views

test for existing directory passes for directory that does not exist

Action I have set an environment variable in a user profile export DATADIR=<absolute path set with `readlink -e target`> Expected outcomes After sourcing the profile file again, I checked ...
-1
votes
1answer
269 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
2answers
53 views

Need to delete logs from different locations using shell script

Need to delete logs with different name from different locations and need to delete the logs more than 5 days. Ex: /abc/bcd/fgh/log/log1.txt /abc/bcd/fgh/test/log2.log /test/urc/mhg/event.log /hjy/...
-2
votes
1answer
2k views

Redirect error message to /dev/null in if condition [closed]

I have the following if statement: if [ "$sourcelast" = "$maxhostlast" ] || [ "$sourcelast" = "$minhostlast" ] || [ "$sourcelast" < "$maxhostlast" ] || [ "$sourcelast" > "$minhostlast" ] ;then ....
-2
votes
1answer
285 views

string comparison not working with sqlplus output

Code: iIMSI=`sqlplus -s ${APP_ORA_USER}/${APP_ORA_PASS}@${APP_DB_INST} << EOF SET HEAD OFF PAGESIZE 0 LINESIZE 1000 TRIMOUT ON TIMING OFF select resource_value from agd1_resources where ...
-3
votes
3answers
521 views

How to compare “abc” with abc in a shell script?

My code need compare "stop" with stop this is stand bash string. pi@raspberrypi:~/Voice $ ./test.sh | more "stop" stop My code: #!/bin/bash command=stop while : do QUESTION=$(cat stt.txt) #stt,...
-3
votes
4answers
9k views

-eq: unary operator expected [closed]

x=$? if [ ${x} -eq '0' ]; then echo "something missing" exit 1 else echo "all present" fi Why I am getting -eq: unary operator expected when executing above? A more complete version of what I am ...