Re: Modify a script to work on bash shell rather than ksh (885 Views)
Reply
Super Advisor
NDO
Posts: 545
Registered: ‎12-10-2003
Message 1 of 17 (940 Views)
Accepted Solution

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

 

Acclaimed Contributor
Dennis Handly
Posts: 25,274
Registered: ‎03-06-2006
Message 2 of 17 (934 Views)

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
NDO
Posts: 545
Registered: ‎12-10-2003
Message 3 of 17 (930 Views)

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
Dennis Handly
Posts: 25,274
Registered: ‎03-06-2006
Message 4 of 17 (928 Views)

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
Laurent Menase
Posts: 1,079
Registered: ‎11-06-2003
Message 5 of 17 (917 Views)

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
Dennis Handly
Posts: 25,274
Registered: ‎03-06-2006
Message 6 of 17 (911 Views)

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
NDO
Posts: 545
Registered: ‎12-10-2003
Message 7 of 17 (904 Views)

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
Dennis Handly
Posts: 25,274
Registered: ‎03-06-2006
Message 8 of 17 (902 Views)

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
NDO
Posts: 545
Registered: ‎12-10-2003
Message 9 of 17 (901 Views)

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
Dennis Handly
Posts: 25,274
Registered: ‎03-06-2006
Message 10 of 17 (900 Views)

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
NDO
Posts: 545
Registered: ‎12-10-2003
Message 11 of 17 (898 Views)

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

du returns: single digit numbers, and find returns: wc -l
Super Advisor
NDO
Posts: 545
Registered: ‎12-10-2003
Message 12 of 17 (898 Views)

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
Dennis Handly
Posts: 25,274
Registered: ‎03-06-2006
Message 13 of 17 (896 Views)

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
NDO
Posts: 545
Registered: ‎12-10-2003
Message 14 of 17 (893 Views)

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
Dennis Handly
Posts: 25,274
Registered: ‎03-06-2006
Message 15 of 17 (888 Views)

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
NDO
Posts: 545
Registered: ‎12-10-2003
Message 16 of 17 (887 Views)

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
Acclaimed Contributor
Dennis Handly
Posts: 25,274
Registered: ‎03-06-2006
Message 17 of 17 (885 Views)

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)"

The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.