Sponsored Content
Top Forums Shell Programming and Scripting Unable to print last line in Linux in while loop Post 303046032 by RudiC on Wednesday 22nd of April 2020 04:57:43 AM
Old 04-22-2020
The problem is not IFS's contents, which is used as a field separator between the individual fields. Your read is missing the line terminator, usually <LF> (= \n = 0x0A = ^J) which is not printfed. So read waits for it but runs into an EOF / EOT (as stdin terminates), discards the data read so far, and breaks out of the loop having unprocessed data in the variable(s) (Thanks to MadeInGermany's post#5 for mentioning that). You can
- add a linefeed <LF> to the printf command
- switch read to use a different line terminator (which still needs to be printfed). man bash:


Quote:
read
-d delim
The first character of delim is used to terminate the input line, rather than newline.
sea's proposal will set IFS to " ", "\", and "n", split input on any collection of those, and eliminate them from input. Set IFS like IFS=$' \n' to make it <space> and <line feed>.

Last edited by RudiC; 04-25-2020 at 02:59 PM..
 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

loop? print max column in each line for 800 files and merge

Hello, I have 800 or so files with 3 columns each and >10000 lines each. For each file and each line I would like to print the maximum column number for each line. Then I would like to 'paste' each of these files together (column-wise) so that the file with expression in label '_1' is the... (6 Replies)
Discussion started by: peanuts48
6 Replies

2. Shell Programming and Scripting

Unable to Print Count

Hi All, I am performing addition of two inetger variables which assigning output to a new variable getting following error. Check1.sh #!/bin/ksh filesrc=/usr/kk/Source1.txt filetgt=/usr/kk/Source2.txt FINAL_COUNTS= `awk '{n++} END {printf "%012d\n",n}' ${filesrc} ${filetgt}`... (3 Replies)
Discussion started by: kmsekhar
3 Replies

3. Shell Programming and Scripting

Unable to print dynamic arguments in for loop

