This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 6 years ago.
I'm having an issue with a script that I am trying to program. Narrowed down and simplified code and it gives an error that command is not found. If i do "test -f file" in command line it returns nothing, not command not found
PATH=$1
#!/bin/bash
DIR=$1
if [[-f $PATH]]; then
echo expression evaluated as true
else
echo expression evaluated as false
fi
exit
Here is the actual more complicated script I'm trying to run
verify()
{
if [[-f $1]]; then
VFY[$2]="f"
echo "$1 is a file"
elif [[-d $1]]
then
VFY[$2]="d"
echo "$1 is a directory"
else
VFY[$2]=0
echo -e "\r"
echo "$1 is neither a file or a directory"
echo -e "\r"
fi
}
Its part of a larger script that can move things around depending on inputs. I've run this in CentOS 6, and FreeBSD, both give the same error "[[-f: Command not found"
Simply add an extra space between [[ and -f, and also before ]].
You will get:
#! /bin/bash
DIR=${1-} # unused in your example
if [[ -f test.sh ]]; then
echo "expression evaluated as true"
else
echo "expression evaluated as false"
fi
exit
and for your function
verify() # file ind
{
local file=$1 ind=$2
if [[ -f "$file" ]]; then
VFY[ind]="f" # no need of $ for ind
echo "$file is a file"
elif [[ -d "$file" ]]; then
VFY[ind]="d"
echo "$file is a directory"
else
VFY[ind]=0
echo -e "\n$file is neither a file or a directory\n"
fi
}
Related
This question already has answers here:
How to check if a string contains a substring in Bash
(29 answers)
Closed 3 years ago.
Output is same, and it always echos need to pull.
If I remove the quotes around $text in if condition it throws the too many arguments error.
var="$(git status -uno)" &&
text="On branch master Your branch is up-to-date with 'origin/master'. nothing to commit (use -u to show untracked files)";
echo $var;
echo $text;
if [ "$var" = "$text" ]; then
echo "Up-to-date"
else
echo "need to pull"
fi
Better do this, =~ for bash regex :
#!/bin/bash
var="$(git status -uno)"
if [[ $var =~ "nothing to commit" ]]; then
echo "Up-to-date"
else
echo "need to pull"
fi
or
#!/bin/bash
var="$(git status -uno)"
if [[ $var == *nothing\ to\ commit* ]]; then
echo "Up-to-date"
else
echo "need to pull"
fi
Warning: bash's regex require more ressources and won't work in other shell!
Simple old fashion
This syntax is POSIX compatible, not bash only!
if LANG=C git status -uno | grep -q up-to-date ; then
echo "Nothing to do"
else
echo "Need to upgrade"
fi
Or testing a variable (posix too)
From this answer to How to check if a string contains a substring in Bash, here is a compatible syntax, working under any standard POSIX shell:
#!/bin/sh
stringContain() { [ -z "${2##*$1*}" ] && { [ -z "$1" ] || [ -n "$2" ] ;} ; }
var=$(git status -uno)
if stringContain "up-to-date" "$var" ;then
echo "Up-to-date"
# Don't do anything
else
echo "need to pull"
# Ask for upgrade, see:
fi
This question already has answers here:
Test if a command outputs an empty string
(13 answers)
Closed 4 years ago.
Some Example:
I've a shellscript where I want to check if a the stdout of a command is empty.
So I can do
if [[ $( whateverbin | wc -c) == 0 ]] ; then
echo no content
fi
But is there no direct command, to check this? Something like :
if whateverbin | checkifstdinisempty ; then
echo no content
fi
You could use the -z conditional expression to test if a string is empty:
if [[ -z $(ls) ]]; then echo "ls returned nothing"; fi
When you run it on an empty result, the branch gets executed:
if [[ -z $(cat non-existing-file) ]]; then echo "there was no result"; fi
Just try to read exactly one character; on no input, read will fail.
if ! whateverbin | IFS= read -n 1; then
echo "No output"
fi
If read fails, the entire pipeline fails, and the ! negates the non-zero exit status so that the entire condition succeeds.
[[ `echo` ]] && echo output found || echo no output
--> no output
[[ `echo something` ]] && echo output found || echo no output
--> output found
With if :
if [ `echo` ] ; then echo ouput found; else echo no output; fi
This question already has answers here:
How do you tell if a string contains another string in POSIX sh?
(12 answers)
Closed 7 years ago.
Any idea what is wrong with this script? I am trying to check to see if the username is part of the present working directory file path. I have included two different implementations of the same thing, and neither of them work.
#!/bin/bash
USER_NAME=$(whoami)
if [[ "$USER_NAME" == *"$PWD"* ]]; then
echo $PWD
else
echo "not found"
fi
case "$USER_NAME" in
*$PWD*) echo $PWD ;;
esac
Also, this does not work either:
grep "$USER_NAME" "$PWD"
However, this works:
echo "$PWD" | grep "$USER_NAME"
Any idea what is going on here?
EDIT: This does not work either:
if [[ *"$PWD"* == "$USER_NAME" ]]; then
echo "found"
else
echo "not found"
fi
or even this:
if [[ $PWD == $USER_NAME ]]; then
echo "found"
else
echo "not found"
fi
or any other combination of * or quotes that I have tried so far.
EDIT2:
finally got it, have to use this:
if [[ $PWD == *$USER_NAME* ]]; then
echo "found"
else
echo "not found"
fi
There are two things, what you search for, and what you search in. You're mixing the two in your examples.
if [[ "$USER_NAME" == *"$PWD"* ]]; then
^------swap----^
And this greps in the file named by the contents of PWD clearly won't work, because PWD is a directory, not a file:
grep "$USER_NAME" "$PWD"
Write a script that takes exactly one argument, a directory name. The script should print that argument back to standard output. Make sure the script generates a usage message if needed and that it handles errors with a message.
I write code, how i understand. Am i understand correctly this question? Maybe there are other versions how find directory.
#!/bin/bash
echo "Enter fail name:"
read str
find "$str" 2>/dev/null
sa=$?
if [ "$sa" = '0' ]
then
echo "$str"
else
echo "Error"
fi
Your script doesn't appear to be using an argument. In bash the first one ($0 is your script) would be $1 and something like,
#!/bin/bash
if [ "$1" == "" ]; then
echo "$0: Please provide a directory name"
exit 1
fi
if [ ! -d "$1" ]; then
echo "$0: $1 is not a directory name"
exit 1
fi
echo "$1"
I'm using the find command in my bash script like so
for x in `find ${1} .....`;
do
...
done
However, how do I handle the case where the input to my script is a file/directory that does not exist? (ie I want to print a message out when that happens)
I've tried to use -d and -f, but the case I am having trouble with is when ${1} is "." or ".."
When the input is something that doesn't exist it does not enter my for loop.
Thanks!
Bash gives you this out of the box:
if [ ! -f ${1} ];
then
echo "File/Directory does not exist!"
else
# execute your find...
fi
Bash scripting is a bit weird. Practice before implementation. But this site seems to break it down well.
If the file exists, this works:
if [ -e "${1}" ]
then
echo "${1} file exists."
fi
If the file does not exist, this works. Note the '!' to denote 'not':
if [ ! -e "${1}" ]
then
echo "${1} file doesn't exist."
fi
assign the find to a variable and test against the variable.
files=`find ${1} .....`
if [[ "$files" != “file or directory does not exist” ]]; then
...
fi
You can try something like this:
y=`find . -name "${1}"`
if [ "$y" != "" ]; then
for x in $y; do
echo "found $x"
done
else
echo "No files/directories found!"
fi