리눅스 경로 지정으로 아무곳에서나 명령어 실행하기

리눅스 경로 지정으로 아무곳에서나 명령어 실행하기

아래에서 장황하게 설명을 할려다보니 너무 길어져서 못하겠네요.

핵심만 말씀드릴게요.


#PATH=$PATH:/root/test   -  로그인 세션에서 특정 디렉토리 경로 걸때
#PATH=$PATH:.     - 로그인 세션에서 특정 전 디렉토리 경로 걸때
#vi /etc/profile   - 모든 사용자에게 특정 디렉토리 경로 걸때 , 재로그인 시점 부터 적용(권장안함)
[root@localhost ~]# vi root/.bash_profile  - 특정사용자(여긴 root)가 특정 디렉토리 경로 걸때






#PATH=$PATH:/root/test
로그인한 세션 상태에서만 /root/test  디렉토리 안에 있는 스크립트 실행

#PATH=$PATH:.
로그인한 상태의 세션에서만 모든 경로에서 원하는 스크립트 실행

#vi /etc/profile  에서..
# Path manipulation
if [ "$EUID" = "0" ]; then
        pathmunge /sbin
        pathmunge /usr/sbin
        pathmunge /usr/local/sbin
        pathmunge /root/test
이 부분에 추가 ,  대신 이 파일에 추가를 하면 모든 사용자들에게도 경로가 잡히기 때문에 권장하지 않습니다. 이렇게 사용하시면 안되겠죠.

자신만 사용하기 위해서는..


[root@localhost ~]# cd
[root@localhost ~]# pwd
/root
[root@localhost ~]# vi .bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin:root/test

export PATH
unset USERNAME
[root@localhost ~]#










리눅스의 기본 명령어들은 경로가 잡혀있어서 아무곳에서나 실행이 가능합니다.

가령

[root@localhost ~]# ls
anaconda-ks.cfg  Desktop  Documents  Download  install.log  install.log.syslog  Music  Pictures  Public  Templates  test  Videos

이런 ls 명령어는 아무곳 에서 실행해도 잘 먹히는 데 그것은 저 명령어가 있는 곳의 디렉토리 경로가 잡혀있어서 그렇습니다.

경로를 보는 방법은

[root@localhost ~]# echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/lib/ccache:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin
:/usr/bin:/root/bin:/root/test:/wim/bin:/wim/conf

이렇게 입력하면 경로들을 볼 수 있습니다.  저 경로에 자신이 원하는 스크립트가 실행되도록 경로를 잡아주면 되는 것이죠.

경로를 잡는데는 크게 4가지 방법이 있습니다.

1. 로그인한 세션 상태에서만 특정 경로 잡는 경우
2. 로그인한 세션 상태에서만 모든 경로 잡는 경우
3. 리눅스 사용자 모두에게 경로를 잡아주는 경우
4. 본인만 사용할 수 있게 경로를 잡는 경우

먼저

일단  /test/test.sh 라는 파일을 만들고  Hello 라는 문구가 출력되게 해보겠습니다.

[root@localhost ~]# cd /
[root@localhost /]# pwd
/
[root@localhost /]# mkdir test
[root@localhost /]# cd test
[root@localhost test]# touch test.sh
[root@localhost test]# vi test.sh

#!/bin/sh


 echo HELLO?
 echo

 exit 0
~

~
~
~
~
~
~
"test.sh" 7L, 40C written
[root@localhost test]#

[root@localhost test]# test.sh
-bash: test.sh: command not found
[root@localhost test]# ./test.sh
-bash: ./test.sh: Permission denied
[root@localhost test]# chmod 655 test.sh
[root@localhost test]# ./test.sh
HELLO?

[root@localhost test]# test.sh
-bash: test.sh: command not found
[root@localhost test]# PATH=$PATH:/test
[root@localhost test]# test.sh
HELLO?

[root@localhost test]#