Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

Modify a script to work on bash shell rather than ksh

SOLVED
Go to Solution
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

 

1 ACCEPTED SOLUTIONS
Acclaimed Contributor Acclaimed Contributor

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.

16 REPLIES
Acclaimed Contributor 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).

 

 

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?

Acclaimed Contributor 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.

Honored Contributor 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
Acclaimed Contributor Acclaimed Contributor

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.

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.

Acclaimed Contributor 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?

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
Acclaimed Contributor 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?

Super Advisor

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

du returns: single digit numbers, and find returns: wc -l
Super Advisor

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

 

 

Now its working I have used the following:

 

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

 

Acclaimed Contributor Acclaimed Contributor

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

>du returns: single digit numbers

 

As I said, du(1) returns a line for every arg and subdirectory.

Super Advisor

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

its now working fine, I put in the cron to run every hour, but I what I would like now is to insert those figures in some king database or spreadsheet, to make statiscs on hourly and daily basis.

Acclaimed Contributor Acclaimed Contributor

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

>is to insert those figures in some kind database or spreadsheet, to make statistics on hourly and daily basis.

 

Well, you could create a CSV by inserting a "," between the name and count.

You probably also want to add a simple timestamp as another field:

echo "$(date +"%Y-%m-%d:%H:%M") ..."


Super Advisor

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

please I am not quite sure how to create a csv on that line, if I undestood correctly
Highlighted
Acclaimed Contributor Acclaimed Contributor

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

>I am not quite sure how to create a csv on that line,

 

Add a title line before your for-loop

echo "Date,Directory,File_count"

 

Then in the loop:

echo "$(date +"%Y-%m-%d:%H:%M"),$dir,$(find $dir -type d | wc -l)"