of a while pipeline in a Linux system.

Source: Internet
Author: User

Since the project was migrated from Aix to Redhat system, a previously written shell script did not run successfully, and after analysis, the key code was locked as follows:

readfilecontent () {Currentfile=$1fileshowrows=0filerealrows=0filebusinessdate=$2Filecountstarttime=0Filecountendtime=0  Echo$1  Echo$2  Cat$1| whileRead Line Do           if["$fileRealRows"-eq"0" ]       Then          Echo "$line"fileshowrows=`Echo$line |awk '{split ($0,a, "|"); Print A[1]}'' Filecountstarttime=`Echo$line |awk '{split ($0,a, "|"); Print A[2]}'' Filecountendtime=`Echo$line |awk '{split ($0,a, "|"); Print A[3]}'`          Echo "$fileShowRows"          Echo "$fileCountStartTime"          Echo "$fileCountEndTime"      fi      Echo 'filerealrows' "$fileRealRows"Let filerealrows+=1      Echo 'filerealrows' "$fileRealRows"   Done  Echo "$fileShowRows"  Echo "$fileRealRows"  Echo "$fileCountStartTime"  Echo "$fileCountEndTime"}

This code is used to parse a piece of text

1| 20170322000001| 20170322235959 1| 00206342| 020602| 37906| 20170322| 083934| 21205967090| 21205967090| | | 10002930|

One piece of code that uses the difference is to read the contents of the file as a while pipeline, and the results are as follows in Aix and Linux systems:

Aix Execution Results:201703221|20170322000001|2017032223595912017032200000120170322235959filerealrows0filerealrows1filerealrows1filerealrows2122017032200000120170322235959
Linux execution results:201703221|20170322000001|2017032223595912017032200000120170322235959filerealrows0filerealrows1filerealrows1filerealrows20000

As you can see from the execution results, the parent variable assigned to Linux in the child shell is not valid for the parent shell, and the AIX system is valid. Here's how to modify it

readfilecontent () {Currentfile=$1fileshowrows=0filerealrows=0filebusinessdate=$2Filecountstarttime=0Filecountendtime=0  Echo$1  Echo$2   while Read Line Do           if["$fileRealRows"-eq"0" ]       Then          Echo "$line"fileshowrows=`Echo$line |awk '{split ($0,a, "|"); Print A[1]}'' Filecountstarttime=`Echo$line |awk '{split ($0,a, "|"); Print A[2]}'' Filecountendtime=`Echo$line |awk '{split ($0,a, "|"); Print A[3]}'`          Echo "$fileShowRows"          Echo "$fileCountStartTime"          Echo "$fileCountEndTime"      fi      Echo 'filerealrows' "$fileRealRows"Let filerealrows+=1      Echo 'filerealrows' "$fileRealRows" Done  <$1   Echo "$fileShowRows"  Echo "$fileRealRows"  Echo "$fileCountStartTime"  Echo "$fileCountEndTime"}

of a while pipeline in a Linux system.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.