jmituzas
jmituzas

Reputation: 603

Bash write to file (variables?)

OK trying to write a SQL query to a text file. I have tried cat, and echo no luck.

Heres what I would like to write to the file:

   USE vsftpd;
    CREATE TABLE `accounts` (
    `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    `username` VARCHAR( 30 ) NOT NULL ,
    `pass` VARCHAR( 50 ) NOT NULL ,
    UNIQUE (
    `username`
    )

) ENGINE = MYISAM ;

quit;

this is what I get when using cat or echo (maybe I need some kind of switch?):

USE vsftpd;
CREATE TABLE  (
uid=0(root) gid=0(root) groups=0(root) INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
 VARCHAR( 30 ) NOT NULL ,
 VARCHAR( 50 ) NOT NULL ,
UNIQUE (

)
) ENGINE = MYISAM ;

quit;

Upvotes: 0

Views: 3173

Answers (3)

jmituzas
jmituzas

Reputation: 603

anyways guess I can post the whole thing now in case someone else out there uses vsftpd and you helped me out. Heres the script for vsftpd with mysql for virtual users.Everything here except creating new users which you can figure out how to write that from what is given:

#!/bin/bash

while [[ "$yn" != "Yes" && "$yn" != "Y" && "$yn" != "y" && "$yn" != "yes" ]]; do
echo "name for your root password for MySQL"
read rpsql
echo "name for your primary FTP user"
read puftp
echo "what's you ftp password?"
read pftp
echo "what's the IPaddress of this server?"
read fip
echo "What is the Hostname of this server?" 
read fhost
    echo "You have entered $rpsql as your MySQL password"
    echo "You have entered $puftp as your FTP user"
    echo "You have entered $pftp as your Primary FTP password"
    echo "You have entered $fip as your IP address"
    echo "You have  entered $fhost as your hostname"
    echo "Are all of these correct? (Yes or No)"
      read yn
done

echo '######################################################################'
echo '##### Installing, configuring, and creating FTP Users and Shares #####'
echo '######################################################################'

apt-get install vsftpd libpam-mysql mysql-client phpmyadmin

mysqladmin -h nexwrxdemo.com -u root password $rpsql

echo 'CREATE DATABASE vsftpd;
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP ON vsftpd.* TO '$puftp'@'localhost' IDENTIFIED BY '$pftp';
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP ON vsftpd.* TO '$puftp'@'localhost.localdomain' IDENTIFIED BY '$pftp';
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP ON vsftpd.* TO '$puftp'@'$fip' IDENTIFIED BY '$pftp';
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP ON vsftpd.* TO '$puftp'@'$fhost' IDENTIFIED BY '$pftp';
FLUSH PRIVILEGES;

USE vsftpd;
    CREATE TABLE `accounts` (
    `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    `username` VARCHAR( 30 ) NOT NULL ,
    `pass` VARCHAR( 50 ) NOT NULL ,
    UNIQUE (
    `username`
    )

) ENGINE = MYISAM ;

quit; ' > vsftpd.db

echo "granting permissions to 'vsftpd' on database 'vsftpd'"

mysql -u root -p$rpsql < vsftpd.db 

echo "creating a non-privileged user called 'vsftp' (with the homedir /home/vsftpd) belonging to the group 'nogroup'"

useradd --home /home/$puftp --gid nogroup -m --shell /bin/false $puftp

echo "configuring vsftpd"
echo "backing up vsftpd config file"

cp -r -f -p /etc/vsftpd.conf /backups/
cat /dev/null > /etc/vsftpd.conf

echo 'listen=YES
anonymous_enable=NO
local_enable=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
nopriv_user=vsftpd
chroot_local_user=YES
secure_chroot_dir=/var/run/vsftpd
pam_service_name=vsftpd
rsa_cert_file=/etc/ssl/certs/vsftpd.pem
guest_enable=YES
guest_username=vsftpd
local_root=/home/vsftpd/$USER
user_sub_token=$USER
virtual_use_local_privs=YES
user_config_dir=/etc/vsftpd_user_conf
' > /etc/vsftpd.conf

mkdir /etc/vsftpd_user_conf

echo "backing up pam/vsftpd config file"

cp -r -f -p /etc/pam.d/vsftpd /orig-config/
cat /dev/null > /etc/pam.d/vsftpd

echo "auth required pam_mysql.so user=$puftp passwd=$psql host=localhost db=vsftpd table=accounts usercolumn=username passwdcolumn=pass crypt=2
account required pam_mysql.so user=$puftp passwd=$psql host=localhost db=vsftpd table=accounts usercolumn=username passwdcolumn=pass crypt=2" > /etc/pam.d/vsftpd

/etc/init.d/vsftpd restart
#may be service vsftpd restart

echo "setting up ftp admin & standard users for websites" 
echo "adding new users to ftp database"

Upvotes: 0

Jim Lewis
Jim Lewis

Reputation: 45035

It's hard to say how to fix this without seeing the exact command you're using, but I suspect that the shell is doing backquote substitution on your text. So where you have

CREATE TABLE `accounts`

bash tries to execute accounts as a command, and replace the backquoted text with the standard output of the command. And since there's probably no such command in your path, the backquoted section disappears in the output.

Try escaping each ` character with a backslash, like this:

CREATE TABLE \`accounts\`

Upvotes: 2

Andreas Wong
Andreas Wong

Reputation: 60516

If you run things with backticks in shell, it will evaluate the commands enclosed in ``. So one way of doing it is to use a single quote like so:

$ echo '   USE vsftpd;
    CREATE TABLE `accounts` (
    `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    `username` VARCHAR( 30 ) NOT NULL ,
    `pass` VARCHAR( 50 ) NOT NULL ,
    UNIQUE (
    `username`
    )

) ENGINE = MYISAM ;

quit;
' > file.sql

$ more file.sql # verify content is correct

Upvotes: 1

Related Questions