LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   BASH scripting help (https://www.linuxquestions.org/questions/linux-newbie-8/bash-scripting-help-38114/)

Chucklez 12-12-2002 09:49 AM

BASH scripting help
 
I'm a newbie to bash scripting, and the past 2 classes we've taken a look at it. I've tried writing a script from scratch many times while looking at examples from the book, but I cant get the script to work at all, I'm completely lost. We were given a project to:

Quote:

Write a bash script that takes an ordinary file as an argument and removes the file if its size is zero. Otherwise, the script displays file's name, size, number of hard links, owner and modify date (in this order) on one line. Your script must do appropriate error checking
Anybody know what a script to do this would look like? Any help would be appreciated.

unSpawn 12-12-2002 10:00 AM

Please come back when you've got something to show. I don't mind helping out if you're really stuck but without showing what you at least tried you're asking us do your assignments. That's sad.

Chucklez 12-12-2002 10:02 AM

Is what I have:

#!/bin/bash


if [ -f "$1" ]

then

filename="$1"

set $(ls -il $filename)

filesize="$6"

hardlinks="$3"

owner="$4"

moddate="$7"

if [ "$filesize" == 0 ]

then

rm -r "$filename"

echo "$filename size was $filesize and was removed"

exit 0

else

echo "File Hard Links Owner Modified Date"

echo

echo "$filename $hardlinks $owner $moddate"

exit 1
fi
exit 2
fi


edit: I wasnt asking for my project to be done for me.

unSpawn 12-12-2002 10:45 AM

#!/bin/bash
if [ -f "$1" ]; then
# I use
f=( $(stat -t "$1") )
# Then filename is ${f[0]}:
echo "Filename: ${f[0]}"
# etc etc
filename="$1"
# This doesn't do anything for me...
set $(ls -il $filename)

filesize="$6"

hardlinks="$3"
# owner could be:
f[4]=$(grep /etc/passwd -e ":${f[4]}" | cut -d ":" -f 1)
echo "Owner ${f[4]}"
# etc etc
owner="$4"

moddate="$7"

if [ "$filesize" == 0 ]

then

rm -r "$filename"

echo "$filename size was $filesize and was removed"

exit 0

else

echo "File Hard Links Owner Modified Date"

echo

echo "$filename $hardlinks $owner $moddate"

exit 1
fi
exit 2
fi


edit: I wasnt asking for my project to be done for me.
Np. It's just we sometimes have ppl around thinking we do their assignments. If you've got probs with a script, try executing it as "sh -x <script>" or with logging as well: "sh -x <script> 2> <logfile>".

Edit: uhm, to clarify "doesn't do.*" should read "ain't my style". I mean it doesn't mean it doesn't work...

Chucklez 12-12-2002 12:07 PM

Thanks. I tried yours and was getting the same error, so I Did the sh -x script, with the error to a file. It was giving an error on line 31. So out of the blue I moved the first echo up on to the same line as the else statement and it works. :)

I'm going to do the same thing with the script I wrote also, to see if thats what was causing my problems.

Again, thanks for the help :)


All times are GMT -5. The time now is 04:40 AM.