Computing.Net > Forums > Linux > Writing a small bash script.

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Writing a small bash script.

Reply to Message Icon

Name: Aaron Everhart
Date: December 12, 2002 at 01:19:44 Pacific
OS: Linux
CPU/Ram: unknown
Comment:

Hello,

We just started on bash scripting in my college course, the problem is we're only dong 3 days on it. Our project for my class is to

"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."

I've tried multiple ways of doing it by usin if then else statements, but I keep gettings errors, like integer expected, unexptected token.

Anybody know how a script like this would be written? Any help would be appreciated.




Sponsored Link
Ads by Google

Response Number 1
Name: armin
Date: December 12, 2002 at 01:24:16 Pacific
Reply:


unexpected token etc comes often, when u dont have a whitespace between parentheses

if [this is true]; then..........<== WRONG
if [ this is true ]; then........<== RIGHT

post what youve tried and i will try to help you.


0

Response Number 2
Name: Aaron Everhart
Date: December 12, 2002 at 03:21:29 Pacific
Reply:

What would the actuall code look like? I tried starting from scratch and now I cant get it to work at all.

#!/bin/bash


if [ -f "$1" ]

then

filename="$1"

set $(ls -il $filename)

filesize="$6"

hardlinks="$3"

owner="$4"

moddate="$7"

if [ "$filsize" == 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


0

Response Number 3
Name: armin
Date: December 12, 2002 at 03:47:09 Pacific
Reply:


this script works :))

u just have a type'o:
if [ "$filsize" == 0 ]
should be
if [ "$filesize" == 0 ]

:))



0

Response Number 4
Name: Aaron Everhart
Date: December 12, 2002 at 04:24:01 Pacific
Reply:

I get this error when I try to run it :p

: bad interpreter: No such file or directory


0

Response Number 5
Name: Aaron Everhart
Date: December 12, 2002 at 10:31:18 Pacific
Reply:

I've fixed the script, thanks for all your help :D


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Linux Forum Home


Sponsored links

Ads by Google


Results for: Writing a small bash script.

Writing a login script in unix www.computing.net/answers/linux/writing-a-login-script-in-unix/18953.html

chmod problem www.computing.net/answers/linux/chmod-problem/19346.html

To lock USB Flash Dr with password www.computing.net/answers/linux/to-lock-usb-flash-dr-with-password/28634.html