Current Directory in Shell Script

吃可爱长大的小学妹 提交于 2019-12-13 19:26:31

问题


I am quite new to shell scripting.

I am trying to make a directory when a user runs my script using bash scriptName.sh.

At present my shell script looks like,

mkdir -p build/iphoneos/XXXXXXXXXX.txt

I want to know how I can put the current directory's name in place of XXXXXXX, in the script.

Any help will be appreciated.


回答1:


You could use:

mkdir -p build/iphoneos/$(basename $PWD).txt

or

mkdir -p build/iphoneos/${PWD##*/}.txt

The first calls the basename binary. The second one removes all character up to the last / character.




回答2:


Use pwd and parse get the last word.

Here is a way

foo=`pwd | rev | cut -f1 -d'/' | rev` 

then use $foo instead of XXXXX



来源:https://stackoverflow.com/questions/22234933/current-directory-in-shell-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!