Convert Bash shell script to Korn shell script

Convert Bash shell script to Korn shell script

Post by Mat » Wed, 24 Nov 2004 09:42:34



The following bash script works fine to delete all files that are
older than $1 minutes. To execute this script,
bash cleanup +10

find /mypath -type f -cmin $1 -exec rm -f {} \;

Unfortunately, now I just realize I need to make it work in Korn
Shell. Even I
change the header from #!/bin/bash to #!/usr/bin/ksh, and execute the
script by ksh cleanup +10, it still have
different errors:

cleanup[9]: -cmin:  not found.
cleanup[10]: -type:  not found.

Looks like it doesn't work anymore. I think I need to re-write the
Korn Shell script that do the task.
I tried to search for equivalent Korn shell commands but unsuccessful.

Please help. thanks!!

 
 
 

Convert Bash shell script to Korn shell script

Post by William Par » Wed, 24 Nov 2004 11:12:50



> The following bash script works fine to delete all files that are
> older than $1 minutes. To execute this script,
> bash cleanup +10

> find /mypath -type f -cmin $1 -exec rm -f {} \;

> Unfortunately, now I just realize I need to make it work in Korn
> Shell. Even I
> change the header from #!/bin/bash to #!/usr/bin/ksh, and execute the
> script by ksh cleanup +10, it still have
> different errors:

> cleanup[9]: -cmin:  not found.
> cleanup[10]: -type:  not found.

> Looks like it doesn't work anymore. I think I need to re-write the
> Korn Shell script that do the task.
> I tried to search for equivalent Korn shell commands but unsuccessful.

> Please help. thanks!!

Try typing it out on your command-line.  I don't think this is Bash/Ksh
issue.

--

Linux solution for data management and processing.

 
 
 

Convert Bash shell script to Korn shell script

Post by rakesh shar » Wed, 24 Nov 2004 22:27:24



> The following bash script works fine to delete all files that are
> older than $1 minutes. To execute this script,
> bash cleanup +10

> find /mypath -type f -cmin $1 -exec rm -f {} \;

> Unfortunately, now I just realize I need to make it work in Korn
> Shell. Even I
> change the header from #!/bin/bash to #!/usr/bin/ksh, and execute the
> script by ksh cleanup +10, it still have
> different errors:

> cleanup[9]: -cmin:  not found.
> cleanup[10]: -type:  not found.

> Looks like it doesn't work anymore. I think I need to re-write the
> Korn Shell script that do the task.

change 'find' to '/bin/find'.

probably your ksh has 'find' aliased.

 
 
 

Convert Bash shell script to Korn shell script

Post by Mat » Thu, 25 Nov 2004 02:46:09


Quote:> change 'find' to '/bin/find'. probably your ksh has 'find' aliased.

I tried it, it still doesn't work. Let me make the descriptions more
clear.

Here's the bash script that worked in home's Linux before, and then I
put this script
to Unix, and no longer works with error "ksh: bash: not found." I
guess the
Unix environment setting doesn't support bash scripts?

#!/bin/bash
find /usr/local/opt/WebSphere/AppServer/installedApps/proj1App.ear/proj1.war/proj1/download
-type f -cmin $1 -exec rm -f {} \;

$ bash cleanup +5
ksh: bash: not found

I then replaced the header with #!/usr/bin/ksh, and it has another
error "-cmin is not a valid option."
#!/usr/bin/ksh
find /usr/local/opt/WebSphere/AppServer/installedApps/proj1App.ear/proj1.war/proj1/download
-type f -cmin $1 -exec rm -f {} \;

$ ksh cleanup +5
find: 0652-017 -cmin is not a valid option.

I don't want to change any setting in unix, but I have no idea what to
do now. I guess I
need to re-write the bash shell script to korn shell script?

Thanks again. Please advise.

 
 
 

Convert Bash shell script to Korn shell script

Post by Willia » Thu, 25 Nov 2004 03:14:32



Quote:

> I then replaced the header with #!/usr/bin/ksh, and it has another
> error "-cmin is not a valid option."
> #!/usr/bin/ksh
> find

/usr/local/opt/WebSphere/AppServer/installedApps/proj1App.ear/proj1.war/proj
1/download

Quote:> -type f -cmin $1 -exec rm -f {} \;

It isn't a valid option for the find on my machine, the only option it
supports is -ctime, which would do what you want. -Wm
 
 
 

Convert Bash shell script to Korn shell script

Post by Stephane CHAZELA » Thu, 25 Nov 2004 03:04:42


2004-11-23, 09:46(-08), Matt:
[...]
Quote:> I then replaced the header with #!/usr/bin/ksh, and it has another
> error "-cmin is not a valid option."
> #!/usr/bin/ksh
> find /usr/local/opt/WebSphere/AppServer/installedApps/proj1App.ear/proj1.war/proj1/download
> -type f -cmin $1 -exec rm -f {} \;

