[英]Bash alias or function to find Compass, and CD into the directory
通常,我的项目在文件系统的深处有一个SCSS + Compass目录。
例如,我的所有项目都在~/Web/com.example.subdomain
,有时样式目录是~/Web/com.example.subdomain/trunk/docroot/_assets/locked/styles
。
每天多次输入整个CD命令显然有点重复。 我可以为每个项目创建一个别名(例如alias cd-com.marketo.subdomain-scss="..."
)但是必须为所有25个以上的项目添加和维护。
$ cd ~/Web/com.example
$ cd-compass
find .
查找config.rb
或.sass-cache/
.sass-cache/
不存在且config.rb
不存在,它将运行compass stats /the/dir
Nothing to compile. If you're trying to start
开始Nothing to compile. If you're trying to start
Nothing to compile. If you're trying to start
...... .sass-cache/
目录 添加包含指南针命令的功能,因此输入
cd-compass compile --force
将遵循上面的流程(包括cd进入目录),然后执行compass compile --force
也许在这种情况下,命令将是
compass-do
或fcompass
或lcompass
(f = find; l = locate)。
如果找到多个Compass安装,则显示编号的选项列表。 输入其中一个数字并按Enter将继续该选项。
这基本上允许我简单地输入:
$ cd-compass
1. /home/user/Web/git/com.example/trunk/docroot/admin/styles/compass
2. /home/user/Web/git/com.example/trunk/docroot/styles/compass
3. /home/user/Web/svn/com.example.sub/trunk/docroot/_assets/locked/styles
Which one? > __
这并没有完全做你想要的一切 - 例如,它不会构建.sass-cache,如果它丢失了,但它应该直接扩展它这样做(读者的练习:-)。 如果目录名中有空格,它也将无效。
对于shell别名来说真的有点大。
首先,您需要将所有潜在目录放入变量中
dirs=$(find . -name config.rb -o \( -type d -name .sass-cache \) | sed -e 's,/.sass-cache$,,' -e 's,/config\.rb$,,' | sort | uniq -c | grep '^ *2 ' | sed -e 's/^ *2 //')
这个位调用find来查找名为config.rb的任何文件或名为.sass-cache的目录。 然后用sed剥去路径的最后一部分以获取目录名,对结果进行排序并计算简单结果路径的数量。 然后只提取出现两次的目录,并从目录的开头删除计数......并且“嘿presto!” 我们有一个Compass目录列表
然后计算它们中有多少:
count=0 ; for i in $dirs ; do : ; count=$((count+1)) ; done
或者,您可以通过回显dirs变量并将其输送到“wc -w”来获取计数
然后你去处理不同可能的计数值
if [ $count -eq 0 ] ; then echo no compass directory found; exit ; fi
if [ $count -eq 1 ] ; then cd $dirs ; exit ; fi
if [ $count -gt 1 ] ; then idx=1 ; for i in $dirs ; do echo $idx $i; idx=$((idx+1)) ; done ; fi
while true
do
echo -n "enter directory number: "
read choice
if [ $choice -gt $count ]
then
echo "invalid choice"
else
idx=1
for i in $dir
do
if [ $idx -eq $choice ]
then
cd $i ; exit
fi
idx=$((idx+1))
done
fi
done
Z解决了在命令行上快速跳转到目录的更普遍的问题。 它通过构建您cd到的目录数据库来实现这一点,并通过“frecency”对它们进行排名 - 基于频率和新近度的综合得分。
一旦访问了某些目录,就可以使用z
后跟一个或多个子字符串跳转到它们。 例如,要访问我的gitolite管理存储库,我只需输入z gito
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.