LinuxQuestions.org
Register a domain and help support LQ
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 03-04-2010, 08:51 AM   #16
kernel-P4N1C
Member
 
Registered: Nov 2007
Location: Panama city, Republic of Panmaa
Posts: 167

Original Poster
Rep: Reputation: 18

same error chrism01


What does this do ?
N=$((N+1)) increase the line number... and actually doesn't do anything... dunno why i left it there.

ok this script function as follows.
the codes are on a text file... it reads the file one by one and crop the reading from 16:27 VALUE=${LINE:16:27}
then it is supposed to check that on a table name accesscodes that have the following structure
Code:
+-------------+-------------+------+-----+---------------------+-------+
| Field       | Type        | Null | Key | Default             | Extra |
+-------------+-------------+------+-----+---------------------+-------+
| id          | bigint(20)  | NO   | PRI | 0                   |       |
| accesscode  | varchar(50) | NO   |     |                     |       |
| clientid    | bigint(20)  | YES  |     | NULL                |       |
| createtime  | datetime    | NO   |     | 0000-00-00 00:00:00 |       |
+-------------+-------------+------+-----+---------------------+-------+
what i'm doing is basically checking if the codes has been imported on the db... if not then it is supposed to do something but i haven't get that far since i'm stuck with this issue.
 
Old 03-04-2010, 02:59 PM   #17
kernel-P4N1C
Member
 
Registered: Nov 2007
Location: Panama city, Republic of Panmaa
Posts: 167

Original Poster
Rep: Reputation: 18
ok so i guess everybody gave up
 
Old 03-04-2010, 03:22 PM   #18
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 249Reputation: 249Reputation: 249
I can't see where you told the while loop what file to work on.
I also don't understand the while condition.

So Yes, I've given up.

$value could be coming from anywhere.
 
Old 03-04-2010, 04:48 PM   #19
kernel-P4N1C
Member
 
Registered: Nov 2007
Location: Panama city, Republic of Panmaa
Posts: 167

Original Poster
Rep: Reputation: 18
the loop looks for a file here

done<$folder$file

I've modified the loop to look like:
Code:
while IFS= read -r LINE ; do
        x=${LINE:16:27} # cropping the record and leaving access code
dbexist=`mysql -h$mysqlhost -u$mysqluser -p$mysqlpass -e "SELECT id FROM dbname.accesscodes WHERE accesscode='"$x"'"`
done<$folder$file
this basically read the $file line by line, crop the data on the record to pick the actual access code and run the query.
N is no longer needed as i'm not counting the number of lines here..

