Last active
August 29, 2015 14:10
-
-
Save beta/bc6767874bd5ccb08578 to your computer and use it in GitHub Desktop.
7file - a simple file manager in (BSD) bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# 输出分隔符 | |
function separator () { | |
echo '-------------------------------------------------------------------------------' | |
} | |
# 输出当前目录中的文件列表 | |
function show_files () { | |
count=0 | |
for file in ${fileList} | |
do | |
echo ${file} | |
count=`expr $count + 1` | |
done | |
if [ ${#errorMsg} -gt 0 -o ${#successMsg} -gt 0 ] | |
then | |
blank=`expr 15 - ${count}` | |
else | |
blank=`expr 16 - ${count}` | |
fi | |
for i in $(seq 1 ${blank}) | |
do | |
echo | |
done | |
} | |
# 输出提示符 | |
function prompt () { | |
echo -ne '$ ' | |
} | |
# 若有错误,显示错误信息 | |
function error () { | |
if [ ${#errorMsg} -gt 0 ] | |
then | |
echo -e "\033[31m\a错误: "${errorMsg} | |
echo -ne "\033[0m" | |
fi | |
} | |
# 显示成功信息 | |
function success () { | |
if [ ${#successMsg} -gt 0 ] | |
then | |
echo -e "\033[32m"${successMsg} | |
echo -ne "\033[0m" | |
fi | |
} | |
# 显示命令提示 | |
function hint () { | |
if [ ${#paste[*]} -gt 0 ] | |
then | |
echo -ne "\033[47mexit\033[0m|\033[47mq\033[0muit 退出" | |
echo -ne "\t\033[47mls\033[0m 列出文件" | |
echo -ne "\t\033[47mp\033[0maste 复制剪贴板文件" | |
echo -e "\t\033[47mm\033[0move 移动剪贴板文件" | |
else | |
echo -ne "\033[47mexit\033[0m|\033[47mq\033[0muit 退出" | |
echo -e "\t\033[47mls\033[0m 列出文件" | |
fi | |
echo -ne "\033[47mcd\033[0m 更改目录" | |
echo -ne "\t\033[47mf\033[0mind 查找" | |
echo -ne "\t\033[47mzip\033[0m 归档" | |
echo -e "\t\t\033[47ma\033[0mdd 添加到剪贴板" | |
echo -ne "\033[47mrm\033[0m 删除" | |
echo -ne "\t\t\033[47mn\033[0mew 新建文件" | |
echo -ne "\t\033[47mr\033[0mename 重命名" | |
echo -e "\t\t\033[47mo\033[0mpen 在Finder中打开" | |
echo -ne "\033[0m" | |
} | |
dir=`pwd` # 获得当前目录 | |
willExit=0 | |
successMsg='' # 成功信息 | |
errorMsg='' # 错误信息 | |
fileList=`ls -p` | |
paste=() | |
while [ $willExit == 0 ] | |
do | |
clear | |
echo '7file' | |
echo '当前目录: '${dir} | |
separator | |
show_files | |
fileList=`ls -p` | |
separator | |
success | |
error | |
hint | |
prompt | |
read input | |
i=0 | |
inst=() | |
for part in ${input} | |
do | |
inst[$i]=${part} | |
i=`expr $i + 1` | |
done | |
successMsg='' | |
errorMsg='' | |
# 缩写转义 | |
case ${inst[0]} in | |
f) inst[0]='find';; | |
a) inst[0]='add';; | |
p) inst[0]='paste';; | |
m) inst[0]='move';; | |
o) inst[0]='open';; | |
n) inst[0]='new';; | |
r) inst[0]='rename';; | |
z) inst[0]='zip';; | |
q) inst[0]='quit';; | |
*) ;; | |
esac | |
case ${inst[0]} in | |
cd) # 更改目录 | |
if [ ${#inst[*]} -ge 2 ] | |
then | |
cd ${inst[1]} | |
else | |
cd | |
fi | |
dir=`pwd` | |
fileList=`ls -p` | |
;; | |
ls) # 显示文件列表 | |
fileList=`ls -p` | |
;; | |
find) # 查找 | |
if [ ${#inst[*]} -ge 2 ] | |
then | |
fileList=`find *${inst[1]}*` | |
else | |
errorMsg='缺少关键字(用法: find keyword)' | |
fi | |
;; | |
add) # 添加到剪贴板 | |
if [ ${#inst[*]} -ge 2 ] | |
then | |
# 判断文件是否存在 | |
if [ ! -f ${inst[1]} ] | |
then | |
errorMsg="文件${inst[1]}不存在" | |
else | |
# 得到文件的完整路径 | |
s=${inst[1]} | |
s=${s%%[/]*} | |
fullPath='' | |
if [ ${#s} == ${#inst[1]} ] | |
then | |
fullPath="`pwd`/${inst[1]}" | |
else | |
fullPath=${inst[1]} | |
fi | |
# 判断是否已在剪贴板中 | |
exist=0 | |
for file in ${paste[*]} | |
do | |
if [ ${file} = ${inst[1]} ] | |
then | |
exist=1 | |
fi | |
done | |
if [ ! ${exist} == 0 ] | |
then | |
errorMsg="文件${inst[1]}已在剪贴板中" | |
else | |
paste[${#paste[*]}]=${fullPath} | |
successMsg="文件${inst[1]}已添加到剪贴板" | |
fi | |
fi | |
else | |
errorMsg="缺少文件名(用法: add file)" | |
fi | |
;; | |
paste) # 复制剪贴板中文件 | |
for file in ${paste[*]} | |
do | |
cp ${file} . | |
done | |
paste=() | |
fileList=`ls -p` | |
;; | |
move) # 移动剪贴板中文件 | |
for file in ${paste[*]} | |
do | |
mv ${file} . | |
done | |
paste=() | |
fileList=`ls -p` | |
;; | |
open) # 在Finder中打开 | |
if [ ${#inst[*]} -ge 2 ] | |
then | |
open ${inst[1]} | |
else | |
open ${dir} | |
fi | |
;; | |
rm) # 删除 | |
if [ ${#inst[*]} -ge 2 ] | |
then | |
rm ${inst[1]} | |
successMsg="文件${inst[1]}已删除" | |
else | |
errorMsg='缺少文件名(用法: rm filename)' | |
fi | |
fileList=`ls -p` | |
;; | |
new) # 新建文件 | |
if [ ${#inst[*]} -ge 2 ] | |
then | |
touch ${inst[1]} | |
successMsg="文件${inst[1]}已创建" | |
else | |
errorMsg='缺少文件名(用法: new filename)' | |
fi | |
fileList=`ls -p` | |
;; | |
rename) # 重命名 | |
if [ ${#inst[*]} -ge 3 ] | |
then | |
if [ ! -f ${inst[1]} ] # 判断文件是否存在 | |
then | |
errorMsg="文件${inst[1]}不存在" | |
else | |
mv ${inst[1]} ${inst[2]} | |
fi | |
elif [ ${#inst[*]} == 2 ] | |
then | |
errorMsg='缺少新文件名(用法: rename old_file new_file)' | |
else | |
errorMsg='缺少文件名(用法: rename old_file new_file)' | |
fi | |
fileList=`ls -p` | |
;; | |
zip) # 归档 | |
if [ ${#inst[*]} -ge 3 ] # 提供了要归档的文件列表 | |
then | |
flag=0 | |
files='' | |
zipname="${inst[1]}.zip" | |
if [ -f ${zipname} ] | |
then | |
errorMsg="文件${zipname}已存在" | |
else | |
for i in $(seq 3 ${#inst[*]}) | |
do | |
i=`expr ${i} - 1` | |
file=${inst[${i}]} | |
files="${files} ${file}" | |
if [ ! -f ${file} ] | |
then | |
errorMsg="文件${file}不存在" | |
flag=1 | |
fi | |
done | |
if [ ${flag} == 0 ] | |
then | |
zip ${zipname} ${files} | |
successMsg="归档成功" | |
fi | |
fi | |
elif [ ${#inst[*]} == 2 ] | |
then | |
errorMsg="缺少文件名(用法: zip zipname file1 file2 ...)" | |
else | |
errorMsg="缺少归档文件名(用法: zip zipname file1 file2 ...)" | |
fi | |
fileList=`ls -p` | |
;; | |
exit) # 退出 | |
clear | |
willExit=1 | |
;; | |
quit) # 退出 | |
clear | |
willExit=1 | |
;; | |
*) | |
errorMsg='无效的指令' | |
;; | |
esac | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
looks like this