bash & Ksh loop problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash & Ksh loop problem
# 1  
Old 04-08-2009
PHP bash & Ksh loop problem

hi
i was trying to optimize one script and i came across this problem .. i am putting some pseudo code here

Code:
$ >cat a.sh
ls | while read I
do
        i=$(($i + 1))
done
echo "total no of files : [$i]"

$ >ksh a.sh
total no of files : [14]

$ >bash a.sh
total no of files : []

why is bash not working ? if i use ksh to run the actual script in which arrays are used then it throws error run out of subscript ... so how can i make this thing work in bash ?
# 2  
Old 04-08-2009
Its a side-effect of using pipes. It forks off a new process, which has its own seperate variables, causing the value to be lost when the loop completes and the pipe closes. The values are only local to things behind the pipe, so try this:

Code:
ls | (  i=0
        while read I
        do
                i=$(($i + 1))
        done
        echo "total no of files : [$i]" )

You can avoid the pipe entirely by redirecting into a temp file:
Code:
ls > /tmp/$$-ls
i=0
while read I
do
        i=$((i+1))
done < /tmp/$$-ls
rm -f /tmp/$$-ls
echo "Total no of files: [$i]"

But there's probably security problems in depending on a temp file like that.

Also, you can count lines with the wc utility.
Code:
i=$(ls | wc -l)
echo $i

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to ksh problem

Hi all Below code works in bash but it is not working in ksh. enddate=`date -d "$enddate + $i day" "+%Y_%m_%d"` Please help me how it works in ksh Thanks (10 Replies)
Discussion started by: pmreddy
10 Replies

2. Shell Programming and Scripting

Problem exiting a WHILE loop in ksh

Hi I am having a problem exiting a WHILE loop. I am on a Sun server using ksh. I am running a Veritas Cluster Software (High Availablity) command to obtain a group status and grepping the command output for status "G" which means that the filesystem is frozen and therefore not available to... (3 Replies)
Discussion started by: bigbuk
3 Replies

3. Shell Programming and Scripting

Problem Using If & For loop in AWK Command

I am parsing file for the fields using awk command, first i check 26th field for two characters using substr function if it matches then using for loop on array i search 184th field for 4 chars if it matches then i print the required fields but on execution i get the error, please help...... (5 Replies)
Discussion started by: siramitsharma
5 Replies

4. Shell Programming and Scripting

Typeset conversion problem from ksh to bash

Hi, typeset -l sgf # all lowercase letters typeset -u SGF # all uppercase letters sgf=$1 SGF=$sgf these lines used in my scripts . It ran fine in ksh but when we convert this to bash it erroring out. I like to know what the use of typeset ?? Thanks & Regards kanagaraj (3 Replies)
Discussion started by: kanagaraj
3 Replies

5. Shell Programming and Scripting

Problem with for loop in bash

I'm trying to do a script where I want to see if all users home directories are only writable by owner. However, in my script I do not know how to implement the for loop so that all directories are checked. In mine, I am only checking the permissions for the first directory found. I do know that a... (3 Replies)
Discussion started by: detatchedd
3 Replies

6. Shell Programming and Scripting

Bash & Ksh

I am using a Linux machine and it's default shell is BASH . I have korn shell script and inside it's setting environment variables. But after execuitng the script those env values are not holding the values . If I run the script outside of that ksh script with source a.sh , it's able to hold to... (2 Replies)
Discussion started by: talashil
2 Replies

7. Shell Programming and Scripting

Bash & Ksh

I am using a Linux machine and it's default shell is BASH . I have korn shell script and inside it's setting environment variables. But after execuitng the script those env values are not holding the values . If I run the script outside of that ksh script with source a.sh , it's able to hold to... (1 Reply)
Discussion started by: talashil
1 Replies

8. UNIX for Advanced & Expert Users

echo in ksh sh & bash

Hello, I have lib file which contain a function that get text to print on screen by echo command. Several scripts are inculde this lib and use this function. Each one of them is written in different shell language (sh ksh & bash). This causing some issues when using backslash charater as... (4 Replies)
Discussion started by: Alalush
4 Replies

9. Shell Programming and Scripting

bash and ksh: variable lost in loop in bash?

Hi, I use AIX (ksh) and Linux (bash) servers. I'm trying to do scripts to will run in both ksh and bash, and most of the time it works. But this time I don't get it in bash (I'm more familar in ksh). The goal of my script if to read a "config file" (like "ini" file), and make various report.... (2 Replies)
Discussion started by: estienne
2 Replies

10. Shell Programming and Scripting

Bash while loop problem

Hi, I'm having a problem with the while loop in bash. I try the following script: #!/bin/bash while true do echo "test" done When I try this, it gives me this error: while: Too few arguments. What am I doing wrong? Thanks (5 Replies)
Discussion started by: Kweekwom
5 Replies
Login or Register to Ask a Question