note:
i've replce $x by $y on the query and asigned a value to y above... works fine; somehow is not picking up the value of x... i've print out x (as seen on older version) and x value comes out fine [i've repalace $VALUE for $x]
 
Old 03-04-2010, 05:06 PM   #20
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 249Reputation: 249Reputation: 249
I think you only need one set of quotes around $x (or $y whatever it is now)
Which kind is open to debate, try both types by themselves.

In that loop you are setting IFS to the default of splitting on spaces and whitespace.

Is that what you need ?
 
Old 03-04-2010, 09:17 PM   #21
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
You can see from my example that you don't need all those various quote marks.
As a test from the cmd line, just run the bare line like I did to prove that you can communicate with the DB ok.
Then, just add var=$(stuff_from-test) eg

Code:
accesscode=<number>
dbexist=$(mysql -h$mysqlhost -u$mysqluser -p$mysqlpass -e "SELECT id FROM dbname.accesscodes WHERE accesscode=$x")
echo $dbexist
Just try the lines above; use absolute vars for the test for host/user/passwd.
Let's show that the db connect/select works first before trying the rest of your code.

The way to test the whole script is to add

set -xv

just under the
#!/bin/bash

line.


If you have issues, don't just say 'same error'
Post entire cmd & err msgs; please try the short/manual version first.

Last edited by chrism01; 03-05-2010 at 01:24 AM.
 
Old 03-04-2010, 11:27 PM   #22
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by smoker View Post
In that loop you are setting IFS to the default of splitting on spaces and whitespace.
That is not correct. IFS= puts an empty string in IFS so it has no characters for use when parsing a string into words. Unsetting IFS makes Bash treat it as if it were set to the default value.

Here's a demonstration
Code:
IFS= read var rest << EOF
word1 word2
EOF
echo "var is '$var', rest is '$rest'"

unset var rest

unset IFS
read var rest << EOF
word1 word2
EOF
echo "var is '$var', rest is '$rest'"
The output is
Code:
var is 'word1 word2', rest is ''
var is 'word1', rest is 'word2'
 
Old 03-05-2010, 08:06 AM   #23
kernel-P4N1C
Member
 
Registered: Nov 2007
Location: Panama city, Republic of Panmaa
Posts: 167

Original Poster
Rep: Reputation: 18
Not working...
output
Code:
ERROR 1054 (42S22) at line 1: Unknown column '27060022900' in 'where clause'
dbexist
anyways, if i do it like this
Code:
dbexist=$(mysql -h$mysqlhost -u$mysqluser -p$mysqlpass -e "SELECT id FROM shop.accesscodes WHERE accesscode='$x'")
it talks to database fine
output:
Code:
dbexist id 6842

as i said; the single quote have something to do here.


Quote:
Originally Posted by chrism01 View Post
You can see from my example that you don't need all those various quote marks.
As a test from the cmd line, just run the bare line like I did to prove that you can communicate with the DB ok.
Then, just add var=$(stuff_from-test) eg

Code:
accesscode=<number>
dbexist=$(mysql -h$mysqlhost -u$mysqluser -p$mysqlpass -e "SELECT id FROM dbname.accesscodes WHERE accesscode=$x")
echo $dbexist
Just try the lines above; use absolute vars for the test for host/user/passwd.
Let's show that the db connect/select works first before trying the rest of your code.

The way to test the whole script is to add

set -xv

just under the
#!/bin/bash

line.


If you have issues, don't just say 'same error'
Post entire cmd & err msgs; please try the short/manual version first.
 
Old 03-05-2010, 08:58 AM   #24
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 249Reputation: 249Reputation: 249
Quote:
Originally Posted by catkin View Post
That is not correct. IFS= puts an empty string in IFS so it has no characters for use when parsing a string into words. Unsetting IFS makes Bash treat it as if it were set to the default value.
What is the default value ?

I was trying to work out what was going on from this:
http://tldp.org/LDP/abs/html/x17304.html#MAILBOXGREP

And as we have never seen the text file the OP is trying to read from, I thought maybe he's picking up some extra fluff and passing it to the sql query.

If his coordinates are wrong for the text he's extracting, he could be capturing a newline char and putting that in the query.

Those coordinates can be affected by whitespace, length of expected terms, whether it splits on spaces or a character etc.

As seen by his last test, a direct assignment of $x doesn't throw a mysql error. So it must be his coded assignment of $x.

I was going to suggest Chris's test next. It's been done. Now the OP needs to sanitise his input.


I would have used chomp in perl, I don't know the equivalent bash function.
 
Old 03-05-2010, 11:27 AM   #25
kernel-P4N1C
Member
 
Registered: Nov 2007
Location: Panama city, Republic of Panmaa
Posts: 167

Original Poster
Rep: Reputation: 18
The typical file is something like:
Code:
 xxxxxxxxxxxxxxx123456789AB
 xxxxxxxxxxxxxxx123456789AB
1EOF 2
EOF 2 <--- basically it's a counter of the lines
the xxxx means numbers that i don't care; the data is basically the last 11 characters
 
Old 03-05-2010, 11:47 AM   #26
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 249Reputation: 249Reputation: 249
Quote:
Originally Posted by kernel-P4N1C View Post
The typical file is something like:
Code:
 xxxxxxxxxxxxxxx123456789AB
 xxxxxxxxxxxxxxx123456789AB
1EOF 2
EOF 2 <--- basically it's a counter of the lines
the xxxx means numbers that i don't care; the data is basically the last 11 characters
Why does your code extract up to the 27th character then ?

Code:
x=${LINE:16:27} # cropping the record and leaving access code
Are the coordinates zero based or start from 1 ?
Are they inclusive ?
Because char 28 is a newline.
char 1 (or 0 depending on the indexing) is a space.
So why start at 16 ?

Last edited by smoker; 03-05-2010 at 11:49 AM.
 
Old 03-05-2010, 12:16 PM   #27
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by smoker View Post
What is the default value ?

I was trying to work out what was going on from this:
http://tldp.org/LDP/abs/html/x17304.html#MAILBOXGREP

And as we have never seen the text file the OP is trying to read from, I thought maybe he's picking up some extra fluff and passing it to the sql query.

If his coordinates are wrong for the text he's extracting, he could be capturing a newline char and putting that in the query.

[snip]

I would have used chomp in perl, I don't know the equivalent bash function.
The default value of $IFS is <space><tab><newline> (implied in GNU Bash Reference, Word Splitting).

The effect of IFS= in the ABS example you linked is to prevent any word splitting. From the "Word Splitting" link above: "If the value of IFS is null, no word splitting occurs".

Bash's read command will anyway terminate at newline so he cannot be capturing a newline char.

If I knew perl I might be able to tell you of bash's equivalent of chomp!
 
Old 03-05-2010, 12:23 PM   #28
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 249Reputation: 249Reputation: 249
@catkin
Oh, ok. I was confused by the comment he'd put under that line, i.e. :
Quote:
while IFS= read -r mail
# ^^^^ Reset $IFS.
# Otherwise "read" will strip leading & trailing space from its input.
I was confused between reset and unset.

I didn't know that about bashs read command.
Quote:
# chomp( LIST )
# chomp

This safer version of "chop" removes any trailing string that corresponds to the current value of $/ (also known as $INPUT_RECORD_SEPARATOR in the English module). It returns the total number of characters removed from all its arguments. It's often used to remove the newline from the end of an input record when you're worried that the final record may be missing its newline. When in paragraph mode ($/ = "" ), it removes all trailing newlines from the string. When in slurp mode ($/ = undef ) or fixed-length record mode ($/ is a reference to an integer or the like, see perlvar) chomp() won't remove anything. If VARIABLE is omitted, it chomps $_ .
 
Old 03-05-2010, 12:43 PM   #29
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by smoker View Post
@catkin
Oh, ok. I was confused by the comment he'd put under that line, i.e. :

I was confused between reset and unset.

I didn't know that about bashs read command.
The comment says one thing, the code does another! IFS is neither being reset (not meaningful in bash-speak) nor unset; it is being set to the empty string a.k.a "null".

Thanks for the chomp info. Bash has nothing as sophisticated as that. read and IFS can go some way toward it; parameter expansion can trim off leading and trailing whitespace.
 
Old 03-05-2010, 01:29 PM   #30
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 249Reputation: 249Reputation: 249
Quote:
Originally Posted by catkin View Post
The comment says one thing, the code does another! IFS is neither being reset (not meaningful in bash-speak) nor unset; it is being set to the empty string a.k.a "null".
Ok, I thought its being on tldp would mean it was actually reliable. Silly really.
Thanks for the linked info anyway, I really ought to learn more bash, but as I mainly script for the web (if at all), perl suits me better.

cheers
 
  


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
Bash script thats checks for faild login attempts. k1piee Programming 4 02-11-2009 10:46 PM
Script that checks for new files in a folder achtung_linux Programming 7 02-03-2007 10:18 AM
Script which checks Disk Volume - - on FC5 bskrakes Linux - General 1 10-11-2006 02:27 PM
shell script that checks for existence of files Rotwang Linux - General 3 12-02-2005 03:11 PM
Script that checks mapsize objorkum Linux - Software 1 07-06-2005 12:42 PM


All times are GMT -5. The time now is 03:27 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