Problem with a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with a script
# 1  
Old 05-15-2010
Problem with a script

Hi everyone,
I got a problem with a script. What it's supposed to do is:

  • to take as arguments a directory name <dir> and a dimension (in byte) <dim>;
  • if <dir> exists, to write name and dimension of every regular file within it that sizes lesser than <dim> in regFileList.
Nothing happens when I run it, and digiting later "echo $?" result is 0.

Here's the code:

Code:
#!/bin/bash 

if [ $# -ne 2 ]
then
    echo "script use: bash prova <dir> <dim>"
else
    if [ -d $1 ]
    then
        for file in `ls $1`
        do
          if [ -f $file ]
          then
              a=`ls -o | awk '/$file/ {print $4}'`
              if [ $a -lt $2 ]
              then
                  ls -o | awk '/$file/ { print $3 " " $4 }' >> regFileList
              fi
          fi
        done
    else
        echo "$1 is not a directory or it does not exist"
    fi
fi

Where do I make a mistake?

Also, since I'm learning, I'd like any tips on my coding.

Thanks.
# 2  
Old 05-15-2010
Code:
a=`ls -o | awk '/$file/ {print $4}'

$file is within the shell single quotes and is not substituted.
There is only one filename in the loop so we only need to look at the directory for the one file.


Code:
a=`ls -o $file| awk '{print $4}'`

ls -o $file | awk '{print $3 " " $4 }' >> regFileList

This User Gave Thanks to methyl For This Post:
# 3  
Old 05-15-2010
Right, but still not working.
# 4  
Old 05-15-2010
Please post the current version of the script and any output. (When I ran the original script I got loads of errors from "test" and "awk").
What happens with the corrected version?
# 5  
Old 05-15-2010
Quote:
Originally Posted by methyl
(When I ran the original script I got loads of errors from "test" and "awk")
Wow, my terminal reports nothing when I run it, even if surely there's something wrong; maybe this depends on my terminal version or what?

I simply corrected it with your lines:

Code:
#!/bin/bash

if [ $# -ne 2 ]
then
    echo "script use: bash prova <dir> <dim>"
else
    if [ -d $1 ]
    then
        for file in `ls $1`
        do
          if [ -f $file ]
          then
              a=`ls -o $file | awk '{ print $4 }'`
              if [ $a -lt $2 ]
              then
                  ls -o $file | awk '{ print $3 " " $4 }' >> regFileList
              fi
          fi
        done
    else
        echo "$1 is not a directory or it does not exist"
    fi
fi

There's no output.

EDIT: Maybe my code is not good at all? Would you post a version of yours?

Last edited by Luke Bonham; 05-15-2010 at 05:32 PM..
# 6  
Old 05-15-2010
Just ran yours on a directory containing some files which were smaller than the second parameter value. No errors.

The output is in the file called regFileList.

To read the output.

Code:
more regFileList


I should add that your script will misbehave if the directory name or the files contain space characters.
The main task is to answer your original post.
# 7  
Old 05-15-2010
Ok it works, I just didn't use it properly. :-)

My latest version is:

Code:
#!/bin/bash

if [ $# -ne 2 ]
then
    echo "script use: bash prova <dir> <dim>"
else
    if [ -d $1 ]
    then
        for file in `ls $1`
        do
          if [ -f $file ] && [ $(ls -o $file | awk '{ print $4 }') -lt $2 ]
          then
              ls -o $file | awk '{ print $3 " " $4 }' >> regFileList
          fi
        done
    else
        echo "$1 is not a directory or it does not exist"
    fi
fi

Thanks for the awk tip. Problem is solved.

Quote:
Originally Posted by methyl
I should add that your script will misbehave if the directory name or the files contain space characters.
What should be done in this case?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem in calling a script inside a script

Hi team, I have a script in different folder. Now i want to call that script and execute that script from that path alone. My code is #!/bin/bash wname=yahoo PATH='/opt/IBM' wac=`/usr/bin/ls $PATH | /usr/bin/grep "$wname"` STOP=`/usr/bin/find $PATH/$wac -type f -name "stop.sh"`... (8 Replies)
Discussion started by: natraj005
8 Replies

2. Shell Programming and Scripting

Shell script newbie, what is problem with my script?

Hello, Ubuntu server 11.10 can anybody help what is problem with my shell script? #!/bin/bash #script to find out currently logged on user is root or not. if ] then echo "You are super" else echo "You are awesome!" fi When I run script, I get following output ./uid: line 3: I... (4 Replies)
Discussion started by: kaustubh
4 Replies

3. Shell Programming and Scripting

Problem while calling a script within a script

Hi , I have moduled my scripts in three scripts . From First script i am calling second and from second i am calling third for some check . Problem is with the third script call. ---In second script EXP ='test.\abc.\Server.*abc.xml.*' pid=$($HOME/bin/checkpid $EXP) --Third... (2 Replies)
Discussion started by: amrishn
2 Replies

4. Shell Programming and Scripting

Problem running a program/script in the background from a script

Hi all, I have a script that calls another program/script, xxx, to run in the background. Supposedly this program at most should finish within five (5) minutes so after five (5) minutes, I run some other steps to run the script into completion. My problem is sometimes the program takes... (5 Replies)
Discussion started by: newbie_01
5 Replies

5. UNIX for Dummies Questions & Answers

Problem with script

Hello All. I have a script that is suppossed to start up a daemon but when executed, simply hangs. Could you please take a look and let me know where the problem might be? TIA ################################################################### # # SCRIPT: dstart3000.sh # Bring up the Domain... (6 Replies)
Discussion started by: grin1dan
6 Replies

6. Shell Programming and Scripting

script problem

Hello I am trying to execute the following script #!/bin/ksh ABC=/abc/def/ghi/jkl/mna/opq/input cd $ABC Filename=`ls -t $ABC | tail -1`; echo $Filename awk 'NR == 1 || substr($0,63,5) ~ /H... / && \ _++ == 1 { fn && close(fn); fn = "$Filename_" ++c; _ = 1 } { print > fn }' $Filename... (2 Replies)
Discussion started by: dsdev_123
2 Replies

7. Shell Programming and Scripting

call shell script from perl cgi script problem

hi,, i have perl scipt with line : system('./try.sh $t $d $m'); in shell scipt try.sh i have the line: echo $1 its not printing value of $t that i hav passed..y is it so..i am running it from apache web server (2 Replies)
Discussion started by: raksha.s
2 Replies

8. Shell Programming and Scripting

ssh script problem problem

Hi Please help me with the following problem with my script. The following block of code is not repeating in the while loop and exiting after searching for first message. input_file ========== host001-01 host001-02 2008-07-23 13:02:04,651 ConnectionFactory - Setting session state... (2 Replies)
Discussion started by: pcjandyala
2 Replies

9. Shell Programming and Scripting

Help. Script problem

hey guys. i have a bunch of programs in a script that needs to run as root and the rest as another user, we'll call him gabriel. now, in this script, i want to run the first few lines as root. now, how do i, after running as root, tell the script to run the remaining lines as the user gabriel?... (3 Replies)
Discussion started by: Terrible
3 Replies

10. UNIX for Dummies Questions & Answers

Problem starting a script from a 'main'-script

Please Help! :o I have a main script (ksh) where another script is called (convert_picture). Normally this works ok, but since some changes has been made on the unix-server (I dont know what :( ) suddenly it doesnt work anymore: i get an error message: ksh: convert_picture not found. I am... (3 Replies)
Discussion started by: Rakker
3 Replies
Login or Register to Ask a Question