Bash 写入文件(变量?)

发布于 2024-10-06 12:16:16 字数 590 浏览 6 评论 0原文

OK,尝试将 SQL 查询写入文本文件。我试过猫,但没有成功。

这是我想写入文件的内容:

   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;

这是我使用 cat 或 echo 时得到的结果(也许我需要某种开关?):

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;

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

似梦非梦 2024-10-13 12:16:16

在没有看到您正在使用的确切命令的情况下,很难说如何解决这个问题,但我怀疑 shell 正在执行 反引号替换 在您的文本上。因此,在有

CREATE TABLE `accounts`

bash 的地方,尝试将 accounts 作为命令执行,并用命令的标准输出替换反引号文本。由于您的路径中可能没有这样的命令,因此反引号部分会在输出中消失。

尝试使用反斜杠转义每个 ` 字符,如下所示:

CREATE TABLE \`accounts\`

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\`
Spring初心 2024-10-13 12:16:16

如果您在 shell 中运行带有反引号的命令,它将评估 `` 中包含的命令。因此,一种方法是使用单引号,如下所示:

$ 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

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
司马昭之心 2024-10-13 12:16:16

无论如何,我想我现在可以发布整个内容,以防其他人使用 vsftpd 并且你帮助了我。这是使用 mysql 的 vsftpd 虚拟用户的脚本。除了创建新用户之外,这里的所有内容都可以从给定的内容中找出如何编写:

#!/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"

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"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文