LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 12-12-2002, 09:49 AM   #1
Chucklez
LQ Newbie
 
Registered: Dec 2002
Posts: 3

Rep: Reputation: 0
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.

Last edited by Chucklez; 12-12-2002 at 09:51 AM.
 
Old 12-12-2002, 10:00 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,457
Blog Entries: 54

Rep: Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897
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.
 
Old 12-12-2002, 10:02 AM   #3
Chucklez
LQ Newbie
 
Registered: Dec 2002
Posts: 3

Original Poster
Rep: Reputation: 0
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.

Last edited by Chucklez; 12-12-2002 at 10:04 AM.
 
Old 12-12-2002, 10:45 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,457
Blog Entries: 54

Rep: Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897
#!/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...

Last edited by unSpawn; 12-12-2002 at 11:04 AM.
 
Old 12-12-2002, 12:07 PM   #5
Chucklez
LQ Newbie
 
Registered: Dec 2002
Posts: 3

Original Poster
Rep: Reputation: 0
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash scripting help (su ...) shwong Linux - General 1 11-02-2005 12:26 PM
Bash scripting pete1234 Programming 1 09-27-2005 01:48 AM
bash scripting vadon Linux - Newbie 6 05-10-2005 04:07 AM
need help with bash scripting rich2oo1 Programming 2 12-17-2003 12:50 PM
HELP with BASH scripting atwah Linux - Newbie 6 09-09-2003 01:10 AM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration