#!/bin/bash read -p "请输入大小(单位:G):" i o=`awk "BEGIN{printf $i*1024000}"` dd if=/dev/zero of=/var/swapfile bs=1024 count=$o &> /dev/null sleep 1 echo "/var/swapfile文件创建成功......" /sbin/mkswap /var/swapfile &> /dev/null sleep 1 echo "swapfile文件格式化并转换为swap分区......" /sbin/swapon /var/swapfile &> /dev/null sleep 1 echo "挂载并激活分区......" chmod -R 0600 /var/swapfile &> /dev/null echo "请手动将新的swap分区添加到/etc/fstab里(/var/swapfile swap swap defaults 0 0)......" free=`free -h | grep wap: | awk -F " " '{print $2}'` echo "swap大小为$free" exit 0
来源:https://www.cnblogs.com/frank-hui/p/10882310.html