Operating System - HP-UX
1748181 Members
4207 Online
108759 Solutions
New Discussion

Modify a script to work on bash shell rather than ksh

 
SOLVED
Go to solution
NDO
Super Advisor

Modify a script to work on bash shell rather than ksh

 

 

Hi

 

what needs to be taken into account when writing for bash? I have a small script that does not work on bash, but is does if I type first ksh on comand prompt

#!/bin/ksh
for dir in directory1/subdirectory /directory2/subdirectory
/directory3/subdirectory
do
echo "$dir $( du $dir |wc -l)"
done > ./message1
mailx -s "number of files" fretagi@mcel.co.mz < ./message1

 

16 REPLIES 16
Dennis Handly
Acclaimed Contributor

Re: Modify a script to work on bash shell rather than ksh

Why would you want to port from a real shell to bash?

If this is because of Linux, you are in the wrong board.

 

What happens if you just use bash on that fragment?

 

>echo "$dir $(du $dir | wc -l)"

 

If you want the number of files in $dir, you shouldn't be using du(1).

 

 

NDO
Super Advisor

Re: Modify a script to work on bash shell rather than ksh

Hi

 

I had originaly had:

 

a=`/directory/subdirectory/ | wc -l`
echo "/directory/subdirectory :$a"
b=`/another_dir/subdir/ | wc -l`
echo "/another_dir/subdir :$b"

 I wanted to

while

 But a friend told me to use

du

 So, can you advise?

Dennis Handly
Acclaimed Contributor

Re: Modify a script to work on bash shell rather than ksh

>I had originally had:

 

Your archaic `` needs a command:

dir=/directory/subdirectory
echo "$dir: $(ls $dir | wc -l)"

dir=/another_dir/subdir
echo "$dir: $(ls $dir | wc -l)"

 

>But a friend told me to use du:

 

A ls(1) or find would work better.

Laurent Menase
Honored Contributor

Re: Modify a script to work on bash shell rather than ksh

First

#!/usr/bin/ksh and not #!/bin/ksh

 

no need for echo "$dir $(....

 

Then

#!/usr/bin/ksh
for dir in directory1/subdirectory /directory2/subdirectory
/directory3/subdirectory
do
echo "$dir \c"
find "$dir" | wc -l
done > ./message1 mailx -s "number of files" fretagi@mcel.co.mz < ./message1
Dennis Handly
Acclaimed Contributor
Solution

Re: Modify a script to work on bash shell rather than ksh

Perhaps I'm confused.  Did you want to port the script to bash or run it under a bash shell?

I found the script works without changes under bash.  And the #!  line should enable it to work under bash.

 

It looks like the du - wc pipeline counts directories not files so you could do this:

   echo "$dir $(find $dir -type d | wc -l)"

 

>#!/usr/bin/ksh and not #!/bin/ksh

 

If the permanent transition links are there, it should work.

NDO
Super Advisor

Re: Modify a script to work on bash shell rather than ksh

Hi

 

I want to run under the  bash shell, and for some strange reason its printing only single digits, but there 3 digits number there.

Dennis Handly
Acclaimed Contributor

Re: Modify a script to work on bash shell rather than ksh

>it's printing only single digits, but there 3 digits number there.

 

Can you provide the output of the script?

And what do you want the script to do with each directory?

Count the number of files or only subdirectories?

NDO
Super Advisor

Re: Modify a script to work on bash shell rather than ksh

The script is to only count the number of files in the subdirectories:
the output is:
/nikira/NIKIRAROOT/RangerData/DiamondTemp/Mcel_Ericsson_CDR_Temp 1
/nikira/NIKIRAROOT/RangerData/DiamondTemp/Mcel_Ericsson_SGSN_Temp 2
/nikira/NIKIRAROOT/RangerData/DiamondTemp/Mcel_Ericsson_GGSNCDR_Temp 2
Dennis Handly
Acclaimed Contributor

Re: Modify a script to work on bash shell rather than ksh

>The script is to only count the number of files in the subdirectories:

 

And are you using du(1), ls(1) or find?