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
3
votes
1answer
31k views

Why am I getting “too many arguments”

Here's a part of my script that is telling me that I have too many arguments on line 3: #!/bin/bash export LC_ALL='C' if [ `echo "*file 2" | grep -o ^.` = '.' ] then echo success fi Anybody know ...
3
votes
3answers
872 views

Why is my condition always false?

I tried to write a script which creates a user. First it needs to check if: root is running if user maybe already exists I Also tried to set the password equal to the username which was typed in. ...
3
votes
2answers
155 views

A Unix AIX variable without a $ symbol?

I'm trying to debug an existing program and found an if condition without $ symbol prefixed to it. Values are: dt_val=1234 prev_dt_val=1234 If condition goes like: if [ dt_val -eq prev_dt_val ] ...
3
votes
1answer
49 views

What's the difference between `-h` and `-L` in bash conditional expressions?

According to the man page: -h file True if file exists and is a symbolic link. ... -L file True if file exists and is a symbolic link. They both seem to ...
3
votes
2answers
412 views

What's the difference between 'test' and evaluating with /dev/null?

I noticed different .sh scripts for homebrew which check whether it's installed. One uses this syntax: if test ! $(which brew); then The other uses this syntax: if ! which brew > /dev/null; ...
3
votes
3answers
1k views

Why is my script calling multiple functions? [duplicate]

I have a few sets of scripts that I've written to make setting up linux very simple. So I have made those scripts into separate functions instead and put it all in one script. I have numbers set so ...
3
votes
2answers
63 views

Exit statuses of comparisons in test constructs

I was writing some "if then" statements and found what seemed to me an odd behavior. Upon investigation I realized that it boiled down to the exit code of the comparison I was making. I illustrate my ...
3
votes
1answer
759 views

Test multiple file conditions by combining flags—does it work?

My answer over at StackOverflow, which received at least two upvotes, is what worked on the shell of a workstation I used at my previous workplace. On that workstation, which is probably some old ...
3
votes
2answers
431 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 ...
3
votes
2answers
972 views

Comparing an string by value of a floating number to a number

I wrote the code below: for j in `cat output_10.txt` do dri=`echo $j |cut -d':' -f1` nu=`echo $j |cut -d':' -f2` tot=`echo $j |cut -d':' -f3` fre=`echo $j |cut -d':' -f4` if [ $fre > 2 ] ...
3
votes
1answer
189 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 ...
3
votes
1answer
733 views

( test -n $st ) != ( test -z $st ) right?

As I read, test -n $string ==> Exit status is 0 if $string is not null, 1 otherwise and test -z $string ==> Exit status is 0 if $string is null, 1 otherwise But in this particular example, (I ...
3
votes
0answers
710 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 ]) ...
3
votes
0answers
773 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 ...
2
votes
1answer
974 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 result, ...
2
votes
2answers
297 views

Pattern matching in a zsh conditional expression

I am having trouble with pattern matching in zsh's hook function precmd. I have the following: precmd(){ local x='test' if [ $x = '*test*' ]; then echo 'hello' fi } which does not ...
2
votes
2answers
1k 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 ...
2
votes
2answers
76 views

Why does test -e fail on a file in a directory with only read permissions?

From what I understand about the read permission on directories, it allows listing of which files are in a directory and that's about it. Given a directory with 0744 permissions, owned by userA: [...
2
votes
1answer
2k views

The meaning of [ $? == 1 ] && return 0

I need to understand this command: [ $? == 1 ] && return 0
2
votes
2answers
299 views

What's wrong with my file check script?

I don't understand why the last part of my script does not display anything when no file or directory is found. echo -n "Please enter a filename or directory: " read filename if [ -z "$filename" ]; ...
2
votes
2answers
380 views

How to write a function in bash?

I am drawing blank here. How should I have written this script? #!/bin/sh XZY_NODES="\ vl-ocnt-1 \ vl-ocnt-2 \ vl-onet-1 \ vl-onet-2 \ vl-ocmp-1 \ vl-oomp-2 \ " is_xyz_node () { host=$1 ...
2
votes
2answers
17k views

bash: syntax error near unexpected token `}'

I tried using this: $ if [$a == 1] then { echo 'yes'; } fi; but I get an error: -bash: syntax error near unexpected token `}' What is the correct format? I tried several with no luck.
2
votes
1answer
1k views

Compare command output inside if statement without subshell

According to https://www.gnu.org/software/bash/manual/bashref.html#Command-Grouping Placing a list of commands between curly braces causes the list to be executed in the current shell context. But ...
2
votes
2answers
174 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 ...
2
votes
3answers
377 views

Why is my elif being treated as an else statement in my bash script?

