have written the below code to check whether the string received from user is a file name or dir using case statement, but its going into default case*).
#!/bin/sh
#Get a string from user and check whether its a existing filename or not
rm str2
rm str3
echo "enter a file \c"
read fil
#echo $fil
grep $fil fl >> str2 #fl has all filename stored in it using ls -l >> fl
cut -c 1-1 str2 >> str3
cat str2
cat str3 #it has the value '-'
case "$str3" in
-) echo "you have entered a filename" ;;
*) echo "not a filename"
exit ;;
esac
Could you write out the logical flow you are trying to achieve? It's all a bit confused and mixed up with which are variables and which are files. str3 is written to as a file and then you try to use it as a variable.
If you read in a variable fil and you are looking to test if it has a leading hyphen - then you can more simply do something like this:-
read "enter a file: "?fil
not_first="${fil#?}" # Trim off first character from input
first="${fil%$notfirst}" # Trim off all the other characters from input
case "$first" in.......
Does that help? If the statements don't make sense, then let me know and I will explain them.
Thanks robin, have modified the code and it works only if one file exists (it doesn't work in case redundant file name exists)
Have read the file testing today and written the below code based on my understanding. it works fine. but the problem var_1 and var_2 are not resetting (how do i initialize the values because it takes the already exists value)
Program Flow:
1) Get a word from user
2) check whether a file or directory exists in the given name
3) if not, display no dir or file was exists in the given name
#!/bin/sh
# Get a word from user and check whether a filename or directory exists in the same name
echo "enter a name \c"
read name
# if its a file, var_1 will have 0
var_1=`[ -f $name ];echo $?`
# if its a directory, var_2 will have 1
var_2=`[ -d $name ];echo $?`
if [ $var_1 -eq 0 -a $var_2 -eq 1 ]
then
echo "Given name has both file and dir"
elif [ $var_1 -eq 0 -a $var_2 -ne 1 ]
then
echo "Given name is a file"
elif [ $var_2 -eq 1 -a $var_1 -ne 0 ]
then
echo "Given name is a directory"
else
echo "neither a file nor a dir"
fi
Please use the "code" tag for code as you have agreed by the forum rules.
Have a look at:
#!/bin/sh
# My system has /bin/sh --> bin/bash, but should be posix compliant
printf "Enter a filename: "
read FN
# List items starting with entered FN, and expand it to a list
# If 'ls' fails, exit with message and error code
str=$(ls ${FN}* 2>/dev/null) || { echo "No entry $FN found!" ; exit 1 ; }
for F in $str
do
if [ -L "$F" ]
then echo "$F is a link"
elif [ -d "$F" ]
then echo "$F is a directory"
elif [ -f "$F" ]
then echo "$F is a file"
else # This should never be printed, as its reading the existing content
echo "No item $F found!"
fi
done
Am I missing something with for F in $str ?
Edit: Yes, I had forgotton to update that varname from a previous approach, and just edited right after posting.
Edit2: Thought, that example doesnt cover spaces in filenames.
Note that a symbolic link is a file and a directory is a file just as much as a regular file is a file. And, there are other file types in addition to those three, including, but not necessarily limited to FIFOs, sockets, block special files, and character special files.
If you don't have files ending in non-alphanumeric characters, have you considered this replacement for your script?
#!/bin/sh
# Get a word from user and check whether a filename or directory exists in the same name
printf 'enter a name: '
read name
ls -dF "$name"