I want to write a script that take 1 command line argument( a directory) and then prompt for 2 number, it will then print out any file(each in a different line) that has the size between that 2 number, this is my script
echo -n "Enter the first number: "
read a
echo -n "Enter the second, bigger number: "
read b
if
[ $b -lt $a ]
then
echo 'The first number must be smaller'
else
echo The files in $1 that are between $a and $b bytes are the following
echo
for var in 'ls $1'
do
if
[ -f $var ]
then
size='ls -l $var | '{ print $5 }''
if
[ $size -le $b && $size -ge $a ]
then
echo $var is $size bytes
fi
fi
done
fi
The problem is after I enter the numbers, it will print out "The files..." and then nothing else. Also, I use Vi to edit it,but the color of last three lines is not quite right(the color should match the first "fi" but it not). Can anyone show me what was wrong? Thank you.
#!/bin/kshor#!/bin/bashor whatever value your get fromecho $SHELL. AND you'll find most of your problems just by running this code with the shell debug/trace option set on. Addset -vxjustecho -n .... The debugging displays each block of code before it is executed, AND the, with a leading+sign, each line from that block that get executed, with the values for variables included. YOu should see that$sizeis not what you likely think it is. Good luck.#!/bin/??shline to, in essence, declare what shell you require to run your script. If it is compatible with good-old-bourne shell you would use#!/bin/shbut if you're using features (built-ins) that are only available in zsh or bash you should use#!/bin/bashor '#!/bin/zsh` — or whatever. You can also write things like a Perl script that starts with#!/usr/bin/perland thenchmod +x myPerlScriptand you don't have to use crap like a.plextension on the file.