I'm building a bash script that uses wget to GET information from a server using a REST api. I'm using getopts to parse options given to the script and then using an if statement to redirect the ...
2
votes
3answers
465 views

Display files to size with lower and upper bound without using `find`

I need to create a program via vim in Linux called between. The script takes two arguments as the lower bound and upper bound of the file size, and prints out a list of file names as well as their ...
2
votes
1answer
3k views

How to use negation of a command in a while loop with grep in shell script?

Is there any way to use while loop and grep all together? See my example: while [[ !(grep -R -h "${text}" ${path}) ]]; do ... done It says: ./test_script.sh: line 1: conditional binary ...
2
votes
1answer
1k views

Expand shell variable without globbing

I want to check whether an input string refers to a file name - not a wildcard string like *.txt. This doesn't work: if [ -f "$1" ]; Because $1 gets expanded to *.txt, which gets expanded to, say ...
2
votes
1answer
104 views

How to determine if result of process substitution is a file path

If I do this: echo <(cat) I get: /dev/fd/63 so say at the command line I have: myapp -f <(cat) when I run it I get this error: You need to pass a file after the -f flag. The resolved ...
2
votes
3answers
216 views

“==” logical operator and zsh version 5.7.x (installed using Homebrew)

Been getting errors on my scripts when using this operator. I've some online documentation and double equals should supposedly work. Any ideas? Zsh reference guide: http://zsh.sourceforge.net/Doc/...
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 ...
2
votes
3answers
436 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. ...
2
votes
1answer
887 views

I want to compare two list and print the difference output from list one

I have two variables input: VAR1="abc red blue cat empty dummy rummy" VAR2="rummy zero empty rat cat reverse" output: I want output as follows: (remove the common ones from $VAR2 in $VAR1) abc red ...
2
votes
2answers
793 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 ...
2
votes
1answer
87 views

Running a Test with expression in a string

I am trying to run the test command from a string. The string contains the expression. TEST="! -e ~/bin/xyz" if [ `echo "$TEST"` ]; then echo running "$TEST"; fi However, the above if ...
2
votes
1answer
441 views

How to check if there are “other” permissions in home directories

Right now, I have a script that runs daily that chmod's the home directories, removing all "other" permissions from the directories and the "group" write permission. See below. #Removing all other ...
2
votes
1answer
498 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 ...
2
votes
2answers
6k views

How to assign value to input variable in shell

I am asking user for input and taking input in the variables such as $1, $2 etc. I am checking the variable for null value and wants to replace the value if null. if [ "$2" == "" ]; then 2=value fi ...
2
votes
3answers
870 views

Proper type casting in a shell script for use with while loop and modulus

I am trying to write a script to get a random, even hex number. I have found the the openssl command has a convenient option for creating random hex numbers. Unfortunately, I need it to be even and my ...
2
votes
1answer
2k views

integer expression expected

I have the following script that is trying to match and IP address with a value in a file #!/usr/bin/env bash #To find the IP via ping ping=$(ping federicolivieri.noip.me -c 1) #To cut the result ip=...
2
votes
3answers
533 views

Rename only one file in each directory, based on condition

I have 100 folders inside every folder I have one or two files named as the following: XXX_001_014_max.jpg XXX_001_024_max.jpg I saved the folders names in a file "list.txt" I ran the following ...
2
votes
2answers
4k 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
235 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 "...
2
votes
2answers
940 views

Nested read fails

I have a list of filenames in a file and want to do let the user decide what to do with each. In bash, iterating over filenames is not trivial in itself, so I followed this answer: #!/bin/bash while ...
2
votes
1answer
361 views

How to check for presence of named pipe on the file system [duplicate]

I tried using the -f flag to test if a named pipe is present if [[ ! -f "$fifo" ]]; then echo 'There should be a fifo.lock file in the dir.' > /dev/stderr return 0; fi this check does not ...
2
votes
1answer
365 views

Dash “-nt” behavior

The use case I can think of for -nt, would be updating a file if it is too old, or if it does not exist. Bash handles this just fine: $ bash -c 'touch 2.txt; sleep 1; touch 1.txt; [ 1.txt -nt 2.txt ];...
2
votes
2answers
9k 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
2answers
3k 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 ...
2
votes
2answers
11k views

“If variable does not contain” not working

This is my script if [[ ! $url == *.txt ]] then exit fi I have also tried: if [[ ! "$url" == *.txt ]] then exit fi and: if [[ "$url" !== *.txt ]] then exit fi But even though $url does contain *....
2
votes
2answers
2k views

Bash script: test command -o option

I have a piece of code like this: count=0 until [ -f $filename -o $count -ge 60 ] do count=$((count+1)) sleep 1 done I know [ -f $filename -o $count -ge 60 ] is a test command, "-f $filename" ...