#!/bin/bash
#文件:.easyshell.sh
#放到/root目录
#在~/.bashrc文件中中插入一行
#source ~/.easyshell.sh
#shell中执行,即时生效
#source ~/.bashrc
#服务
#srv ssh status
function srv() {
/etc/init.d/$@
}
export srv
#解压缩文件
unfile() {
local src
if [ -z "$1" ]; then
echo "usage: unfile filename"
return 1
fi
# 判断文件是否存在
if [ -f "$1" ]; then
src="$1"
else
local file_list=$(find . -maxdepth 1 -name "*$1*.tar" -type f \
-or -name "*$1*.tbz2" -type f \
-or -name "*$1*.tgz" -type f \
-or -name "*$1*.tar.bz2" -type f \
-or -name "*$1*.tar.gz" -type f \
-or -name "*$1*.tar.Z" -type f \
-or -name "*$1*.bz2" -type f \
-or -name "*$1*.rar" -type f \
-or -name "*$1*.gz" -type f \
-or -name "*$1*.zip" -type f \
-or -name "*$1*.Z" -type f \
-or -name "*$1*.xz" -type f \
-or -name "*$1*.lzo" -type f \
-or -name "*$1*.7z" -type f)
IFS=$'\n'
local arr_file=($file_list)
unset IFS
local file_count=${#arr_file[@]}
if [ $file_count -eq 0 ]; then
echo "没有找到需要解压的文件: $1"
return 1
fi
if [ $file_count -gt 1 ]; then
echo " $1 匹配多个文件,输入需要解压的文件序号或输入0解压所有匹配文件"
#echo "-------------------------------------------------------------"
echo "[ 0 ] 解压所有文件"
for i in "${!arr_file[@]}"; do
echo "[ $((i + 1)) ] ${arr_file[$i]}"
done
local input_index
read -p "输入需要解压的文件序号: " input_index
# 检查输入是否为数字
if ! [[ "$input_index" =~ ^[0-9]+$ ]]; then
return 1
fi
if [ "$input_index" -gt "$file_count" ]; then
echo "input error [ 0 - $file_count ]"
return 1
fi
if [ $input_index -eq 0 ]; then
shift
for i in "${!arr_file[@]}"; do
unfile "${arr_file[$i]}" "$@"
done
return 0
else
input_index=$((input_index - 1))
src=${arr_file[$input_index]}
fi
else
src=${arr_file[0]}
fi
fi
shift
if [[ "$src" == *.tar ]] ||
[[ "$src" == *.tbz2 ]] ||
[[ "$src" == *.tgz ]] ||
[[ "$src" == *.tar.bz2 ]] ||
[[ "$src" == *.tar.gz ]] ||
[[ "$src" == *.tar.xz ]] ||
[[ "$src" == *.tar.Z ]]; then
tar xvf $src "$@"
elif [[ "$src" == *.bz2 ]]; then
bunzip2v $src "$@"
elif [[ "$src" == *.rar ]]; then
rar x $src "$@"
elif [[ "$src" == *.gz ]]; then
gunzip $src "$@"
elif [[ "$src" == *.zip ]]; then
unzip $src "$@"
elif [[ "$src" == *.Z ]]; then
uncompress $src "$@"
elif [[ "$src" == *.xz ]]; then
xz -d $src "$@"
elif [[ "$src" == *.lzo ]]; then
lzo -dv $src "$@"
elif [[ "$src" == *.7z ]]; then
7z x $src "$@"
else
echo "不支持的压缩格式"
fi
}
export unfile
test_easyshell() {
echo "hello easyshell"
}
export test_easyshell
#模糊匹配路径- 只能匹配唯一路径
cd() {
local dir_name="$1"
shift
if [ ! -d "$dir_name" ]; then
local file_list=$(find . -maxdepth 1 -name "*$dir_name*" -type d)
IFS=$'\n'
local arr_file=($file_list)
unset IFS
local file_count=${#arr_file[@]}
if [ $file_count -eq 1 ]; then
dir_name=${arr_file[0]}
fi
fi
builtin cd "$dir_name" "$@"
return $?
}
export cd
0