Operating System - HP-UX
1748269 Members
3588 Online
108760 Solutions
New Discussion

Shell Script - For and IF issue

 
SOLVED
Go to solution
Fefers
Frequent Advisor

Shell Script - For and IF issue

Hello experts..

 

I'm trying to copy a file the same way that it is, but when it find a NULL character, must input another value.

 

eg (what I need).

 

File1.txt

80

 

80

80

 

File2.txt

80

100

80

80

 

Whats going on

 

Scipt:

 

for i in File1.txt ;
do
     if [ `cat File1.txt` == "" ]
     then
            echo "100" >> File2.txt
     else
            cat File1.txt >> File2.txt 
     fi
done

 

 

# cat File1.txt
80

 

80

80

 

# sh -x test.ksh

+ cat File1.txt
+ [ 80 80 80 == ]
test.ksh[5]: 80: 0403-012 A test command parameter is not valid.
+ cat File1.txt
+ 1>> File2.txt

 

#cat File2.txt
80


80

80

 

 

Could some help me please?

4 REPLIES 4
Dennis Handly
Acclaimed Contributor
Solution

Re: Shell Script - For and IF issue

If you want to read File1.txt and copy all but empty lines to File2.txt, then you need to use a while vs for loop:

while read line; do

   if [ "$line" = "" ]; then  # Note only one "="
      echo "100"
   else
      echo "$line"

   fi
done < File1.txt > File2.txt

Steven Schweda
Honored Contributor

Re: Shell Script - For and IF issue

 
Fefers
Frequent Advisor

Re: Shell Script - For and IF issue

Thanks Dennis and Steven!!

it worked!!
Dennis Handly
Acclaimed Contributor

Re: Shell Script - For and IF issue

>Thanks Dennis and Steven!!

 

If you are happy with your answers, please click on the Kudos stars of the helpful posts.