写一个shell脚本 切换为jzmanager用户执行命令,jzmanager密码为:db1x@NJ+1 并切换到当前用户下执行继续执行语句
时间: 2024-03-18 16:38:43 浏览: 114
好的,下面是一个简单的脚本,可以实现你的要求:
```shell
#!/bin/bash
# 切换为jzmanager用户执行命令
echo "db1x@NJ+1" | su -c "your_command_here" jzmanager
# 切换到当前用户下执行继续执行语句
# 这里可以继续编写你需要执行的命令
```
请将 `your_command_here` 替换为你需要执行的命令。执行脚本时,会自动输入jzmanager用户的密码。在执行完jzmanager用户的命令后,脚本会自动切换回当前用户,并且继续执行接下来的命令。
相关问题
-- Active: 1689209396165@@127.0.0.1@3306@db1 show databases;
根据你提供的活动信息,数据库连接参数如下:
- 主机名:127.0.0.1
- 端口号:3306
- 数据库名:db1
以下是连接到该数据库并显示数据库列表的示例代码(使用 Python 和 pymysql 库):
```python
import pymysql
# 连接数据库
conn = pymysql.connect(
host='127.0.0.1', # 主机名
port=3306, # 端口号
user='username', # 用户名
password='password', # 密码
db='db1' # 数据库名
)
# 执行查询操作
cursor = conn.cursor()
cursor.execute("SHOW DATABASES")
result = cursor.fetchall()
# 打印数据库列表
for db in result:
print(db[0])
# 关闭连接
cursor.close()
conn.close()
```
请将代码中的`username`和`password`替换为正确的用户名和密码。执行上述代码后,将会打印出数据库的列表。
写一个shell脚本 脚本输入参数:mysql端口号,mysql密码,mysql所在ip,MySQL的表(多个表逗号间隔),mysql表的列名(多个列逗号间隔) 脚本要求:循环判断mysql是否可登录,当可登录删除mysql表的列,并切换到MySQL用户,重启数据库,并重建这些表
好的,我来为您编写这个脚本。
```bash
#!/bin/bash
# 检查参数是否正确
if [ $# -lt 5 ]; then
echo "Usage: $0 <mysql_port> <mysql_password> <mysql_host> <mysql_tables> <mysql_columns>"
exit 1
fi
# 获取参数
mysql_port=$1
mysql_password=$2
mysql_host=$3
mysql_tables=$4
mysql_columns=$5
# 循环判断mysql是否可登录
while ! mysqladmin ping -h$mysql_host -P$mysql_port -p$mysql_password --silent; do
echo "MySQL is not available, waiting for 5 seconds..."
sleep 5
done
# 删除mysql表的列
mysql -h$mysql_host -P$mysql_port -p$mysql_password << EOF
USE mysql;
ALTER TABLE $mysql_tables DROP COLUMN $mysql_columns;
EOF
# 切换到MySQL用户
sudo -u mysql /etc/init.d/mysql restart
# 重建这些表
mysql -h$mysql_host -P$mysql_port -p$mysql_password << EOF
USE mysql;
DROP TABLE IF EXISTS $mysql_tables;
CREATE TABLE $mysql_tables (id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id));
EOF
echo "Done."
```
使用方法:
```bash
./script.sh <mysql_port> <mysql_password> <mysql_host> <mysql_tables> <mysql_columns>
```
其中,`<mysql_tables>` 和 `<mysql_columns>` 都可以使用逗号分隔多个表或列。例如:
```bash
./script.sh 3306 mypassword 127.0.0.1 db1.table1,db2.table2 column1,column2,column3
```
这个脚本会不断地检查 MySQL 是否可登录,如果不可登录,则会等待 5 秒钟后再次尝试。如果 MySQL 可登录,则会删除指定表的指定列,然后切换到 MySQL 用户,重启数据库,最后重新创建指定的表。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![ppt](https://img-home.csdnimg.cn/images/20241231044937.png)
![ppt](https://img-home.csdnimg.cn/images/20241231044937.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)