菜单选择
#!/bin/bash
echo "which one do you want?"
select option in option1 option2 option3 option4;
do
break
done
echo "You have selected $option"
用户输入
read -p "input a word :" word
if [ ! -n "$word" ] ;then
echo "you have not input a word!"
else
echo "the word you input is $word"
read -p "input another word :" anotherword
if [ ! -n "anotherword" ] ;then
echo "you have not input another word!"
else
echo "another word you input is $anotherword"
fi
fi
输入参数
if [ -z "$1" ]; then
echo "no param input"
else
echo "$1"
fi
判断文件和文件夹
#!/bin/bash
if [ ! -e "/home/vagrant/abc" ];then
echo "file or dir not exists."
else
rm -rf /home/vagrant/abc
echo "file/dir removed."
fi
if [ ! -d "/home/vagrant/test" ];then
mkdir /home/vagrant/test
else
echo "dir exists."
fi
if [ ! -f "/home/vagrant/abc" ];then
echo "file not exists."
else
rm -f /home/vagrant/abc
echo "file removed."
fi
Usage
#!/bin/bash
if [ $# != 2 ] ; then
echo "USAGE: $0 file_path_1 file_path_2"
echo " e.g.: $0 /home/vagrant/abc /home/vagrant/efg"
exit 1;
fi
脚本路径
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo $SCRIPT_DIR
sed
替换:sed -i "s/string_a/string_b/g" file_path
替换的如果是路径(含有/),则用|代替。注意要用双引号str="/data/path" sed -i "s|string_a|$str|g" file_path
eval
mycmd=’ls -all’
eval $mycmd #execute command