1833758 Members
2869 Online
110063 Solutions
New Discussion

Re: Script Help Needed

 
SOLVED
Go to solution
TheJuiceman
Super Advisor

Script Help Needed

Hey everyone,

I am wanting to write a script to do something like the following. Sorry if this seems easy to some of you, but scripting is not my strength:

# ls a directory > file
# Ask the user for input
# If the input is not in the file, echo an error
# If the input is a blank, space, or ENTER, echo an error

Thanks for your help.

Bobby
8 REPLIES 8
Rajeev Tyagi
Valued Contributor
Solution

Re: Script Help Needed

Bobby,

You can write this way

#!/bin/ksh

DIR=/tmp/some
ls $DIR > /tmp/file
echo "Please enter a file name : \c"
while true
do
read file
[[ "a$file" = "a" ]] && continue
grep -q $file /tmp/file
[[ $? != 0 ]] && echo "File does not exist"
break
done

Biswajit Tripathy
Honored Contributor

Re: Script Help Needed

By redirecting "ls" out put to a file and searching for
a user supplied string in it, you are basically trying to
findout if the a file with user supplied name exists in
that directory of not. To do this, a simpler way
would be:

DIR="/your_dir"
echo "Enter file name"
read file
if [ $FILE = "" ]
then
echo "Illegal file name"
exit 1
fi

if [ -a $DIR/file ]
then
echo "File exists"
else
echo "File not found"
fi

- Biswajit
:-)
TheJuiceman
Super Advisor

Re: Script Help Needed

Thanks...that will help a lot!!

How can I modify this so that I can grep out any file in the ls listing that starts with a "d" ? Thanks again.

Muthukumar_5
Honored Contributor

Re: Script Help Needed

To search a file which begins with "a" then,

ls a*

HTH.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: Script Help Needed

You can try with a script as like,

#!/bin/ksh
# check files

# User input
echo "Enter your input"
read input

# check
if [[ -z $input ]]
then
echo "ERROR: User input $input is blank"
exit 1
fi

rm -f /tmp/testlog

# Grep in the a* starting files
for file in `find . -type f -name "a*"`
do
grep $input $file >/tmp/testlog
if [[ $? -eq 0 ]]
then
cat /tmp/testlog
else
echo "ERROR: Used input $input is not file $file"
fi
done

rm -f /tmp/testlog

exit 0
### end ###

Let me know if you have issues.

HTH.
Easy to suggest when don't know about the problem!
Bharat Katkar
Honored Contributor

Re: Script Help Needed

Hi Bobby,
I will do this way:
Say my script name is prg and written in korn shell.

# chmod +x prg1.sh
# cat prg

#!/usr/bin/ksh
list=`ls $1`
echo " Enter File Name: \n"
read j
for i in $list
do
if [ "$i" = "$j" ]
then
echo "\n Your file exists in the directory \n"
exit
fi
done
echo "\n Your file doesn't exists in the directory \n"
#
#
# ls /directory
file1 file2 file3
#
# ./prg directory
Enter File Name:

file6

Your file doesn't exists in the directory

# ./prg directory
Enter File Name:

file3

Your file exists in the directory

#

Hope that helps.
Regards,
You need to know a lot to actually know how little you know
Bharat Katkar
Honored Contributor

Re: Script Help Needed

Hi Booby,
Modified scritp to list files starting with "a".
#!/usr/bin/ksh
list=`ls $1 | grep ^a`
echo " Enter File Name: \n"
read j
for i in $list
do
if [ "$i" = "$j" ]
then
echo "\n Your file exists in the directory \n"
exit
fi
done
echo "\n Your file doesn't exists in the directory \n"
#

Regards,
You need to know a lot to actually know how little you know
TheJuiceman
Super Advisor

Re: Script Help Needed

Thanks everyone!!! This has worked out wonderfully!!!