LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Please explain how this Read Command works (https://www.linuxquestions.org/questions/linux-newbie-8/please-explain-how-this-read-command-works-4175532191/)

murali2489 01-27-2015 01:29 AM

Please explain how this Read Command works
 
Hi All,

I am new to linux and I am finding difficulties in understanding the below commands.

Please explain what is happening in the below commands.

Code:

IFS="~"
exec 4< $BCP_DIR/cnts.data
while read -ru4 TableName Count CreationDateTime Type AccessDate
do
        . /apps/rmb/prod/scripts/load_datasum.ksh
done
IFS=" "
exec 4<&-


Please explain how this commands flows especially
exec 4< $BCP_DIR/cnts.data & exec 4<&-

allend 01-27-2015 03:07 AM

Please read the REDIRECTION section of 'man bash'.

rknichols 01-27-2015 08:44 AM

In fairness, there is some rather uncommon stuff in that script. Let's break it down:
Code:

IFS="~"
Change the internal field separator to "~". Presumably, the file to be read is using that character as a field separator.
Code:

exec 4< $BCP_DIR/cnts.data
Continue execution with the named file open for reading on (the otherwise unused) file descriptor 4.
Code:

while read -ru4 TableName Count CreationDateTime Type AccessDate
Execute a loop as long as the read command returns data. The read command will read from file descriptor 4 ("-u4" option), will treat backslash as an ordinary character ("-r" option), and will store the first 4 fields into variables TableName, Count, CreationDateTime, and Type, and the remainder of the line in variable AccessDate.
Code:

        . /apps/rmb/prod/scripts/load_datasum.ksh
Read and execute the commands from that file in the current shell.
Code:

IFS=" "
Restore the internal field separator. Something's not quite right there. IFS would normally contain the three characters <space>, <tab>, and <newline>.
Code:

exec 4<&-
Close file descriptor 4.

vincix 01-27-2015 01:14 PM

Quote:

Originally Posted by allend (Post 5307195)
Please read the REDIRECTION section of 'man bash'.

This is a very beautiful answer. Not atypical, though, but really beautiful. You might as well have pointed the middle finger, stating it more clearly that you won't make any attempt to help him in any way.


Normally, in order to restore IFS, another variable that takes the value of IFS should be declared.
Code:

oIFS=$IFS
$IFS="~"
... ... ...
$IFS=oIFS

In this way, you make sure that the value of IFS remains the same, right?

rknichols 01-27-2015 04:35 PM

Quote:

Originally Posted by vincix (Post 5307407)
Code:

oIFS=$IFS
$IFS="~"
... ... ...
$IFS=oIFS

In this way, you make sure that the value of IFS remains the same, right?

Correct, and that's what I always do. In the current case, though, we don't know what follows in that script. Perhaps the setting to just <space> was intentional. Or, maybe that script has gone through enough copying/pasting from a screen display that the other characters have just been lost.

And of course if the script ends at that point, there wasn't really any need to restore IFS at all.

allend 01-27-2015 06:15 PM

Quote:

You might as well have pointed the middle finger, stating it more clearly that you won't make any attempt to help him in any way.
My post #2 was addressed at the specifics of
Quote:

Please explain how this commands flows especially
exec 4< $BCP_DIR/cnts.data & exec 4<&-
There was insufficient detail in the initial post to know whether the code given comes from a real world situation or a contrived teaching exercise. You are not encouraged to do other people's homework at LQ.

vincix 01-27-2015 07:02 PM

Quote:

Originally Posted by allend (Post 5307516)
My post #2 was addressed at the specifics of

There was insufficient detail in the initial post to know whether the code given comes from a real world situation or a contrived teaching exercise. You are not encouraged to do other people's homework at LQ.

I agree, but doing someone's homework is the exact opposite of what you've written. There's room in-between, just saying... Not that it matters too much, as a lot of users here simply dismiss OPs with 'read the manual' without deigning to give the slightest explanation. Why would this forum even be used if users wouldn't want proper human explanations related exactly to their problems?

murali2489 01-28-2015 02:59 AM

Hi rknichols,

Thanks Very Much for the detailed explanation.

I now understood it clearly :D

Hi vincix,

Thanks for the suggestion .... ;)
Quote:

Originally Posted by allend View Post
My post #2 was addressed at the specifics of

There was insufficient detail in the initial post to know whether the code given comes from a real world situation or a contrived teaching exercise. You are not encouraged to do other people's homework at LQ.

This is a real world scenario, I just posted where im getting doubt.

The functioning of the script is to weekly load the bulk data coming from Downstream to MSSQL Database tables using this script. The files mentioned in the post contains the information of the counts of data being inserted.


I am new to Linux field, that's why Im facing difficulties in Understanding the commands.

Now I see this command in the same script .

Please explain what this below command does and also advise any book or online tutorial for learning Shell commands.

Code:

if [[ -f $BCP_DIR/clmcldt.data ]]
then
  print -n "loading ClosedDate " ;date
    ed - $BCP_DIR/clmcldt.data <<-[] >$LOG_DIR/load_clmcldt.edit.log
        H
        g/ /s///gp
        w
        q
[]


chrism01 01-29-2015 05:11 AM

Try reading these, and more importantly actually start writing small scripts; there's no substitute for experience - guides are just that.

http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

there are a million bash guides on the net ....

The only exotic bit of that script is calling the 'ed' editor as a cmd instead of interactively - an odd way to do it ...

allend 01-29-2015 05:58 AM

Actually the script is written for the Korn shell (ksh). Note the sourced file in post #1 (load_datasum.ksh) and the use of print in post #8.

The code is testing for the presence of a file $BCP_DIR/clmcldt.data. If the file exists, the ed editor is being used to remove all spaces in the file. The edits are being logged to the file $LOG_DIR/load_clmcldt.edit.log
The actual ed commands are:
H - Turn on descriptive errors
g/ /s///gp - Globally search for lines containing a space and replace with nothing for every occurrence of space in the line, printing the line when substitutions occur
w - write the resulting file
q - quit

murali2489 01-30-2015 01:00 AM

Thanks chrism01 and Allen for explanation ... !!


All times are GMT -5. The time now is 10:25 AM.