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.
0
votes
1answer
27 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
16 views
linux /dev/root does not exist after custom ISO build of CentOS 7
I am building a custom ISO for CentOS 7 and for now I am just intending for this to be an absolute minimal install (a proof of concept basically).
I am re-creating the ISO via using mkisofs, the only ...
0
votes
2answers
50 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 ...
2
votes
2answers
70 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 ...
3
votes
1answer
50 views
How to use user input as a while loop condition
I can do this in bash:
while read -n1 -r -p "choose [y]es|[n]o"
do
if [[ $REPLY == q ]];
then
break;
else
#whatever
fi
done
which works but seems a bit redundant, ...
1
vote
1answer
63 views
How do I test if a file exists using ZSH? [duplicate]
This question is about ZSH, not bash.
I have the following lines in my .zshrc file. Whenever I open a terminal I get a no matches found error referencing the line with the if statement.
if [[!( -a ...
6
votes
3answers
181 views
What does `[ EXPRESSION ], [ ] and [OPTION` mean in `man test`?
Examining man test, I see that under synopsis for test are the possibilities test EXPRESSION and test.
What does [ EXPRESSION ], [ ] and [OPTION mean below ?
Why are the brackets empty and why does ...
0
votes
4answers
78 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 ...
-2
votes
3answers
108 views
Why the builtin `test` fail with 'less than' (<) and 'greater than' (>)?
This code:
#!/bin/bash
if [ "4" = "a" ]; then echo "equal"; fi
if [ "4" > "a" ]; then echo "yes 1"; fi
if [ "4" < "a" ]; then echo "yes 2"; fi
echo "exit"
prints this three lines:
yes 1
yes ...
3
votes
1answer
55 views
Bash test on successful subshell command returns false
I don't quite understand this behaviour of bash test operator. I used this line to determine if vagrant is installed.
if [ $(vagrant --version > /dev/null) ]; then echo "HELLO"; fi
I now know ...
2
votes
1answer
101 views
arithmetic syntax error in string compare
I have written a shell script which takes file name as parameter e.g user/test.txt. I want to make this file parameter optional if user does not wish to provide file name he/she can give as "None"
...
0
votes
1answer
41 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 ...
1
vote
2answers
262 views
What does [[ -t 1 ]] do?
To my understanding, the script below runs if the user is not root by comparing $EUID and 0. Then, it uses [[ -t 1 ]] to decide if the script is running in a terminal or not. If it is, it will use ...
3
votes
1answer
41 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 ...
0
votes
1answer
59 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 ...
6
votes
3answers
160 views
Peculiar Solaris integer comparison behaviour
This one caught me unawares!
We have a Solaris machine (version: SunOS 5.8) hosting a pivotal script/process which has been running for many years without complaint. We recently had cause to have a ...
1
vote
3answers
195 views
String comparison in single brackets in zsh [duplicate]
Bash code to print all folders:
for f in ~/*;
do
if [ $f == '/home/sk/.' -o $f == '/home/sk/..' ]; then
true
else
echo "$f"
fi
done
It works on bash. When i ran the code on z ...
1
vote
2answers
562 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
...
0
votes
2answers
45 views
Can't get if statement to run correctly. Beginner. [duplicate]
I am very new to bash, just playing around in my free time off work. 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 ...
1
vote
2answers
118 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 ...
0
votes
0answers
38 views
How do I write a script for testing username/password? [duplicate]
I have about 500+ hosts I need to test a username/password combo to make sure there aren't any false positives in the list. I just need to make sure I can login and log out, no need to run anything ...
1
vote
1answer
123 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
85 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
2answers
55 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 ...
0
votes
0answers
19 views
Bash Scripts arguments and G++ static and dynamic library generation [duplicate]
I have a library named matrix.cpp that is used by a program named test.cpp.
Now, I want to write a BASH script which takes matrix.h and matrix.cpp as input and generates matrix.a and matrix.so
...
1
vote
1answer
102 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
91 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 ]]
...
4
votes
5answers
367 views
Script tries to create files even though it shouldn't have to?
I have a folder which contains some folders, these folder are moved very often so I made a script to see if they exist, and if not then create them. This is what I did to (which I though would) ...
15
votes
2answers
455 views
Bracket expression (without ranges) matching unexpected character in bash
I'm using bash on Linux. I am getting a success from the following if statement, but shouldn't this return a fail code?
if [[ ■ = [⅕⅖⅗] ]] ; then echo yes ; fi
The square does NOT equal any of the ...
0
votes
1answer
27 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 ...
3
votes
1answer
61 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 ...
1
vote
1answer
83 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 ] && ...
0
votes
1answer
871 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
0answers
98 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 ...
1
vote
3answers
305 views
How do I make an output for grep fail?
I am using if-else statement to search for keywords and displaying the results in the terminal, here's an example of my code.
read finding
if ["$finding" != "" ]; then
grep $finding ...
1
vote
2answers
446 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?
0
votes
1answer
76 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
0answers
27 views
ksh if/elif statement not working with || and && [duplicate]
NOTE: Pertaining to the duplicate referenced - I already tried that prior to posting here and got the exact same results. I've posted an additional section below with this so that you can see what I ...
2
votes
2answers
841 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 ...
1
vote
3answers
294 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 ...
2
votes
3answers
1k views
Bash File existence test is always true
I have the following lines of bash script to test whether a file exists:
MYAPPPATH=$(find $APPDIR -iname myapp* -print)
if [ -e $MYAPPPATH ]
then
echo "File exists"
fi
However when file ...
3
votes
3answers
259 views
Why is my script calling multiple functions? [duplicate]
I am new to writing scripts in bash. My script is simple, 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 ...
2
votes
1answer
102 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
4answers
202 views
unix test when to use eq vs = vs == in test commands?
When should I use -eq vs = vs ==
e.g.
[[ $num -eq 0 ]]
[[ $num = 'zzz' ]]
I've observed a pattern of using -eq (and -ne, etc.) for numbers and = for strings. Is there a reason for this and ...
35
votes
7answers
2k views
Is test or [ or [[ more portable both between bash shells and between other shells?
I see I can do
$ [ -w /home/durrantm ] && echo "writable"
writable
or
$ test -w /home/durrantm && echo "writable"
writable
or
$ [[ -w /home/durrantm ]] && echo ...
1
vote
0answers
18 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 ...
2
votes
1answer
2k 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 ...
1
vote
1answer
9k views
Bash if statement [: missing `]' error [duplicate]
I am having trouble with bash. I am trying to put a command in an if statement and then compare it to a string.
This works perfectly.
echo $(ipcs | grep Shared | awk '{print $2}')
When I put it in ...
1
vote
2answers
133 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:
...
0
votes
2answers
91 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" ...