LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-30-2013, 10:57 PM   #1
sysmicuser
Member
 
Registered: Mar 2010
Posts: 353

Rep: Reputation: 0
Question 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
 
Old 05-01-2013, 12:14 AM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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.
 
Old 05-01-2013, 04:41 PM   #3
sysmicuser
Member
 
Registered: Mar 2010
Posts: 353

Original Poster
Rep: Reputation: 0
@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..

Last edited by sysmicuser; 05-01-2013 at 04:42 PM.
 
Old 05-01-2013, 05:04 PM   #4
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 840

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
$( ) 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 ${ }
 
Old 05-01-2013, 05:38 PM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,482

Rep: Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089
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".
 
Old 05-01-2013, 07:23 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,505

Rep: Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890
Also the extra use of cut not required either, just set IFS for read to '=' and use to variables in read.
 
Old 05-02-2013, 07:59 AM   #7
sysmicuser
Member
 
Registered: Mar 2010
Posts: 353

Original Poster
Rep: Reputation: 0
@grail.

Can you explain me a bit more on how not to use "cut" and use IFS?
 
Old 05-02-2013, 08:32 AM   #8
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,348

Rep: Reputation: 2747Reputation: 2747Reputation: 2747Reputation: 2747Reputation: 2747Reputation: 2747Reputation: 2747Reputation: 2747Reputation: 2747Reputation: 2747Reputation: 2747
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.
 
1 members found this post helpful.
Old 05-02-2013, 08:41 AM   #9
sysmicuser
Member
 
Registered: Mar 2010
Posts: 353

Original Poster
Rep: Reputation: 0
@pan64

May I please request to explain me the magic which you have written?
 
Old 05-02-2013, 08:49 AM   #10
sysmicuser
Member
 
Registered: Mar 2010
Posts: 353

Original Poster
Rep: Reputation: 0
@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!
 
Old 05-02-2013, 09:38 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,505

Rep: Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890
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"
 
1 members found this post helpful.
Old 05-02-2013, 04:56 PM   #12
sysmicuser
Member
 
Registered: Mar 2010
Posts: 353

Original Poster
Rep: Reputation: 0
Question

@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.
 
Old 05-02-2013, 06:35 PM   #13
sysmicuser
Member
 
Registered: Mar 2010
Posts: 353

Original Poster
Rep: Reputation: 0
@pan64

Would love to hear from you , about my questions !

Thanks in advance.
 
Old 05-02-2013, 07:24 PM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,505

Rep: Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890
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.
 
Old 05-02-2013, 08:10 PM   #15
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,362

Rep: Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377
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
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LaTeX Table code not working, even example code from book. This is my username Programming 1 12-31-2012 12:00 PM
[SOLVED] Two pieces of code which do the same, one not working? ThatPerson Programming 7 12-11-2011 09:01 AM
code not working namita m Linux - Newbie 4 11-01-2010 02:35 PM
html code not working.... linuxlover.chaitanya Linux - Newbie 16 02-12-2009 01:30 AM
SED - minor changes work - Larger doesn't (working and non working code included) Nimoy Programming 17 09-22-2007 04:34 PM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration