LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   why this code is not working? (https://www.linuxquestions.org/questions/linux-newbie-8/why-this-code-is-not-working-4175460231/)

sysmicuser 04-30-2013 10:57 PM

why this code is not working?
 
Code:

export input_config_file=/app/oracle/scripts/integration_file_monitoring/config/input_properties.file
Sample of properties file
Code:

/data/integration/FIPaymentGateway/processBankCollectedTransactions/input=30
/data/integration/FIPaymentGateway/notifyDirectDebitRefundRequest/input=30

And definitely this is not working.

Code:

cat ${input_config_line}|while read line
        do
        rec=$(echo $line|cut -d'=' -f1)
        rec2=$(echo $line|cut -d'=' -f2)
        find ${rec} -type f -not -wholename "*/restore/*" -mmin +${rec2} -print
        done


shivaa 05-01-2013 12:14 AM

Just try:
Code:

#!/bin/bash
while read -r line
do
rec=$(echo $line|cut -d'=' -f1)
rec2=$(echo $line|cut -d'=' -f2)
find $rec -type f -not -wholename "*/restore/*" -mmin +$rec2 -print
done < $input_config_line

If you still face pb, then add debug mode i.e. set -xv in your script and invoke it again to see where it goes wrong.

sysmicuser 05-01-2013 04:41 PM

@shivaa

Were you around when I was developing solution? :) I wrote exactly the same way you have given here and it did worked.

I was however wonder if I do the same with for loop. It was good if yu can help me with clearing confusion of when to use ${}, $(). Adding confusion..

millgates 05-01-2013 05:04 PM

$( ) is command substitution. You use it whenever you want to capture standard output of a program.
Examples:

Code:

current_directory="$( pwd )"
line="$(grep "$pattern" file)"
echo "My name is $(whoami)"

${ } is parameter expansion. You use it whenever the name of the variable would be ambiguous or when you use string manipulations:

Code:

for ((n=1; n<=10;n++)); do
    echo "This is the ${n}th line"
done

string="abcde"
upper_case="${string^^}"

To summarize: If it is a command, put it inside $( ), if it is a variable, it belongs to ${ }

suicidaleggroll 05-01-2013 05:38 PM

What doesn't work about your first script? I don't see anything obviously wrong with it, except that you're exporting "input_config_file" and then referencing "input_config_line".

grail 05-01-2013 07:23 PM

Also the extra use of cut not required either, just set IFS for read to '=' and use to variables in read.

sysmicuser 05-02-2013 07:59 AM

@grail.

Can you explain me a bit more on how not to use "cut" and use IFS?

pan64 05-02-2013 08:32 AM

definitely it is working, otherwise you should report a bug, but bash is ok. Just it is not the program you need. Try to understand the code written and try to find out what will it really do and what do you really need.... Try to simplify your code and add comments about its functionality

instead of this:
rec=$(echo $line|cut -d'=' -f1)
rec2=$(echo $line|cut -d'=' -f2)
rec=${line%%=*}
rec2=${line#*=}
would be better and quicker.
You will not need { } in find as it was already mentioned and do not mix input_config_line and input_config_file.

sysmicuser 05-02-2013 08:41 AM

@pan64

May I please request to explain me the magic which you have written?

sysmicuser 05-02-2013 08:49 AM

@grail

Code:

while IFS=',' read line; do echo $1; echo $2; done<${input_config_file}
I made up some sample input_config_file which is as follows:

Code:

/h1/h2/h3/h4/done,400,100,T
/h1/h2/h3/h4/done,460,100,T

How you would use IFS to extract individual strings
meaning
/h1/h2/h3/h4/done 400 100 T?

Lot of manipulation can be done on that !

@pan64

Same question goes for you too. The technique which you have given, I could not understand , please tell me how can I extract individual strings as I asked above.

Guys,

This is now becoming very interesting ! Thank you all for your post!

grail 05-02-2013 09:38 AM

Well your second example would be more suited to an array, but if we look at the original question:
Code:

while IFS='=' read -r rec rec2
do
  find "$rec" -type f -not -wholename "*/restore/*" -mmin +"$rec2" -print
done < "$input_config_line"


sysmicuser 05-02-2013 04:56 PM

@grail

But if we want to read 3 string and don't want to read second string then in that case would it be not waste to read second string?
Code:

while IFS='=' read -r rec rec2 rec3
do
  find "$rec" -type f -not -wholename "*/restore/*" -mmin +"$rec3" -print
done < "$input_config_line"

So in above case since IFS acts as a default delimiter we need to read rec2 so that we can read rec3? so I think we are wasting memory by reading something but not utilizing it.

sysmicuser 05-02-2013 06:35 PM

@pan64

Would love to hear from you , about my questions !

Thanks in advance.

grail 05-02-2013 07:24 PM

I am not following your logic? You are reading a single line and it is being split into as many values as you like. being that the split is done a single time on the invocation of
read it would be using less memory than to read the single line and call external commands to perform your splitting as many times as you need. Your original solution requires you to
call cut for each value you wish to receive. The current version requires the one read, which you are also still doing in your current scenario on top the cuts, and performs all splits as
indicated by the number of variables (or an array) that you pass to read.

chrism01 05-02-2013 08:10 PM

Re ignoring certain fields, using IFS
Code:

input="a,b,c,d"
#while IFS=',' read -r f1 f2 f3 f4  # read all
while IFS=',' read -r f1 _ _ f4    # throw away f2 & f3
do
    echo $f1 $f2 $f3 $f4
done< <(echo $input)

# output is
a d



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