LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 11-10-2010, 07:40 PM   #1
navie
LQ Newbie
 
Registered: Nov 2010
Posts: 6

Rep: Reputation: 0
Shell script


heya
i am new to shell scripting and i have assignment. I have to write a program that checks the size of a file and if the size is zero it will delete it and if not then it will print the name and the modification date and stuff. I know for that i gotta use "ls -l <file name>". I am able to run this but I am not able to figure out how can i remove a file if its size is zero. All i know is that i have to use "-s <filename>" to see the size of the file and i have to put that in an if statement. I have tried everything i could, spent 1 hour on this thing. Am just exhausted now and want someone's help . PLEASE HELP ME!!
Thank you
 
Old 11-10-2010, 07:47 PM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Jessie (Fluxbox WM)
Posts: 1,387
Blog Entries: 52

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
Sounds like you are almost there - an if statement with a '-s' test, an rm to delete the file or an ls to list it.

Post the script that you have written so far (even if it isn't working), and you will get some feedback as to what is missing. It might only be a syntax error.
 
Old 11-10-2010, 08:23 PM   #3
navie
LQ Newbie
 
Registered: Nov 2010
Posts: 6

Original Poster
Rep: Reputation: 0
#!/bin/bash
test -s $1
#if [ -s ]
#if [ !-s ]
if [ echo $? -eq 0 ]
then
rm -r $1
else
echo "File name is " $1
echo `ls -l $1`
fi

i tried this "#if [ !-s ]" before but it deleted the file no matter what. So then i tried "if [ echo $? -eq 0 ] " and now it doesn't even enter the body of if statement, just goes in else. I know am going all over the place but yah.
Thanks for replying appreciate it
 
Old 11-10-2010, 09:02 PM   #4
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Jessie (Fluxbox WM)
Posts: 1,387
Blog Entries: 52

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
The '-s' parameter needs to have an argument (which is the file name), ie:
Code:
if [ -s "$1" ]
then
   # file is non-zero
else
   # file is zero
fi
And so you don't need the test statement at all. Notice that the '-s' check is for a non-zero file, so you will need to swap around the content bodies of the then and the else.

It is useful to use double quotes around the $1; it still gets expanded, but it means that if the filename has characters such as spaces, the name will still get treated as a single item.

Also, you don't need to echo the output from the ls since it already outputs its result; just have the ls statement directly in the body.

Last edited by neonsignal; 11-10-2010 at 09:04 PM.
 
1 members found this post helpful.
Old 11-10-2010, 09:17 PM   #5
navie
LQ Newbie
 
Registered: Nov 2010
Posts: 6

Original Poster
Rep: Reputation: 0
OMG its working. Thank you very very very much .
 
Old 11-11-2010, 12:52 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
You should bookmark these: http://tldp.org/LDP/Bash-Beginners-G...tml/index.html http://www.tldp.org/LDP/abs/html/
 
1 members found this post helpful.
  


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
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 08:58 PM
pass variable from one shell script into another shell script xskycamefalling Programming 9 10-03-2009 02:45 AM
help with execute mulitple shell script within shell script ufmale Programming 6 09-13-2008 01:21 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 06:32 PM


All times are GMT -5. The time now is 03:23 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration