쉘스크립트에서 cd 문법

mudori의 이미지

명령문으로 cd ~account 계정안으로 들어가는데

스크립트로 돌릴땐 안됩니다.

계정 홈디렉토리가 각각일때

passwd 파일을 읽는다거나 하지 않고

위와 같은 방법으로 접근 가능하게 하려면 어찌하면 좋을까요?

익명 사용자의 이미지

cd $HOME ?
cd /home/$LOGNAME ?
실행시키는 계정은 고정되어 있나요?

지리즈의 이미지

구여지책으로
ACCOUNT="foo"
eval "cd ~$ACCOUNT"

[root@ns2 root]# pwd
/root
[root@ns2 root]# cat test.sh
#!/bin/sh
ACCOUNT=oracle
eval "cd ~$ACCOUNT"
pwd
[root@ns2 root]# ./test.sh
/home/oracle
[root@ns2 root]# pwd
/root
[root@ns2 root]# . ./test.sh
/home/oracle
[root@ns2 oracle]# pwd
/home/oracle

There is no spoon. Neo from the Matrix 1999.

익명 사용자의 이미지

bash 3.00.13(1)-release 에서는 스크립트에서도 되는군요..