Strange behaviour of -d conditional expression - shell

My KornShell (ksh) manual says that -d expression returns true if the file exists and it is a directory.
So if [[ -d file ]] should return TRUE if file is a directory. But in my case this is not how it works. It returns TRUE if the file exists and is NOT a directory, but the manual of the shells says "and it is a directory". So does anyone have any idea why it is working in the opposite of what it should be?

It's working fine; it's your expectations that are wrong. In shells, a 0 return value is true, and a non-zero return value is false.
$ true ; echo $?
0
$ false ; echo $?
1

ksh File Operator | True if:
-a | file exists
-d | file is a directory
-f | file is a regular file (i.e., not a directory or other special type of file)
-r | You have read permission on file
-s | file exists and is not empty
-w | You have write permission on file
-x | You have execute permission on file, or directory search permission if it is a directory
-O | file You own file
-G | file Your group ID is the same as that of file
kshFileOperatorsFunction.ksh
#***Function to demo ksh file Operators.***#
fileOperators(){
echo "Entering fileOperators function."
if [[ ! -a $1 ]]; then
print "file $1 does not exist."
return 1
fi
if [[ -d $1 ]]; then
print -n "$1 is a directory that you may "
if [[ ! -x $1 ]]; then
print -n "not "
fi
print "search."
elif [[ -f $1 ]]; then
print "$1 is a regular file."
else
print "$1 is a special type of file."
fi
if [[ -O $1 ]]; then
print 'you own the file.'
else
print 'you do not own the file.'
fi
if [[ -r $1 ]]; then
print 'you have read permission on the file.'
fi
if [[ -w $1 ]]; then
print 'you have write permission on the file.'
fi
if [[ -x $1 && ! -d $1 ]]; then
print 'you have execute permission on the file.'
fi
echo "Exiting fileOperators function."
}
Reference : O'Reilly, Learning the KornShell Volume 1

Related

Else if loop command depending on result string

I am trying to have a script take an argument and run a command against it and retry the command if a result matches a certain string. Otherwise other results will echo the result.
We have a script at work where we run "rm <filename>" but it takes 5 seconds to return whether successful or not and I want to make a script to keep running it until it sees a "destroyed file" string in the result.
This is what I have so far..
#!/bin/bash
function rm () {
if [[ $(rm $1 | grep -m 1 'destroyed file') ]] ; then
echo "Destroyed File"
elif [[ $(rm $1 | grep -m 1 'remove failed') ]] ; then
sleep 5;
rm $1
elif [[ $(rm $1 | grep -m 1 'not found') ]] ; then
echo "file not found"
elif [[ $(rm $1 | grep -m 1 'You must have root access') ]] ; then
echo "You do not have root access"
else
echo "Incorrect parameter or command"
fi
}

Issue in echo statement in shell scripting