> $ ksh cleanup +5
> find: 0652-017 -cmin is not a valid option.

[...]

-cmin is a GNU (and BSD) specific option. There's no equivalent
option in standard find.

Note that -cmin is for the inode last change time, I doubt this
is what you want.

If you want to remove the files that have not been modified
withing the last $1 minutes, then you can use perl:

#! /usr/bin/perl
use File::Find;
$minutes = $ARGV[0];
find sub { unlink if (-M) * 24 * 60 > $minutes },
"/usr/local/opt/WebSphere/AppServer/installedApps/proj1App.ear/proj1.war/proj1/download"

(untested)

--
Stephane

 
 
 

Convert Bash shell script to Korn shell script

Post by Ed Morto » Thu, 25 Nov 2004 03:30:15



>>change 'find' to '/bin/find'. probably your ksh has 'find' aliased.

> I tried it, it still doesn't work. Let me make the descriptions more
> clear.

> Here's the bash script that worked in home's Linux before, and then I
> put this script
> to Unix, and no longer works with error "ksh: bash: not found." I
> guess the
> Unix environment setting doesn't support bash scripts?

Not necessarily, it just apparently isn't in "/bin" on your system. It
MAY be in "/usr/bin" (or elsewhere) in which case your shebang should be
"#!/usr/bin/bash" instead of "#!/bin/bash". That wouldn't solve your
main problem though. Your main problem has nothing whatsoever to do with
whether you're using a bash or ksh shell, it's which version of "find"
you're using as discussed elsethread.

<snip>

Quote:> I don't want to change any setting in unix, but I have no idea what to
> do now. I guess I
> need to re-write the bash shell script to korn shell script?

No, you need to download and install GNU "find".

        Ed.

 
 
 

Convert Bash shell script to Korn shell script

Post by dfre.. » Thu, 25 Nov 2004 04:39:16



> The following bash script works fine to delete all files that are
> older than $1 minutes. To execute this script,
> bash cleanup +10

> find /mypath -type f -cmin $1 -exec rm -f {} \;

> Unfortunately, now I just realize I need to make it work in Korn
> Shell. Even I
> change the header from #!/bin/bash to #!/usr/bin/ksh, and execute the
> script by ksh cleanup +10, it still have
> different errors:

> cleanup[9]: -cmin:  not found.
> cleanup[10]: -type:  not found.

> Looks like it doesn't work anymore. I think I need to re-write the
> Korn Shell script that do the task.
> I tried to search for equivalent Korn shell commands but
unsuccessful.

Do the following:
1. Start a bash shell: "bash"
2. run the command "which find"
3. exit the bash shell: "exit"
4. start a ksh: "ksh"
5. run the command "which find"
6. exit the ksh
7. use the find command identified in the bash shell as
the path to the find command in the ksh script.  The
path will probably be "/usr/local/bin/find"

--
Dana French

 
 
 

Convert Bash shell script to Korn shell script

Post by Bill Marcu » Thu, 25 Nov 2004 19:12:11


On Tue, 23 Nov 2004 12:14:32 -0600, William



>> I then replaced the header with #!/usr/bin/ksh, and it has another
>> error "-cmin is not a valid option."
>> #!/usr/bin/ksh
>> find
> /usr/local/opt/WebSphere/AppServer/installedApps/proj1App.ear/proj1.war/proj
> 1/download
>> -type f -cmin $1 -exec rm -f {} \;

> It isn't a valid option for the find on my machine, the only option it
> supports is -ctime, which would do what you want. -Wm

-[cma]time searches for files according to the age in days, whereas
-[cma]min uses minutes.  When the "min" options aren't available, you
can use touch and "find -newer" or "find ! -newer"

--
"At a scheduled time, the robot would pull the flush lever and scream as
it got sucked down the drain." --Kibo

 
 
 

Convert Bash shell script to Korn shell script

Post by Dan Merce » Fri, 26 Nov 2004 00:45:54


When I see problems like this I want to go back to first principles.
What are you really trying to do.  I will assume for now that you
want to run a script,  either from batch or cron,  to clean up a
temp directory.  Let's assume every ten minutes you run the following
script as root to clean up /usr/tmp of files beginning with sh:

    #!/usr/bin/ksh
    VARDIR==/var/${0##*/}
    TOUCHSTONE=$VARDIR/touchstone
    [[ -d $VARDIR ]] || mkdir $VARDIR ||
        {
        print -u2 "Fatal error - can't make $VARDIR"
        exit 1
        }

    trap '>$TOUCHSTONE' exit

    cd /usr/tmp

    for file in sh*
        do
        [[ -f $file ]] || continue
        [[ $file -ot $TOUCHSTONE ]] || continue
        rm -f $TOUCHSTONE
        done

This is portable to all ksh versions and does not require any
external command other than rm

Dan Mercer