Problem with a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with a script
# 8  
Old 05-15-2010
Use a "while" loop not a "for" loop. In the "for" loop a filename with a space character becomes two parameters. Within the "while" loop preserve spaces by placing double quotes round the variable.

Code:
#!/bin/bash

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


BTW. Until first looking at this board I had never seen "ls" in a "for" line ever. Yet we see daily here. It must be mentioned in a popular shell book.
This User Gave Thanks to methyl For This Post:
# 9  
Old 05-15-2010
Thanks man, I learned something new! Smilie

Quote:
Originally Posted by methyl
BTW. Until first looking at this board I had never seen "ls" in a "for" line ever. Yet we see daily here. It must be mentioned in a popular shell book.
Just because I'm new to scripting languages and like to experiment.

If you find this kind of stuff somewhere, please link me.
# 10  
Old 05-15-2010
The FAQ section of this site is worth a read.
A quick search of this site for "bash tutorial" turned up this post:

https://www.unix.com/shell-programmin...ncryption.html


It is important to realise early on that there is a huge variety of Linux and unix out in the field. Beware of blindly copying shell script without working what it will actually do on your system.
# 11  
Old 05-16-2010
ls -o output itäs little different, envitonment like LANG and so on change the output. This ex. has done using my ls -o result format:
-rw-r--r-- 1 owner 45 2010-03-29 18:43 filename

Code:
#!/bin/bash or ksh93 or ...

[ $# -ne 2 ] && echo "usage:$0 <dir> <dim>" >&2  && exit 1

dir="$1"
dim="$2"

[ ! -d "$dir" ] && echo "$dir is not a directory or it does not exist" >&2 && exit 2

cd "$dir"
ls -o | while read filedata
do
        values=($filedata)
        owner=${values[2]}
        size=${values[3]}
        date=${values[4]}
        # filename can include spaces, empty other flds, rest is filename
        for f in 0 1 2 3 4 5
        do
               values[$f]=""
        done
        name=${values[*]}

        if [ -f "$name" -a "$size" -lt "$dim" ] ; then
          echo "$name $size $date"
        fi
done

Code:
chmod a+rx prova
./prova somedir 100 > regFileList


Last edited by kshji; 05-16-2010 at 04:42 AM..
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