写一个shell脚本,两个变量的值如果不同则将该变量的值加入/data/erro.txt的文件中,如果两个值相同则不会加入文件
时间: 2024-04-07 20:33:09 浏览: 10
可以使用以下的shell脚本实现你的需求:
```shell
#!/bin/bash
variable1="value1"
variable2="value2"
if [ "$variable1" != "$variable2" ]; then
echo "$variable1" >> /data/erro.txt
echo "$variable2" >> /data/erro.txt
fi
```
以上的脚本中,我们首先定义了两个变量 `variable1` 和 `variable2` 的值。然后通过判断这两个变量的值是否相等,来决定是否将它们的值加入到 `/data/erro.txt` 文件中。如果值不相等,则将两个变量的值分别追加到文件中。如果值相等,则不执行任何操作。
你可以根据实际需求修改 `variable1` 和 `variable2` 的值,以及文件路径 `/data/erro.txt`。
相关问题
centos7 写一个shell脚本,将当前主机IP(Shell变量配置)、CPU使用率(纯数字)、内存总量和使用量(单位GB)、硬盘总量和使用量(单位GB)使用curl json方式调用http://192.168.220.1:18181/jeecg-boot/luodiye/sendMsg(Shell变量配置),脚本每隔N秒钟执行一次(Shell变量配置),curl 超时时间为5秒,调用失败输出当前时间和错误信息到当前目录monitor_error.log日志文件(没有则创建),脚本循环调用
```bash
#!/bin/bash
# 定义变量
ip=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/')
cpu=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | cut -f1 -d"%")
mem_total=$(free -g | grep Mem | awk '{print $2}')
mem_used=$(free -g | grep Mem | awk '{print $3}')
disk_total=$(df -h | grep /dev/sda1 | awk '{print $2}')
disk_used=$(df -h | grep /dev/sda1 | awk '{print $3}')
url="http://192.168.220.1:18181/jeecg-boot/luodiye/sendMsg"
interval=60 # 执行间隔时间
timeout=5 # curl超时时间
while true
do
# 构造json数据
json_data="{\"ip\":\"$ip\",\"cpu\":\"$cpu\",\"mem_total\":\"$mem_total\",\"mem_used\":\"$mem_used\",\"disk_total\":\"$disk_total\",\"disk_used\":\"$disk_used\"}"
# 发送请求
result=$(curl -s -m $timeout -H "Content-Type:application/json" -d "$json_data" $url)
# 判断结果
if [ $? -ne 0 ]; then
# 记录错误日志
echo "[$(date)] curl error: $result" >> monitor_error.log
fi
# 等待
sleep $interval
done
```
这是一个示例脚本, 用于收集当前主机的IP、CPU使用率、内存总量和使用量、硬盘总量和使用量, 然后使用curl请求发送到指定的URL中, 并且每隔N秒钟(60s)执行一次,如果curl失败则记录到当前目录的monitor_error.log文件中.
请注意:
- 脚本中的IP地址、URL、间隔时间、超时时间等参数需要根据实际情况进行修改.
- 脚本中的硬盘检测是基于/dev/s
编写shell脚本,实现txt文件导入到orcale数据库
下面是一个基本的 shell 脚本,实现将 txt 文件导入到 Oracle 数据库的功能:
```
#!/bin/bash
# 定义文件名和表名
filename="your_file.txt"
tablename="your_table"
# 判断文件是否存在
if [ ! -f "$filename" ]; then
echo "Error: $filename not found."
exit 1
fi
# 使用 sqlldr 导入文件
sqlldr userid=your_username/your_password control="$filename.ctl" log="$filename.log" bad="$filename.bad" data="$filename"
# 判断导入是否成功
if [ $? -eq 0 ]; then
echo "Data has been loaded successfully."
else
echo "Data has not been loaded successfully."
exit 1
fi
exit 0
```
在运行此脚本之前,请确保已安装了 Oracle 客户端,并且配置了环境变量以使用 `sqlldr` 命令。
这个脚本中,我们使用了 `sqlldr` 命令来导入 txt 文件,该命令需要一个控制文件(control file)来指定如何加载数据。控制文件内容如下:
```
load data
infile 'your_file.txt'
into table your_table
fields terminated by ","
(
field1,
field2,
...
)
```
请根据自己的实际情况替换文件名、表名、用户名、密码以及字段分隔符等信息。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)