设为首页
收藏本站
用户名
Email
自动登录
找回密码
密码
登录
注册
快捷导航
平台
Portal
论坛
BBS
文库
项目
群组
Group
我的博客
Space
搜索
搜索
热搜:
shell
linux
mysql
本版
用户
LinuxSir.cn,穿越时空的Linuxsir!
»
论坛
›
Linux 综合讨论区 —— LinuxSir.cn
›
shell进阶应用、shell编程
›
写了一个显示目录树的脚本,可输出不对 ...
1
2
3
4
5
6
/ 6 页
下一页
返回列表
楼主:
LYOO
写了一个显示目录树的脚本,可输出不对
[复制链接]
LYOO
LYOO
当前离线
积分
782
IP卡
狗仔卡
楼主
|
发表于 2003-5-4 11:19:40
|
显示全部楼层
看你的帖子觉得是正常的。每个着色目录名都包含在颜色标签中(如:^[[01;34m文件名^[[00m)。下面是我做的实验,很正常
屏幕输出:
/home/lyoo/GNUstep
|-- Defaults
| |-- WindowMaker
| |-- WMGLOBAL
| |-- WMRootMenu
| |-- WMState
| |-- WMWindowAttributes
| |-- WPrefs
|-- Library
| |-- Icons
| |-- WindowMaker
| | |-- autostart
| | |-- Backgrounds
| | |-- IconSets
| | |-- Pixmaps
| | |-- plmenu
| | |-- Sounds
| | |-- SoundSets
| | |-- Styles
| | |-- Themes
`
11 directories, 8 files
重定向输出:
/home/lyoo/GNUstep
|-- ^[[01;34mDefaults^[[00m
| |-- WindowMaker
| |-- ^[[01;32mWMGLOBAL^[[00m
| |-- WMRootMenu
| |-- WMState
| |-- WMWindowAttributes
| |-- WPrefs
|-- ^[[01;34mLibrary^[[00m
| |-- ^[[01;34mIcons^[[00m
| |-- ^[[01;34mWindowMaker^[[00m
| | |-- ^[[01;32mautostart^[[00m
| | |-- ^[[01;34mBackgrounds^[[00m
| | |-- ^[[01;34mIconSets^[[00m
| | |-- ^[[01;34mPixmaps^[[00m
| | |-- plmenu
| | |-- ^[[01;34mSounds^[[00m
| | |-- ^[[01;34mSoundSets^[[00m
| | |-- ^[[01;34mStyles^[[00m
| | |-- ^[[01;34mThemes^[[00m
`
11 directories, 8 files
回复
支持
反对
使用道具
举报
显身卡
Freebird
Freebird
当前离线
积分
2456
IP卡
狗仔卡
发表于 2003-5-4 12:37:25
|
显示全部楼层
能不能在输出重定向后不要这些颜色选项呀?如果能才叫完美呀!
回复
支持
反对
使用道具
举报
显身卡
LYOO
LYOO
当前离线
积分
782
IP卡
狗仔卡
楼主
|
发表于 2003-5-4 12:49:56
|
显示全部楼层
当然可以,你可以给plan9兄去信,也可以自己动手。我也在改进我的脚本。
回复
支持
反对
使用道具
举报
显身卡
plan9
plan9
当前离线
积分
208
IP卡
狗仔卡
发表于 2003-5-6 10:02:19
|
显示全部楼层
更新了一下,在tree.sh后加上参数-n就可以不显示颜色,例如:
linux$ ./tree.sh -n /etc
#!/bin/sh
#
# tree.sh
# A tool that display the dictionary structure in dos's
# tree command style.
# By Matthew <matthew@linuxforum.net>
#
# __@
# _ \<_
# (_)/(_)
# Apr 29 2003
#
# Tested on slackware, openbsd, netbsd, freebsd.
#
# Just for fun.
#
# The name of the ls program, please use
# the absolute path, otherwise, there
# may be get some strange errors.
#
LSPROG="/bin/ls"
# COLOR DEFINE
# ============
#
DIR="\033[01;34m"
EXE="\033[01;32m"
DEV="\033[01;33m"
LNK="\033[01;36m"
ZIP="\033[01;31m"
SOCK="\033[01;35m"
NULL="\033[00m"
TRUE=0
FALSE=1
LAYERS=0
FILECOUNT=0
DIRCOUNT=0
color=${TRUE}
if [ "${1#-n}" != "${1}" ]; then
shift
color=${FALSE}
fi
ROOT=${1:-.}
if [ ${COLOR} -eq ${FALSE} ]; then
DIR=""
EXE=""
DEV=""
LNK=""
ZIP=""
SOCK=""
NULL=""
fi
# print_dash
# ==========
# Print the structure lines
#
print_dash()
{
local i=0
local num=$1
while [ $i -lt $num ]; do
echo -n "|"
for j in 1 2 3; do
echo -n " "
done
i=`expr $i + 1`
done
echo -n "|-- "
}
# ispkg
# =====
# Test if the file is a package like:
# .gz .tar .tgz .tar.gz .zip .rar .rpm
# and etc.
#
ispkg()
{
local f=$1
local i
# Package extension list, you can add your coustom
# extensions in it.
#
local pkg__ext=".gz .tar .tgz .tar.gz .zip .rar .rpm"
# if the file's suffix contain any package extension
# then cut it.
for i in $pkg__ext; do
f=${f%$i}
done
if [ "$f" != "$1" ]; then
return $TRUE
else
return $FALSE
fi
}
# mktree
# ======
# The main function, that print the
# dictionary structure in dos's tree
# command style. It's runs in nesting.
#
mktree()
{
local f
for f in `$LSPROG -1 $1`; do
f=${f%/}
f=${f##*/}
# If dictionary then print it and enter
# the nesting block.
if [ -d $1/$f ]; then
print_dash $LAYERS
echo -e "${DIR}$f${NULL}"
DIRCOUNT=`expr $DIRCOUNT + 1`
LAYERS=`expr $LAYERS + 1`
mktree $1/$f
else
print_dash $LAYERS
# file is a symbol link
if [ -L $1/$f ]; then
echo -e "${LNK}$f${NULL}"
# file is executable
elif [ -x $1/$f ]; then
echo -e "${EXE}$f${NULL}"
# file is a device
elif [ -c $1/$f -o -b $1/$f ]; then
echo -e "${DEV}$f${NULL}"
# file is a socket
elif [ -S $1/$f ]; then
echo -e "${SOCK}$f${NULL}"
# file is a package
elif `ispkg $f`; then
echo -e "${ZIP}$f${NULL}"
else
echo -e "$f"
fi
FILECOUNT=`expr $FILECOUNT + 1`
fi
done
LAYERS=`expr $LAYERS - 1`
}
echo $ROOT
mktree $ROOT
echo "\`"
echo "$DIRCOUNT directories, $FILECOUNT files"
复制代码
回复
支持
反对
使用道具
举报
显身卡
Freebird
Freebird
当前离线
积分
2456
IP卡
狗仔卡
发表于 2003-5-6 12:26:56
|
显示全部楼层
呵呵,你搞错了,我说得是输出重定向,你这样是在终端都没颜色了
回复
支持
反对
使用道具
举报
显身卡
plan9
plan9
当前离线
积分
208
IP卡
狗仔卡
发表于 2003-5-7 09:20:43
|
显示全部楼层
重定了向当然在屏幕上就不能显示了呀。你要重定向的时候就加上-n参数,这样就不会显示出颜色,而如果是非重定向,就不加-n参数这样就还是彩色的呀。
比如:
重定向: linux$ ./tree.sh -n /etc > out
正常: linux$ ./tree.sh /etc
回复
支持
反对
使用道具
举报
显身卡
LYOO
LYOO
当前离线
积分
782
IP卡
狗仔卡
楼主
|
发表于 2003-5-7 11:43:35
|
显示全部楼层
报告plan9:
俺申请把你的ispkg()和mktree()的一部分移到自己的脚本中了,实现目录着色。
回复
支持
反对
使用道具
举报
显身卡
plan9
plan9
当前离线
积分
208
IP卡
狗仔卡
发表于 2003-5-7 14:59:24
|
显示全部楼层
最初由 LYOO 发表
报告plan9:
俺申请把你的ispkg()和mktree()的一部分移到自己的脚本中了,实现目录着色。
LYOO兄严重了,你直接移就可以了,我贴出来就是大家共享的嘛,等你做好了,Mail一份给我:matthew@linuxforum.net,谢谢!
回复
支持
反对
使用道具
举报
显身卡
LYOO
LYOO
当前离线
积分
782
IP卡
狗仔卡
楼主
|
发表于 2003-5-7 19:42:15
|
显示全部楼层
呵呵,用plan9的代码就是省力,不怎么改就可以显示色彩了。不过计算目录数和文件数的代码不好移,写这个脚本时没考虑到计数,现在还真不好加。
$ ./dirtree.sh /your/dir----------------------默认为不上色显示
$ ./dirtree.sh -color /your/dir------------加上-color选项就可以上色显示了
#!/bin/bash
# Name: dirtree.sh
# Dispaly your filesystem like a tree
# You can use "-color" toggle the color
# I wrote it in Woody
# Special Thanks for plan9
#########################
# COLOR DEFINE
# Thanks for plan9's code
#########################
DIR="\033[01;34m"
EXE="\033[01;32m"
DEV="\033[01;33m"
LNK="\033[01;36m"
ZIP="\033[01;31m"
SOCK="\033[01;35m"
NULL="\033[00m"
#########################
# Check File extension
# Thanks for plan9's code
#########################
ispkg()
{
local f=$1
local i
# Package extension list, you can add your coustom
# extensions in it.
#
local pkg__ext=".gz .tar .tgz .tar.gz .zip .rar .rpm"
#local pic__ext=".png .jpg .jpeg .gif .tif"
# if the file's suffix contain any package extension
# then cut it.
for i in $pkg__ext; do
f=${f%$i}
done
if [ "$f" != "$1" ]; then
return $TRUE
else
return $FALSE
fi
}
#########################
# Select Color for files
# Thanks for plan9's code
#########################
dircolor ()
{
if [ $1 = "yes" ]; then
# file is directory
if [ -d $thisfile ]; then
colorfile=${DIR}$file/${NULL}
echo -e $line$colorfile
# file is link
elif [ -L $thisfile ]; then
colorfile=${LNK}$file${NULL}
echo -e $line$colorfile
# file is executable
elif [ -x $thisfile ]; then
colorfile=${EXE}$file${NULL}
echo -e $line$colorfile
# file is a device
elif [ -c $thisfile -o -b $file ]; then
colorfile=${DEV}$file${NULL}
echo -e $line$colorfile
# file is a socket
elif [ -S $thisfile ]; then
colorfile=${SOCK}$file${NULL}
echo -e $line$colorfile
# file is a package
elif $(ispkg $thisfile); then
colorfile=${ZIP}$file${NULL}
echo -e $line$colorfile
else
colorfile=$file
echo -e $line$colorfile
fi
else
echo -e $line$file
fi
}
################
# Directory Tree
################
redir ()
{
tab=$tab$singletab
line=${tab%"$singletab"}"|-------"
local count=$#
for file in "$@"; do
thisfile=${thisfile:-$PWD}/$file
if [ -d "$thisfile" ]; then
if [ $count -eq 1 ]; then
dircolor $colorswitch
tab=${tab%"$singletab"}"\t"
redir $(ls $thisfile)
else
dircolor $colorswitch
redir $(ls $thisfile)
fi
else
dircolor $colorswitch
fi
thisfile=${thisfile%/*}
let count=count-1
done
tab=${tab%"\t"}
tab=${tab%"|"}
line=${tab%"$singletab"}"|-------"
}
#################
# THE MAIN SCRIPT
#################
singletab="|\t"
colorswitch="no"
if [ -n "$1" ]; then
# dose user want color ?
if [ $1 = "-color" ]; then
colorswitch="yes"
shift
fi
# make dirtrees
if [ -e $1 ]; then
for file in $1; do
echo $file
echo '|'
if [ -d "$file" ]; then
cd $file
redir $(ls)
cd ..
fi
done
else
echo "the Directory doesn't exist"
fi
else
echo "USEAGE: $0 [-color] Directory"
fi
复制代码
回复
支持
反对
使用道具
举报
显身卡
plan9
plan9
当前离线
积分
208
IP卡
狗仔卡
发表于 2003-5-9 09:45:57
|
显示全部楼层
呵呵,恭喜,恭喜。
现在我正在想办法能不能解决最后文件显示的问题,LYOO兄有没有办法,指点一下?
回复
支持
反对
使用道具
举报
显身卡
下一页 »
1
2
3
4
5
6
/ 6 页
下一页
返回列表
高级模式
B
Color
Image
Link
Quote
Code
Smilies
您需要登录后才可以回帖
登录
|
注册
本版积分规则
发表回复
回帖后跳转到最后一页
Copyright © 2002-2023
LinuxSir.cn
(http://www.linuxsir.cn/) 版权所有 All Rights Reserved.
Powered by
RedflagLinux!
技术支持:
中科红旗
|
京ICP备19024520号
快速回复
返回顶部
返回列表