Hi All, I want to pass few dynamic arguments to shell script. The number of arguments differ each time I call the script. I want to print the arguments using the for loop as below. But not working out. for (( i=1; i<=$#; i++ )) do echo $"($i)" done /bin/sh test.sh arg1 arg2 arg3 ... (1 Reply)
Discussion started by: laalesh
1 Replies

4. Shell Programming and Scripting

Print in New line in loop

Hi , i want to print the output in line by line while read LINE do echo $LINE | grep UCM | egrep '(Shutdown|Unavailable)' echo $LINE | grep SRBr | egrep '(Shutdown|Unavailable)' echo $LINE | grep SRP| egrep '(Shutdown|Unavailable)' echo $LINE | grep OM | grep JMS|... (7 Replies)
Discussion started by: navsan
7 Replies

5. Shell Programming and Scripting

Print awk output in same line ,For loop

My code is something like below. #/bin/bash for i in `ps -ef | grep pmon | grep -v bash | grep -v grep | grep -v perl | grep -v asm | grep -v MGMT|awk '{print $1" "$8}'` do echo $i ORACLE_SID=`echo $line | awk '{print $2}'` USERNAME=`echo $line | awk '{print $1}'` done ============= But... (3 Replies)
Discussion started by: tapia
3 Replies

6. Shell Programming and Scripting

Print loop output on same line dynamically

Hi, I am trying to print copy percentage completion dynamically by using the script below, #!/bin/bash dest_size=0 orig_size=`du -sk $sourcefile | awk '{print $1}'` while ; do dest_size=`du -sk $destfile | awk '{print $1}'` coyp_percentage=`echo "scale=2; $dest_size*100/$orig_size"... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

7. UNIX for Beginners Questions & Answers

Loop through directory and print on line

In the bash below I am trying to loop through all the R1.gz in a directory (always 1), store them in ARRAY, and cut them before the second _. That is being done but I can't seem to print then one a single line seperated by a space. Is the below the best way or is there a better solution? Thank you... (3 Replies)
Discussion started by: cmccabe
3 Replies

8. Shell Programming and Scripting

Unable to print python array in shell script loop.

I am unable to loop print a python string array in my unix shell script: ~/readarr.sh '{{ myarr }}' more readarr.sh echo "Parameter 1:"$1 MYARRAY= $1 IFS= MYARRAY=`python <<< "print ' '.join($MYARRAY)"` for a in "$MYARRAY"; do echo "Printing Array: $a" done Can you... (10 Replies)
Discussion started by: mohtashims
10 Replies

9. Shell Programming and Scripting

Echo print on same line while loop using variable

Currently using below script but echo it print the output in two line. Input file all-vm-final-2.txt CEALA08893 SDDC_SCUN DS_SIO_Workload_SAPUI_UAT_01 4 CEALA09546 SDDC_SCUN DS-SIO-PD5_Workload_UAT_SP1_Flash_07 4 CEALA09702 SDDC_SCUN DS-VSAN-RMP-WORKLOAD01 4 DEALA08762 SDDC_LDC... (3 Replies)
Discussion started by: ranjancom2000
3 Replies
READ(1P)						     POSIX Programmer's Manual							  READ(1P)

PROLOG
This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the correspond- ing Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux. NAME
read -- read a line from standard input SYNOPSIS
read [-r] var... DESCRIPTION
The read utility shall read a single line from standard input. By default, unless the -r option is specified, <backslash> shall act as an escape character. An unescaped <backslash> shall preserve the literal value of the following character, with the exception of a <newline>. If a <newline> follows the <backslash>, the read utility shall interpret this as line continuation. The <backslash> and <newline> shall be removed before splitting the input into fields. All other unescaped <backslash> characters shall be removed after splitting the input into fields. If standard input is a terminal device and the invoking shell is interactive, read shall prompt for a continuation line when it reads an input line ending with a <backslash> <newline>, unless the -r option is specified. The terminating <newline> (if any) shall be removed from the input and the results shall be split into fields as in the shell for the results of parameter expansion (see Section 2.6.5, Field Splitting); the first field shall be assigned to the first variable var, the sec- ond field to the second variable var, and so on. If there are fewer fields than there are var operands, the remaining vars shall be set to empty strings. If there are fewer vars than fields, the last var shall be set to a value comprising the following elements: * The field that corresponds to the last var in the normal assignment sequence described above * The delimiter(s) that follow the field corresponding to the last var * The remaining fields and their delimiters, with trailing IFS white space ignored The setting of variables specified by the var operands shall affect the current shell execution environment; see Section 2.12, Shell Execu- tion Environment. If it is called in a subshell or separate utility execution environment, such as one of the following: (read foo) nohup read ... find . -exec read ... ; it shall not affect the shell variables in the caller's environment. OPTIONS
The read utility shall conform to the Base Definitions volume of POSIX.1-2008, Section 12.2, Utility Syntax Guidelines. The following option is supported: -r Do not treat a <backslash> character in any special way. Consider each <backslash> to be part of the input line. OPERANDS
The following operand shall be supported: var The name of an existing or nonexisting shell variable. STDIN
The standard input shall be a text file. INPUT FILES
None. ENVIRONMENT VARIABLES
The following environment variables shall affect the execution of read: IFS Determine the internal field separators used to delimit fields; see Section 2.5.3, Shell Variables. LANG Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume of POSIX.1-2008, Section 8.2, Internationalization Variables for the precedence of internationalization variables used to determine the values of locale categories.) LC_ALL If set to a non-empty string value, override the values of all the other internationalization variables. LC_CTYPE Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as opposed to multi-byte characters in arguments). LC_MESSAGES Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error. NLSPATH Determine the location of message catalogs for the processing of LC_MESSAGES. PS2 Provide the prompt string that an interactive shell shall write to standard error when a line ending with a <backslash> <newline> is read and the -r option was not specified. ASYNCHRONOUS EVENTS
Default. STDOUT
Not used. STDERR
The standard error shall be used for diagnostic messages and prompts for continued input. OUTPUT FILES
None. EXTENDED DESCRIPTION
None. EXIT STATUS
The following exit values shall be returned: 0 Successful completion. >0 End-of-file was detected or an error occurred. CONSEQUENCES OF ERRORS
Default. The following sections are informative. APPLICATION USAGE
The -r option is included to enable read to subsume the purpose of the line utility, which is not included in POSIX.1-2008. EXAMPLES
The following command: while read -r xx yy do printf "%s %s $yy$xx" done < input_file prints a file with the first field of each line moved to the end of the line. RATIONALE
The read utility historically has been a shell built-in. It was separated off into its own utility to take advantage of the richer descrip- tion of functionality introduced by this volume of POSIX.1-2008. Since read affects the current shell execution environment, it is generally provided as a shell regular built-in. If it is called in a sub- shell or separate utility execution environment, such as one of the following: (read foo) nohup read ... find . -exec read ... ; it does not affect the shell variables in the environment of the caller. Although the standard input is required to be a text file, and therefore will always end with a <newline> (unless it is an empty file), the processing of continuation lines when the -r option is not used can result in the input not ending with a <newline>. This occurs if the last line of the input file ends with a <backslash> <newline>. It is for this reason that ``if any'' is used in ``The terminating <new- line> (if any) shall be removed from the input'' in the description. It is not a relaxation of the requirement for standard input to be a text file. FUTURE DIRECTIONS
None. SEE ALSO
Chapter 2, Shell Command Language The Base Definitions volume of POSIX.1-2008, Chapter 8, Environment Variables, Section 12.2, Utility Syntax Guidelines COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2013 Edition, Standard for Information Technol- ogy -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, Copyright (C) 2013 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. (This is POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Stan- dard is the referee document. The original Standard can be obtained online at http://www.unix.org/online.html . Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html . IEEE
/The Open Group 2013 READ(1P)
All times are GMT -4. The time now is 02:01 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy