LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-30-2018, 04:29 PM   #1
Springs
Member
 
Registered: Apr 2008
Posts: 73

Rep: Reputation: 0
Bash variables - working with words with an apostrophe?


i'm having issues with a script that i'm working on.

Within the script i have 2 variables which are pulling txt from a file and writing it into a database.

my problem is that some of the words have a single apostrophe in them which causes my script to fail or write the literal variable name to the database. eg isn't / i'm / i've etc..

Any ideas on how i can work around it? I've tried single and double quotes and also searching google but a lot of results don't talk about actual words with an apostrophe.
 
Old 08-30-2018, 04:34 PM   #2
lougavulin
Member
 
Registered: Jul 2018
Distribution: Slackware,x86_64,current
Posts: 279

Rep: Reputation: 100Reputation: 100
For the databases I know, I think it is SQL ANSI, you can protect a single quote by adding another one : ' become '' (2 singles quotes)
 
1 members found this post helpful.
Old 08-30-2018, 04:49 PM   #3
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Can you paste the relevant portion of the script here?
 
1 members found this post helpful.
Old 08-30-2018, 05:04 PM   #4
lougavulin
Member
 
Registered: Jul 2018
Distribution: Slackware,x86_64,current
Posts: 279

Rep: Reputation: 100Reputation: 100
Sorry it seems I read your post to quickly for my first answer.

So I go with hydrurga.
 
Old 08-30-2018, 05:06 PM   #5
Springs
Member
 
Registered: Apr 2008
Posts: 73

Original Poster
Rep: Reputation: 0
var1=$(cat "$file" | cut -d "-" -f1)

echo "INSERT INTO database ("id", "var1", "var2") VALUES ("$id", "$var1","$var2");" | mysql -h 192.168.1.8 -P 3307 -u"$MYSQL_USER" -p"$MYSQL_PASSWORD" "$MYSQL_DATABASE"

this is the basic part of the script thats not working. both $var1 and $var2 get entered into the db literally as $var1 etc. as mentioned i've tried different quotes but can't get anything to take
 
Old 08-30-2018, 06:43 PM   #6
lougavulin
Member
 
Registered: Jul 2018
Distribution: Slackware,x86_64,current
Posts: 279

Rep: Reputation: 100Reputation: 100
Something like that ?
Code:
var1=$(cat "$file"| cut -d "-" -f1 | sed "s/'/''/g")

echo "INSERT INTO database (id, var1, var2) VALUES ('$id', '$var1','$var2');" | mysql -h 192.168.1.8 -P 3307 -u"$MYSQL_USER" -p"$MYSQL_PASSWORD" "$MYSQL_DATABASE"
 
1 members found this post helpful.
Old 08-31-2018, 01:19 AM   #7
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
since this is a shell script, shouldn't the ' be escaped like this: \' ? or maybe even \'\' ?
 
1 members found this post helpful.
Old 08-31-2018, 02:30 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Maybe it could be solved with little steps like creating a mysql-escaping function:
Code:
#!/usr/local/bin/bash

escape_for_mysql () {
    sed "s_[\\\\'\"]_\\\\&_g" <<<"$1"
}

test1 () {
    local V=$(escape_for_mysql "$1")
    printf 'escape[%s]=[%s]\n' "$1" "$V"
}

test1 'back\slash'
test1 'quote: "text"'
test1 "'s1'='s2'"
result:
Code:
escape[back\slash]=[back\\slash]
escape[quote: "text"]=[quote: \"text\"]
escape['s1'='s2']=[\'s1\'=\'s2\']
Having this you can write something like this:
Code:
cmd="$(printf "INSERT INTO table (\`id\`, \`var1\`, \`var2\`) VALUES ('%s', '%s', '%s');" \
 "$(escape_for_mysql "$id")" \
 "$(escape_for_mysql "$var1")" \
 "$(escape_for_mysql "$var2")")"

Last edited by NevemTeve; 08-31-2018 at 02:48 AM.
 
Old 09-01-2018, 05:46 AM   #9
Springs
Member
 
Registered: Apr 2008
Posts: 73

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by lougavulin View Post
Something like that ?
Code:
var1=$(cat "$file"| cut -d "-" -f1 | sed "s/'/''/g")

echo "INSERT INTO database (id, var1, var2) VALUES ('$id', '$var1','$var2');" | mysql -h 192.168.1.8 -P 3307 -u"$MYSQL_USER" -p"$MYSQL_PASSWORD" "$MYSQL_DATABASE"
tried this and it seems to have worked.

Going to run some more test data through to make sure but it looks good so far!
 
  


Reply



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 to find all placeholder variables in a file and replace will real variables spadez Programming 6 11-26-2013 12:24 PM
bash apostrophe ofer4 Programming 7 06-14-2012 07:05 PM
Python: pull words from a text, setting them as variables General Programming 2 03-14-2010 09:14 AM
Inserting apostrophe between a variable (bash) alexpacio Programming 3 12-09-2009 09:04 AM
exporting variables under bash isn't working! dtheorem Programming 6 03-11-2004 08:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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
Open Source Consulting | Domain Registration