What do I miss (while-loop)? - bash

I try to built a Programm where I can create Text-Files and folders. The user can type in i.a. the directory. To check if the given path exits I created a while-loop but it doesn’t work.
My question is where are my errors and how can I fix them.
while [[ test -e $path == false]]
do echo “type in a valid path”
read -r path
done
Error:
./Create: line 28: conditional binary operator expected
./Create: line 28: syntax error near `e'
./Create: line 28: `while [[ test -e $path == false ]]'

I found a couple of issues:
You should use [[ or test, but not both
false is not a constant in bash
The fix:
while [[ ! -e $path ]]
do
....
done

Related

getting sh: =~: unknown operand in shell scripting

I just started doing shell scripts and getting unknown operand error while using regex in if statement. i searched google but did not get anything
IP="172.21.1.1"
if [[ "$IP" =~ /d ]] ; then
echo "qqq"
fi
Getting error as
sh: =~: unknown operand
Bash version is : BusyBox v1.19.3 (2012-01-31 08:57:52 PST) built-in shell (ash)
This is happening because the operator =~ doesn't existe for bash.
As see you are trying to use a Regex to compare your variables. I recommend to use the expr command. Here is an example:
IP="172.21.1.1"
if [[ $(expr match "$IP" 'my_regex') != 0 ]]; then echo "qqq"; fi;

Removing character from variable in BASH script

I have a script which utilises SaltStack's command-line as well as BASH commands. The script is used to gather data from multiple Linux servers (hence SaltStack), one of the checks which I would like to gather is disk space.
I have done this by using the following command:
salt $i cmd.run 'df -Ph / | tail -n1 | awk '"'"'{ print $4}'"'"'' | grep -v $i
$i = hostname and the use of the ugly '"'"' is so that my command can run via SaltStack as Salt's remote execution functionality requires single quotes around the command, if I left them in my command wouldn't run inside my BASH script.
Example syntax:
salt $hostname cmd.run 'command here'
After many questions on here and with colleagues I have this section of the script sorted. However I now the problem of stripping the output of my above command to remove the 'G' so that my script can compare the output with a threshold I have defined and turn the HTML which this script is piping to red.
Threshold:
diskspace_threshold=5
Command:
while read i ; do
diskspace=`salt $i cmd.run 'df -Ph / | tail -n1 | awk '"'"'{ print $4}'"'"'' | grep -v $i`
Validation check:
if [[ "${diskspace//G}" -lt $diskspace_threshold ]]; then
ckbgc="red"
fi
The method I have used for stripping the G works on the command line but not within my script so it must be something to do with the syntax or just the fact that it is now within a script. Any ideas/thoughts would be helpful.
Cheers!
EDIT: Here is the error message I receive when running my script:
serverdetails.sh: line 36: p
: 2.8: syntax error: invalid arithmetic operator (error token is ".8")
I assume the error is coming from here (is this line 36?)
if [[ "${diskspace//G}" -lt $diskspace_threshold ]]; then
Note the error message:
serverdetails.sh: line 36: p : 2.8: syntax error: invalid arithmetic operator (error token is ".8")
bash does not do floating point arithmetic
$ [[ 2.8 -lt 3 ]] && echo OK
bash: [[: 2.8: syntax error: invalid arithmetic operator (error token is ".8")
You'll need to do something like this:
result=$( bc <<< "${diskspace%G} < $diskspace_threshold" )
if [[ $result == 1 ]]; then
echo OK
else
echo Boo
fi

Script to match regex fails with a syntax error on a specific version of bash

I have a script to match a pattern, and if it matches, I used the match to append to a variable.
My script works on Bash v3.2.57 and fails on v4.3.30.
Could someone tell me what is wrong with the second ifcondition that matches a pattern here?
#!/bin/sh
if [ -f .file-to-read ]; then
while read p; do
echo "yes"
if [[ $p =~ "#user/"(.+)"#"[0-9]+"."[0-9]+"."[0-9]+ ]]
then
var="$var<#user/${BASH_REMATCH[1]}|$p>\\n"
fi
done < .file-to-read
fi
The error message is
Syntax error: "(" unexpected (expecting "then")
I figured out the issue. The problem was the file was being executed in #!/bin/sh when it should have been #!/bin/bash

shell script working fine on one server but not on another

the following script is working fine on one server but on the other it gives an error
#!/bin/bash
processLine(){
line="$#" # get the complete first line which is the complete script path
name_of_file=$(basename "$line" ".php") # seperate from the path the name of file excluding extension
ps aux | grep -v grep | grep -q "$line" || ( nohup php -f "$line" > /var/log/iphorex/$name_of_file.log & )
}
FILE=""
if [ "$1" == "" ]; then
FILE="/var/www/iphorex/live/infi_script.txt"
else
FILE="$1"
# make sure file exist and readable
if [ ! -f $FILE ]; then
echo "$FILE : does not exists. Script will terminate now."
exit 1
elif [ ! -r $FILE ]; then
echo "$FILE: can not be read. Script will terminate now."
exit 2
fi
fi
# read $FILE using the file descriptors
# $ifs is a shell variable. Varies from version to version. known as internal file seperator.
# Set loop separator to end of line
BACKUPIFS=$IFS
#use a temp. variable such that $ifs can be restored later.
IFS=$(echo -en "\n")
exec 3<&0
exec 0<"$FILE"
while read -r line
do
# use $line variable to process line in processLine() function
processLine $line
done
exec 0<&3
# restore $IFS which was used to determine what the field separators are
IFS=$BAKCUPIFS
exit 0
i am just trying to read a file containing path of various scripts and then checking whether those scripts are already running and if not running them. The file /var/www/iphorex/live/infi_script.txt is definitely present. I get the following error on my amazon server-
[: 24: unexpected operator
infinity.sh: 32: cannot open : No such file
Thanks for your helps in advance.
You should just initialize file with
FILE=${1:-/var/www/iphorex/live/infi_script.txt}
and then skip the existence check. If the file
does not exist or is not readable, the exec 0< will
fail with a reasonable error message (there's no point
in you trying to guess what the error message will be,
just let the shell report the error.)
I think the problem is that the shell on the failing server
does not like "==" in the equality test. (Many implementations
of test only accept one '=', but I thought even older bash
had a builtin that accepted two '==' so I might be way off base.)
I would simply eliminate your lines from FILE="" down to
the end of the existence check and replace them with the
assignment above, letting the shell's standard default
mechanism work for you.
Note that if you do eliminate the existence check, you'll want
to either add
set -e
near the top of the script, or add a check on the exec:
exec 0<"$FILE" || exit 1
so that the script does not continue if the file is not usable.
For bash (and ksh and others), you want [[ "$x" == "$y" ]] with double brackets. That uses the built-in expression handling. A single bracket calls out to the test executable which is probably barfing on the ==.
Also, you can use [[ -z "$x" ]] to test for zero-length strings, instead of comparing to the empty string. See "CONDITIONAL EXPRESSIONS" in your bash manual.

Why does this simple bash code give a syntax error?

I have the following bash code, which is copied and pasted from "bash cookbook" (1st edition):
#!/bin/bash
VERBOSE=0;
if [[ $1 =-v ]]
then
VERBOSE=1;
shift;
fi
When I run this (bash 4.0.33), I get the following syntax error:
./test.sh: line 4: conditional binary operator expected
./test.sh: line 4: syntax error near `=-v'
./test.sh: line 4: `if [[ $1 =-v ]]'
Is this as simple as a misprint in the bash cookbook, or is there a version incompatibility or something else here? What would the most obvious fix be? I've tried various combinations of changing the operator, but I'm not really familiar with bash scripting.
Bash uses spaces to tokenise scripts. The line:
if [[ $1 =-v ]]
should be:
if [[ $1 = -v ]]

Resources