LinuxQuestions.org
Review your favorite Linux distribution.
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 03-20-2013, 05:21 AM   #1
wielgeraats
LQ Newbie
 
Registered: Mar 2013
Posts: 2

Rep: Reputation: Disabled
Using if statement


Hi.

I'm creating a script to clean up a map periodically. The goal is to maintain 3 files in the map and to clean up the oldest file. The problem for me is the if statement.
Belowstanding scripts does create an output redirection and creates a file named 3.

if `ls | wc -l > 3'
then rm `ls -t | tail -1`
fi

the following gives an error message "command not found"

if `ls | wc -l` -gt 3

Can someone help me?

Wiel Geraats
 
Old 03-20-2013, 05:33 AM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Just try like this:
Code:
#!/bin/bash
num=`ls -l | wc -l`
if [ $num -gt 3 ]; then
rm `ls -t | tail -1`
fi
OR, you can use:
Code:
#!/bin/bash
if [ `ls -l | wc -l` -gt 3 ]; then
rm `ls -t | tail -1`
fi
Note: Better use ls -l instead of just ls. Also use $(command) instead of using backticks i.e. "`" in script.
 
Old 03-20-2013, 05:51 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,374

Rep: Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383
Code:
if [[ `ls | wc -l` -gt 3 ]]
then 
    echo 'here'
fi
http://rute.2038bug.com/index.html.gz
 
Old 03-20-2013, 05:54 AM   #4
wielgeraats
LQ Newbie
 
Registered: Mar 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
Hi shivaa and chris

Thanks for the quick response, it works fine now.

Last edited by wielgeraats; 03-20-2013 at 06:00 AM. Reason: completing
 
Old 03-20-2013, 05:57 AM   #5
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Happy to hear that.

Please Mark the thread as solved (option is under Thread Tools on top menu), if you think it has so.
 
Old 03-20-2013, 08:21 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960
1) Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

2) $(..) is highly recommended over `..`

3) When using bash or ksh, it's recommended to use [[..]] for string/file tests, and ((..)) for numerical tests. Avoid using the old [..] test unless you specifically need POSIX-style portability.

http://mywiki.wooledge.org/BashFAQ/031
http://mywiki.wooledge.org/ArithmeticExpression

4) parsing ls for filenames and metadata is not recommended. If you want to know how many files are in a directory, you can use an array.

Code:
files=( * )

if (( ${#files[@]} > 3 )); then
    rm "${files[@]: -3}"          #removes the last 3 files in the file list
fi
How can I use array variables?
http://mywiki.wooledge.org/BashFAQ/005/

Although the above simply relies on the shell's default sorting. If you can ensure that all the files have the same pattern and their date directly in their filename in YYYY-MM-DD form, then this would be just fine.

Otherwise you'll have to run them through a loop to test the metadata, or else use find.


How can I find the latest (newest, earliest, oldest) file in a directory?
http://mywiki.wooledge.org/BashFAQ/003
http://mywiki.wooledge.org/BashFAQ/099
 
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
Perl switch statement throwing error like Bad case statement (invalid case value?) kavil Programming 2 10-07-2010 04:50 AM
[SOLVED] Shell script for adding a statement in a file after a particular statement TheIndependentAquarius Programming 4 06-28-2010 03:07 AM
Problem with if statement in a find -exec statement romsieze Programming 2 10-02-2008 12:38 AM
for statement WT* malikah Programming 7 07-11-2008 02:15 AM
Case statement with If statement cbo0485 Linux - Newbie 4 11-07-2007 08:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:08 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