su 명령과 함께 cd 명령을 사용하는 방법은 무엇입니까?

su 명령과 함께 cd 명령을 사용하는 방법은 무엇입니까?

아래 스크립트에서는 aserver와 bserver가 제대로 작동하고 있습니다. 하지만 위의 "cserver"의 경우 su - gsxuserp 이후 동일한 사용자를 사용하여 다음 세 가지 옵션을 실행해야 합니다.

cd ..
cd random_directory
tail -f file_in_random_directory

아무것도 하지 않고 연결이 닫히기 때문에 -c 옵션을 사용하여 이 작업을 수행할 수 없습니다. 누군가 이를 수행하는 기본적인 방법을 제안할 수 있습니까?

echo "Please type one of the following: aserver,bserver,cserver:" 
read input
echo "You entered: $input"
case "$input" in 
    "aserver")
        echo "Logging into a. Please enter the passwords when prompted"
        ssh -t [email protected] "ssh -t aserver "su - gsxp -c "sqlplus grep_ro/pwd"""
        ;;
    "bserver")
        echo "Logging into b. Please enter the passwords when prompted"
        ssh -t [email protected] "ssh -t bserver "su - gsxp -c "sqlplus grep_ro/pwd"""
        ;;
    "cserver")
        echo "Logging into c. Please enter the passwords when prompted"
        ssh -t [email protected] "ssh -t cserver "su - gsxuserp -c "cd """

        ;;
        *) 
        echo "Incorrect Option entered. Exiting the script"
        ;;
esac

답변1

왜 그냥 사용하지 않습니까 su - gsxuserp -c "tail -f ../random_directory/file_in_random_directory"?

각각이 새 세션을 시작하고 단일 명령을 실행한 다음 닫히기 때문에 다중은 su의미가 없습니다. 이후의 것들은 이전 디렉토리가 어느 디렉토리에 있는지 기억하지 못하며, 매번 호출 스크립트의 디렉토리를 상속받습니다.

완전한 명령은 (주석을 인용하는 다른 답변에서 인용됨)입니다.

ssh -t [email protected] ssh -t cserver 'su - gsxuserp -c \"tail -f ../random_directory/file_in_random_directory\"'

일련의 큰따옴표뿐만 아니라 작은따옴표와 큰따옴표(그리고 이스케이프된 큰따옴표)의 사용에 유의하세요.

답변2

이것은 답변이 아니며 추가 형식이 필요한 설명에 가깝습니다.

인용이 잘못되었습니다. 큰따옴표가 중첩되지 않고 전환됩니다.

ssh -t [email protected] "ssh -t bserver "su - gsxp -c "sqlplus grep_ro/pwd"""
# .... unquoted ..........^..quoted.......^..unquoted...^....quoted.........^^^

실제 ssh 명령에는 다음 매개변수가 있습니다.

ssh -t \
    [email protected] \
    "ssh -t bserver " \
    su  \
    -  \
    gsxp  \
    -c  \
    "sqlplus grep_ro/pwd"

이것이 더 합리적입니다:

ssh -t [email protected] ssh -t bserver "su - gsxp -c 'sqlplus grep_ro/pwd'"

관련 정보