I have a very peculiar issue with a script that I have wrote today. I am trying to form an ip address from two variables namely url and port. I am getting the url value from a library script which echos 10.241.1.8 and the port number is 10000. Now if I concatenate both the url and the port into another variable ip, I get completely a strange result(:10000241.1.8). I have my code and its result below. Please help me with your suggestions to fix this.
clear
echo $(date +'%H:%M:%S')'>> "Sample Records" Script started...'
usage() {
echo ">> $ script.sh -ctoff 89 -env c -ns reporting -depPath /user/release/audit_prime_oozie"
echo "Usage: $ script.sh -ctoff <Cutoff number> -env <testing cluster. ex: s for staging,c,d,p and a> -ns <optional: hive namespace> -depPath <deployment path>"
}
# Function to validate if value of a parameter is not empty
validate () {
if [[ $flag != 1 ]]; then
if [[ $tmpVar == *"-"* ]] || [[ -z $tmpVar ]]; then
usage
exit 1
fi
fi
}
options=$#
if [[ -z $options ]]; then
usage
exit 1
fi
arguments=($options)
index=0
# Function to extract the parameter values
check (){
for x in $options
do
index=`expr $index + 1`
case $x in
-ctoff)
cutOff="${arguments[index]}"
tmpVar=$cutOff
validate $tmpVar
;;
-env)
env="${arguments[index]}"
tmpVar=$env
validate $tmpVar
;;
-ns)
ns="${arguments[index]}"
tmpVar=$ns
validate $tmpVar
;;
-depPath)
depPath="${arguments[index]}"
tmpVar=$depPath
validate $tmpVar
;;
esac
if [[ -z $ns ]];then
ns=reporting
fi
done
}
check $#
error_exit(){
echo "$1" 1>&2
exit 1
}
# Create the execution directory
user=$(id -u -n)
PWD=`pwd`
INSTALL_ROOT=$PWD
LOCAL_DIR="/tmp/$user/sample_duns"
if [[ ! -d $LOCAL_DIR ]]; then
mkdir -p $LOCAL_DIR
echo ">> Created local directory $LOCAL_DIR"
if [[ $? -ne 0 ]]; then
echo ">> Unable to create $LOCAL_DIR, writing to current folder $INSTALL_ROOT"
LOCAL_DIR=$INSTALL_ROOT
fi
fi
if [[ $(ls -A $LOCAL_DIR) ]]; then
echo ">> Removed the temp files from $LOCAL_DIR"
rm -r $LOCAL_DIR/*
fi
# create the file name
datestamp=$(date '+%Y%m%d%H')
outFile=sample_duns_$datestamp.txt
# Copy the contents from HDFS to Local directory
echo ">> Copying required files from HDFS"
hdfs dfs -copyToLocal $depPath/data-warehouse/config/server.properties $LOCAL_DIR || error_exit "Cannot copy files from HDFS! Exiting now.."
hdfs dfs -copyToLocal $depPath/data-warehouse/reporting/lib_getHiveServer2ip.sh $LOCAL_DIR || error_exit "Cannot copy files from HDFS! Exiting now.."
if [[ $? -ne 0 ]]; then
echo ">> Files missing. Exiting now.."
exit 1
fi
# Call the lib script to get appropriate hiveserver2 ip address from the supplied environment for beeline execution
echo ">> Reading the HiveServer2 ip"
chmod +x $LOCAL_DIR/lib_getHiveServer2ip.sh
url=$($LOCAL_DIR/lib_getHiveServer2ip.sh $env $LOCAL_DIR/server.properties)
echo url=$url
port=10000
echo ip=$url:$b
Here is my output from the terminal.
11:18:16>> "Sample Records" Script started...
>> Removed the temp files from /tmp/user/sample_duns
>> Copying required files from HDFS
>> Reading the HiveServer2 ip
url=10.241.1.8
:10000241.1.8
I am expecting the below result
ip=10.241.1.8:10000
Adding the lib_getHiveServer2ip.sh script below
. $2 # read properties file
if [[ $1 == "d" ]]; then
ip=$devHSer
elif [[ $1 == "c" ]]; then
ip=$crankHSer
elif [[ $1 == "s" ]]; then
ip=$stgHSer
elif [[ $1 == "p" ]]; then
ip=$prdHSer
elif [[ $1 == "a" ]]; then
ip=$alpHSer
else
echo ">> Invalid cluster ip encountered. Exiting now ..."
exit 1
fi
echo $ip
Your url variable contains a carriage return character for some reason. Check lib_getHiveServer2ip.sh for weirdness.
Pipe your echo output to hexdump to confirm.
Edit: looks like your properties file has bad line endings. Use the file utility to check.

Bash script "Syntax Error: Unexpected end of file"

The goal is to create a simple trash utility using a Bourne shell (it's part of an assignment). I am receiving the following error: "line 17: Syntax Error: Unexpected end of file"
I have been staring at the code for a few hours now and I can't see the mistake (probably something simple I am overlooking)
#!/bin/sh
if [$# == 0] ;then
echo "Usage: trash -l | -p | { filename }*"
else
if $1 == '-l'; then
dir $HOME/.trash
else if $1=='-p'; then
rm $HOME/.trash/*
else
for i in ${} ;do
mv i $HOME/.trash
done
fi
fi
Thanks!
This is what I achieved using shellcheck:
#!/bin/sh
if [ $# -eq 0 ] ;then
echo "Usage: trash -l | -p | { filename }*"
else
if [ "$1" = '-l' ]; then
dir "$HOME"/.trash
elif "$1"=='-p'; then
rm "$HOME"/.trash/*
else
for i in ${} ;do
mv "$i" "$HOME"/.trash
done
fi

unary operator expected with more than 1 argument

for var in "$#"
do
if test -z $var
then
echo "missing operand"
elif [ -d $var ]
then
echo "This is a directory"
elif [ ! -f $var ]
then
echo "The file does not exist"
else
basename=$(basename $var)
dirname=$(readlink -f $var)
inodeno=$(ls -i $var| cut -d" " -f1)
read -p "remove regular file $#" input
if [ $input = "n" ]
then exit 1
fi
mv $var "$var"_"$inodeno"
echo "$basename"_"$inodeno":"$dirname" >> $HOME/.restore.info
mv "$var"_"$inodeno" $HOME/deleted
fi
done
**Hello, the above code is trying to mimic the rm command in unix. Its purpose is to remove the file .
Eg if I type in bash safe_rm file1 , it works however if type in
bash safe_rm file1 file 2 , it prompts me to remove file 1 twice and gives me a unary operater expected for line 27(if [ $input = "n" ]).
Why does it not work for two files, ideally I would like it to prompt me to remove file1 and file 2.
Thanks
read -p "remove regular file $#" input
should probably be
read -p "remove regular file $var" input
That's the basic.
And this is how I'd prefer to do it:
for T in "$#"; do
if [[ -z $T ]]; then
echo "Target is null."
elif [[ ! -e $T ]]; then
echo "Target does not exist: $T"
elif [[ -d $T ]]; then
echo "Target can't be a directory: $T"
else
BASE=${T##*/}
DIRNAME=$(exec dirname "$T") ## Could be simpler but not sure how you want to use it.
INODE_NUM=$(exec stat -c '%i' "$T")
read -p "Remove regular file $T? "
if [[ $REPLY == [yY] ]]; then
# Just copied. Not sure about its logic.
mv "$T" "${T}_${INODE_NUM}"
echo "${BASE}_${INODE_NUM}:${DIRNAME}" >> "$HOME/.restore.info"
mv "${T}_${INODE_NUM}" "$HOME/deleted"
fi
fi
done

