Unix Help Please - HotUKDeals
We use cookie files to improve site functionality and personalisation. By continuing to use HUKD, you accept our cookie and privacy policy.
Get the HUKD app free at Google Play

Search Error

An error occurred when searching, please try again!

Login / Sign UpSubmit

Unix Help Please

jamhops Avatar
6y, 4m agoPosted 6 years, 4 months ago
I have to create the following if anyone can help?

Prepare a shell script that will prompt for the name of a directory and then list the files in it to the screen. Send error messages to a separate file called 'errors'.


Anyone could help be greatly appreciated as I have been trying a for a few hours...
jamhops Avatar
6y, 4m agoPosted 6 years, 4 months ago
Options

All Comments

(9) Jump to unreadPost a comment
Comments/page:
#1
anyone? :(
#2
Are you writing it in bash?

When you say prompt for the name of the directory, do you want the user to input the path or do you want to pick it up from pwd?

What error messages are you referring to? Do you mean if the directory is empty or the directory doesn't exist?

It seems like a pretty simple script just need to clarify the points above.
#3
yes bash,

User just enters the name of a directory, dont think its meant to be the path (though that would make it easier)

The error message of if the directory does not exist, If the directory exists but is empty that would be fine to allow it to run and just not list anything
#4
Take a look here and see if any of it helps: http://www.linuxconfig.org/Bash_scripting_Tutorial

In particular, look at "6. Reading User Input" as a starting points. You could adapt some of the code there to take input from the user for the dir name.

As a starting point (I'll leave it to DVS_Dee though as he sounds like he knows more about this stuff than me)

#!/bin/bash

echo -e "Please type a directory name: \c "
read someDir

# now the variable $someDir will be what the user entered, use it in conjunction with ls or something.
#5
well I wrote it as:

clear
echo "Please input a directory"
read DAT
cd DAT
ls
cd ~


But I cant work out the error handler and that will only look in the current directory for the file and though that will do if needs be I would rather look in all files for it and I tried using Find but I dont want it to list all the . files (the system files etc.)
#6
Yeah oldmanhouse is pointing you in the best direction to get started. "read" does exactly what it says on the tin, at this point the user can input the directory. Store this as a variable. Use "ls" to list directory contents (nb if directory's empty ls should return exit status 1). I think you've got enough info to point you in the right direction and as it seems like this is a homework assignment there's no point in me doing it for you as you won't learn that way.

Just start writing bits of your script and testing the results to see if you get the desired outcome or not, that's the 'fun' part of scripting.

Let us know if you get stuck at a specific part.

Good luck!
#7
above is what i tried...
#8
and its coursework I dont wana do after these couple bits...
#9
jamhops
above is what i tried...


What you've got is cool, but just add some checking incase the directory doesn't exist.

So something like:

clear
echo "Please input a directory"
read DAT
\rm ~/errors
touch ~/errors

if [ -d $DAT ];then
cd $DAT
FILES=`wc -l`
if [ $FILES -ge 1];then
ls
else
echo "Directory $DAT is empty"
echo "Directory $DAT is empty" > ~/errors
fi
else
echo "Directory $DAT not found"
echo "Directory $DAT not found" >> ~/errors
echo -n
echo "See ~/errors for errors"
echo -n
fi

cd ~

Post a Comment

You don't need an account to leave a comment. Just enter your email address. We'll keep it private.

...OR log in with your social account

...OR comment using your social account

Thanks for your comment! Keep it up!
We just need to have a quick look and it will be live soon.
The community is happy to hear your opinion! Keep contributing!