Changing PWD in a script to allow for accessing file without prefixing full path

I know I should be able to change the current working directory of a bash script by doing something akin to

cd `dirname $MYPATH`

but for some reason this doesn't work (or not as I imagined it).

#!/bin/bash

WAYPATH="/home/user/articles"
TEST_PATH="/home/user/testing"


# Set working directory of the script to be testing
cd `dirname $TEST_PATH`

for i in $(ls $WAYPATH); do

another_command $i $i.r > $TEST_PATH/htmls/$i.html

done

My goal here is to allow the bash script to find the files located in TEST_PATH (which have matching name to those in WAY_PATH) without having to prefix them with the full path (because another_command) makes use of the whole argument passed to it.


So this is a lesson on understanding what commands do after reading about them on Stackexchange. I was using

cd `dirname $MYPATH`

following this answer where they achieved the desired result

cd `dirname $0`

$0 is the full path of the bash script, so dirname is required to return the path without the name of the file.

Instead, for an arbitrary supplied path is sufficient to do a simple

cd $MYPATH

as suggested in comments.

链接地址: http://www.djcxy.com/p/56790.html

上一篇: 获取应用程序的路径

下一篇: 在脚本中更改PWD以允许访问文件,而无需为完整路径添加前缀