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"
Related
#!/bin/bash
set -e
deb_folder='/home'
myinstall(){
deb=$1
temp="${1%.*}"
num="${temp##*.}"
temp2="${temp%.*}"
method="${temp2##*.}"
case "$method" in
md5)
md5=md5sum $deb
echo 'here'
if [[ "${md5:0:3}${md5: -3}" == "$num" ]]; then echo 'correct' else echo $deb'md5 error';false;fi
;;
256)
sha256=sha256sum $deb
if [[ "${sha256:0:3}${sha256: -3}" == "$num" ]]; then apt-get install $deb; else echo $deb'sha256 error';false;fi
;;
*) echo $deb'sum type wrong'
;;
esac
}
myinstall "${deb_folder}/rstudio-1.4.1106-amd64.md5.e596d3.deb"
Expect result of above bash script is correct or /home/rstudio-1.4.1106-amd64.md5.e596d3.debmd5 error,but I got here after change md5=md5sum $deb to md5=$(md5sum $deb).
Where is the problem?
Problem 1
Instead of md5=md5sum $deb you probably meant md5=$(md5sum $deb) or even better md5=$(md5sum "$deb"). The same goes for sha256=sha256sum $deb.
md5=$(md5sum $deb) runs the command md5sum $deb and stores its output in the variable md5.
md5=md5sum $deb runs the "command" $deb while setting the environment variable md5=md5sum for this command. You may have seen this construct in idioms like IFS= read -r line or LC_ALL=C sort before.
Problem 2
The following if has only one branch. That else is very misleading.
if [[ "${md5:0:3}${md5: -3}" == "$num" ]]; then echo 'correct' else echo $deb'md5 error';false;fi
If written properly formatted, the problem becomes clear:
if [[ "${md5:0:3}${md5: -3}" == "$num" ]]; then
echo 'correct' else echo $deb'md5 error'
false
fi
Here the else is not a keyword, but a simple argument to echo. If you enter the if you would get the output correct else echo /home/rstudio-1.4.1106-amd64.md5.e596d3.debmd5 error.
To fix this, add a ; or linebreak before else.
You may as well fix the check "${md5:0:3}${md5: -3}" == "$num". I don't think these things will ever be equal. Execute your script with set -x to print the values of your variables, then you see the problems.
I have a script that I call from another script with a specific filename as second argument.
script1 </var/tmp/outputfile>
within script1, I want to add a logic to find if the filename after /var/tmp is 'outputfile' if, yes I need to add some logic, if not other logic.
Following code is not working in the script1.sh.
#!/bin/sh
if [ $# -ne 1 ]; then
echo "Invalid usage"
echo "Usage: $0 <logfile>"
exit 1
fi
if [[ $1 == *"outputfile" ]]; then
echo "Found it"
else
echo "Search fail"
fi
Note that input always includes the full path where the file is generated. I just need to search for the filename that is at the end of the input string ($1). I tried many ways. It is not working in the script.
If I try similar logic directly in the shell it is working fine. Can someone tell me what should I add in the script.
[root#mgmt:~]$echo $file
/var/tmp/output
[root#mgmt:~]$if [[ $file == *"output" ]] ; then echo "ok"; else echo "fail";fi
ok
I'm learning BASH scripting, and I've got a problem. I wrote a generator, that checks if there is script with same name as given and if not it makes one, makes it executable and gives it proper shebang. But it doesn't work. I wanted it to exit when there is already one script with that name. Can you tell me what I'm doing wrong?
#!/bin/bash
clear
echo "Name the script"
read name
echo "What shell? (bash/sh)"
read type
if [ -e ./$name ]
then
echo "You already have that script"
read
exit
else
touch $name
chmod 755 $name
fi
case $type in
"bash") echo '#!/bin/bash' > $name ;;
"sh") echo '#!/bin/bash' > $name ;;
*) echo "I don't know what do you want" ;;
esac
vim $name
There is an extra read which is holding you up:
if [[ -e ./$name ]]
then
echo "'./$name' already exists" # <<<< always report the name
# read <<<< This is the line which is causing problems
exit 1 # <<<< indicate there was an error
else
touch "$name"
chmod 755 "$name"
fi
Quote filenames incase they contain embedded whitespace (you don't need them around variables if you use [[ ]].
Also note that the #! line for sh is wrong (you are using bash for both)
Edit:
If you have /etc/shells on your system, this is a good exercise in using select menu. Try this:
#!/bin/bash
read -p "Name the script: " name
if [[ -e ./$name ]]
then
echo "'./$name' already exists" # <<<< always report the name
# read <<<< This is the line which is causing problems
exit 1 # <<<< indicate there was an error
else
touch "$name"
chmod 755 "$name"
fi
declare -a shells
i=0
while read shell
do
if [[ $shell != \#* && $shell != "" ]]
then
shells[i++]="$shell"
fi
done < /etc/shells
PS3='Please select the shell: '
select type in "${shells[#]}" QUIT
do
echo "You selected $type"
if [[ $type == QUIT ]]
then
exit 2
fi
break
done
echo "#!$type" > "$name"
vim $name
You might also consider using $EDITOR rather than hard-coding vim.
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
}
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