Check if passed argument is file or directory in Bash

I'm trying to write an extremely simple script in Ubuntu which would allow me to pass it either a filename or a directory, and be able to do something specific when it's a file, and something else when it's a directory. The problem I'm having is when the directory name, or probably files too, has spaces or other escapable characters are in the name.
Here's my basic code down below, and a couple tests.
#!/bin/bash
PASSED=$1
if [ -d "${PASSED}" ] ; then
echo "$PASSED is a directory";
else
if [ -f "${PASSED}" ]; then
echo "${PASSED} is a file";
else
echo "${PASSED} is not valid";
exit 1
fi
fi
And here's the output:
andy#server~ $ ./scripts/testmove.sh /home/andy/
/home/andy/ is a directory
andy#server~ $ ./scripts/testmove.sh /home/andy/blah.txt
/home/andy/blah.txt is a file
andy#server~ $ ./scripts/testmove.sh /home/andy/blah\ with\ a\ space.txt
/home/andy/blah with a space.txt is not valid
andy#server~ $ ./scripts/testmove.sh /home/andy\ with\ a\ space/
/home/andy with a space/ is not valid
All of those paths are valid, and exist.
That should work. I am not sure why it's failing. You're quoting your variables properly. What happens if you use this script with double [[ ]]?
if [[ -d $PASSED ]]; then
echo "$PASSED is a directory"
elif [[ -f $PASSED ]]; then
echo "$PASSED is a file"
else
echo "$PASSED is not valid"
exit 1
fi
Double square brackets is a bash extension to [ ]. It doesn't require variables to be quoted, not even if they contain spaces.
Also worth trying: -e to test if a path exists without testing what type of file it is.
At least write the code without the bushy tree:
#!/bin/bash
PASSED=$1
if [ -d "${PASSED}" ]
then echo "${PASSED} is a directory";
elif [ -f "${PASSED}" ]
then echo "${PASSED} is a file";
else echo "${PASSED} is not valid";
exit 1
fi
When I put that into a file "xx.sh" and create a file "xx sh", and run it, I get:
$ cp /dev/null "xx sh"
$ for file in . xx*; do sh "$file"; done
. is a directory
xx sh is a file
xx.sh is a file
$
Given that you are having problems, you should debug the script by adding:
ls -ld "${PASSED}"
This will show you what ls thinks about the names you pass the script.
Using -f and -d switches on /bin/test:
F_NAME="${1}"
if test -f "${F_NAME}"
then
echo "${F_NAME} is a file"
elif test -d "${F_NAME}"
then
echo "${F_NAME} is a directory"
else
echo "${F_NAME} is not valid"
fi
Using the "file" command may be useful for this:
#!/bin/bash
check_file(){
if [ -z "${1}" ] ;then
echo "Please input something"
return;
fi
f="${1}"
result="$(file $f)"
if [[ $result == *"cannot open"* ]] ;then
echo "NO FILE FOUND ($result) ";
elif [[ $result == *"directory"* ]] ;then
echo "DIRECTORY FOUND ($result) ";
else
echo "FILE FOUND ($result) ";
fi
}
check_file "${1}"
Output examples :
$ ./f.bash login
DIRECTORY FOUND (login: directory)
$ ./f.bash ldasdas
NO FILE FOUND (ldasdas: cannot open `ldasdas' (No such file or directory))
$ ./f.bash evil.php
FILE FOUND (evil.php: PHP script, ASCII text)
FYI: the answers above work but you can use -s to help in weird situations by checking for a valid file first:
#!/bin/bash
check_file(){
local file="${1}"
[[ -s "${file}" ]] || { echo "is not valid"; return; }
[[ -d "${file}" ]] && { echo "is a directory"; return; }
[[ -f "${file}" ]] && { echo "is a file"; return; }
}
check_file ${1}
Using stat
function delete_dir () {
type="$(stat --printf=%F "$1")"
if [ $? -ne 0 ]; then
echo "$1 directory does not exist. Nothing to delete."
elif [ "$type" == "regular file" ]; then
echo "$1 is a file, not a directory."
exit 1
elif [ "$type" == "directory" ]; then
echo "Deleting $1 directory."
rm -r "$1"
fi
}
function delete_file () {
type="$(stat --printf=%F "$1")"
if [ $? -ne 0 ]; then
echo "$1 file does not exist. Nothing to delete."
elif [ "$type" == "directory" ]; then
echo "$1 is a regular file, not a directory."
exit 1
elif [ "$type" == "regular file" ]; then
echo "Deleting $1 regular file."
rm "$1"
fi
}
https://linux.die.net/man/2/stat
https://en.m.wikipedia.org/wiki/Unix_file_types
A more elegant solution
echo "Enter the file name"
read x
if [ -f $x ]
then
echo "This is a regular file"
else
echo "This is a directory"
fi
Answer based on the title:
Check if passed argument is file or directory in Bash
This works also if the provided argument has a trailing slash .e.g. dirname/
die() { echo $* 1>&2; exit 1; }
# This is to remove the the slash at the end: dirName/ -> dirName
fileOrDir=$(basename "$1")
( [ -d "$fileOrDir" ] || [ -f "$fileOrDir" ] ) && die "file or directory $fileOrDir already exists"
Testing:
mkdir mydir
touch myfile
command dirName
# file or directory mydir already exists
command dirName/
# file or directory mydir already exists
command filename
# file or directory myfile already exists
#!/bin/bash
echo "Please Enter a file name :"
read filename
if test -f $filename
then
echo "this is a file"
else
echo "this is not a file"
fi
One liner
touch bob; test -d bob && echo 'dir' || (test -f bob && echo 'file')
result is true (0)(dir) or true (0)(file) or false (1)(neither)
This should work:
#!/bin/bash
echo "Enter your Path:"
read a
if [[ -d $a ]]; then
echo "$a is a Dir"
elif [[ -f $a ]]; then
echo "$a is the File"
else
echo "Invalid path"
fi

Resources