shell脚本:检查目录名和转换为小写 [英] shell script: check directory name and convert to lowercase

查看:152
本文介绍了shell脚本:检查目录名和转换为小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的bash脚本来检查其运行所在的目录的名称。是这样的:

I would like my bash script to check the name of the directory where it is run. Something like:

#!/bin/bash

path=eval 'pwd'

dirname=eval 'basename $path'

但它不工作:我得到

But it doesn't work: I get

./foo.sh: line 5: basename $path: command not found

我该如何解决?此外,一旦我得到目录名称包含正确的目录名称,我想将其转换为小写,对它进行测试。我能做到这一点使用awk在命令行上:

How can I fix it? Also, once I get dirname to contain the correct dirname, I'd like to convert it to lowercase, to test it. I'm able to do this on the command line with awk:

echo $dirname | awk '{print tolower($0)}'

但我如何获取返回值到一个变量?谢谢你在前进,

but how do I capture the return value into a variable? Thank you in advance,

此致

塞尔吉奥

推荐答案

为什么不使用:

#!/bin/bash

path=`pwd`
dirname=`basename $path | awk '{print tolower($0)}'`

或者,如果你想要做它作为一个单行:

Or if you want to do it as a one liner:

dirname=`pwd | xargs basename | awk '{print tolower($0)}'`

这篇关于shell脚本:检查目录名和转换为小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