Why does my script not work? (Noob Alert)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Why does my script not work? (Noob Alert)
# 1  
Old 02-21-2008
Why does my script not work? (Noob Alert)

I am a scripting noob and I have tried to search on google, but cannot find the answer as to why this script doesn't work properly.

The idea of this script is that it will list all files starting with f in a certain folder, and delete all but the three newest one. I am trying to achieve this by doing command "ls -t" so the newest come out on top. Then I simply skip the first 3 and start deleting what comes after (in the supplied script I am just renaming the files). For some reason when the counter hits 10, it seems to think that it a smaller value than 3. I think it only sees the 1st character of 10, leading it to think its value is 1? Yet when I echo $counter, it says 10?

I am sure you are going to slap me around the ears with one-line scripts that do the job, but as I am trying to learn I would really like to see why mine does not work as well. I really do apreciate any kind of input. Thanks in advance.

My script:

#!/bin/bash
declare -i counter=1
for file in $(ls -t f*); do
if [[ $counter > 3 ]]; then
mv $file $file.del
fi
let counter=$counter+1
done
exit 0
# 2  
Old 02-21-2008
Code:
#!/bin/ksh

for file in $(ls -t1 f* | sed -e ':a' -e '$d;N;2,3ba' -e 'P;D'); do
    mv "$file" "$file.del"
done

sed1liners
# 3  
Old 02-21-2008
Modifying your program rather then one liner:-)

#!/bin/bash
declare -i counter=1
for file in $(ls -t t*); do
if [ $counter -gt 3 ];then
mv $file $file.del
fi
counter=`expr $counter + 1`
done
exit 0
# 4  
Old 02-21-2008
@push, Great! that works, thanks as now I can work out what I have been doing wrong. Smilie

@vgersh99 thanks for you input too, I will be having a look at this tonight to see how it works. I am a bit disapointed it wasn't a one liner though Smilie
# 5  
Old 02-21-2008
Code:
#!/bin/bash

for file in $(ls -t t*| head -3)
do
  mv $file $file.del
fi

exit 0

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Noob. shell script with prompt?

Hey Guys and Gals, Im a complete Noob to Unix. But recently have started working on a unix system for my PET/CT scanner. My scanner comes with a MOD drive for backup. I would like to back up to USB (its alot cheaper). But the only way to do so is by manually enterin the unix commands. Id like to... (13 Replies)
Discussion started by: TorresGXL
13 Replies

2. Shell Programming and Scripting

Need help with simple script (noob)

Hi This is my first time attempting a bash script, or any kind of programming. I'm trying to get a clear picture but it helps to work backwards. It would be greatly appreciated if someone could show me what this script would look like... Write a script that displays todays date in... (1 Reply)
Discussion started by: earthicle
1 Replies

3. UNIX for Dummies Questions & Answers

Date check script (noob)

Hello, I've been given the task of checking the date of expiration dates and notify when the date is 30 days away from expiration. As stated in the Title, I'm very new to this, I did a good bit of work over the last two days to only find out I was going down the wrong path. I'm not... (3 Replies)
Discussion started by: drey4184
3 Replies

4. UNIX for Dummies Questions & Answers

Is there any way of splitting the script (Noob Here).

I m writing a script to check Server Hardening. The problem is whenever i add new point it grows and it become very tedious to edit the script file. Is there any way of making them separate and call them from one base script? Is it possible to define global variable that can be accessed via... (5 Replies)
Discussion started by: pinga123
5 Replies

5. Shell Programming and Scripting

Total noob script

Hi i am a total noob at shell scripting. i was wondering if somebody could help me with my script. i want the script to search the dev folder for the burner file because they are different between distrubutions? as i under stand it. this i the script. #!/bin/bash echo "Script för att bränna 360... (4 Replies)
Discussion started by: MatsO
4 Replies

6. UNIX for Dummies Questions & Answers

NOOB - Scripting to make an App work

Hello everyone. i work in a school and i ran into an application today that is a real pickle. i know how to make it work, but i would need to script it. The way to make it work would be to have a script check each local user profile on login, see if the directory already exists, do nothing... (27 Replies)
Discussion started by: jscan
27 Replies

7. UNIX for Dummies Questions & Answers

Linux noob needing help with a script

Hi, Very new to linux but I've just recently setup an ubuntu server. I have 2 broadband connections and would like to have fallback on the server should one of the lines fail. I know what I want it to do, but dont know how to script it. heres the senario; ubuntu server with 2 ethernet... (0 Replies)
Discussion started by: ziggycat
0 Replies

8. Shell Programming and Scripting

Noob needs script help Grep

while read s1 s2; do grep -i -w $s1 6-29data | tr "" "" | sed 's/^*,//' | sed 's/200906/2009-06-/g' >> $s1.txt ; cat header > here ; cat $s1.txt | sort | uniq | tac >> here ; cat here | uniq > $s1.txt ; done < list Wow this almost worked file list has 8000 stock symbols which are... (4 Replies)
Discussion started by: harte
4 Replies

9. Shell Programming and Scripting

noob. need help to create a script.

Hi All. im a noob to scripting. could somone help me with a script please. what i want to do is. 1. run a cmd in the script - qmqtool -s this will give me an output similar to this. Messages in local queue: 790 Messages in remote queue: 306 Messages in todo queue: 23 i then want... (1 Reply)
Discussion started by: aron
1 Replies

10. UNIX for Dummies Questions & Answers

Using PHP script with crontab (NOOB)

Hi, I am trying to use a PHP script as a test for a cron job. My crontab is 1 line: 30 * * * * /home/www/inc/crontab.php if I test the file through the browser (ie. http://www.domain.com/inc/crontab.php), the PHP script works -- so there is nothing wrong with the PHP script itself or the... (5 Replies)
Discussion started by: Bobafart
5 Replies
Login or Register to Ask a Question