Operating System - HP-UX
1832921 Members
2679 Online
110048 Solutions
New Discussion

How to make my script work?

 
SOLVED
Go to solution
zhaogui
Super Advisor

How to make my script work?

I have a script as below, but I got the syntax error. Can anybody help me solve this?
Thanks,

$1=/tmp
for i in `find $1 -type f -mtime +1 -print`
do
echo $i
done
12 REPLIES 12
Deepak Extross
Honored Contributor

Re: How to make my script work?

"$1=/tmp" ??
Do you mean
"cd /tmp"
Deepak Extross
Honored Contributor

Re: How to make my script work?

I think you want to do a
"1=/tmp", not "$1=/tmp".
suggest you use descriptive names like "mydir" instead of "1".
G. Vrijhoeven
Honored Contributor

Re: How to make my script work?

Hi,

$1 = first agrument

user DIR=/tmp
for i in `find $DIR .....`
do
action
done

Gideon
Vincent Farrugia
Honored Contributor

Re: How to make my script work?

Hello,

I think you cannot assign a string variable to $1. $1 is used to invoke the second parameter in a command intruction. So for instance if you type ls -l, the -l will be stored in $1 by default.

You should have written:

a=/tmp

where a is any string you can imagine.

Remember to replace a by $a in the for loop.

HTH,
Vince
Tape Drives RULE!!!
Ian Dennison_1
Honored Contributor

Re: How to make my script work?

File system cleanup routine, right?

Two 'for' loops will do it. The first loop will deal with the directories to be cleaned in order, and will 'ls' the directory and file template to a temporary file, then the second loop for the contents of the temporary file (the file names).

Shaer and Enjoy! Fred
Building a dumber user
Robin Wakefield
Honored Contributor

Re: How to make my script work?

Hi,

You may want to use a while instead of a for, in case you need to process files with spaces in them. Also, find will work marginally quicker if you cd into the directory first:

dir=/tmp
cd $dir
find . -mtime +1 -type f -print | while read i
do
echo "$i"
done

Rgds, Robin.
Andreas Voss
Honored Contributor

Re: How to make my script work?

Hi,

try this:

dir=/tmp
for file in `find $dir -type f -mtime +1 -print`
do
echo $file
done

Regards


zhaogui
Super Advisor

Re: How to make my script work?

I think you haven't got what I want.

It seems in my script I cannot change $1 to point to other directory instead of the one given in the first parameter.

I want to issue "myscript /tmp" in the cronjob but at first I must test myscript on /tmp/zzg where there are temporary test files that can be used to test my script. That's why I purposely changed $1 to /tmp/zzg and pass it to "for i in `find $1 ..." after I have checked that $1 filesystem free space is less than 20%.
Robin Wakefield
Honored Contributor

Re: How to make my script work?

Hi,

In that case, at the top of your script, you should say:

dir=$1 # normal usage

dir=/tmp/zzg # test usage

for i in `find $dir...`
do
...
done

Rgds, Robin.
Deepak Extross
Honored Contributor
Solution

Re: How to make my script work?

You cannot change $1, but you can use another temporary variable say DIR, which can point to either $1 or /tmp, as you need.
DIR=/tmp

DIR=$1


Does this help?
Paula J Frazer-Campbell
Honored Contributor

Re: How to make my script work?

Hi zhaogui

What exactly do you require your script to do?

As far as I can gleen you want to check free space in a lvol ??

If that is the case then this script from Andreas Voss will help.

Paula
If you can spell SysAdmin then you is one - anon
zhaogui
Super Advisor

Re: How to make my script work?

I got it. Thank you all for your help. Now I know $1 can't be changed but can use a tempory variable! Thanks!