실행 중 셸 스크립트 편집
실행중인 쉘 스크립트를 편집하고 변경 사항이 실행중인 스크립트에 영향을 미치도록 할 수 있습니까?
csh 스크립트의 특정 사례에 대해 궁금합니다. 배치가 여러 가지 빌드 버전을 실행하고 밤새도록 실행됩니다. 작업 중에 뭔가가 발생하면 들어가서 추가 명령을 추가하거나 실행되지 않은 명령을 주석 처리하고 싶습니다.
가능하지 않은 경우이를 수행 할 수있는 쉘 또는 배치 메커니즘이 있습니까?
물론 시도해 보았지만 효과가 있는지 확인하기까지는 몇 시간이 걸리며, 뒤에서 무슨 일이 일어나고 있는지 아닌지 궁금합니다.
스크립트는 그렇게 작동하지 않습니다. 실행중인 사본은 편집중인 소스 파일과 독립적입니다. 다음에 스크립트가 실행될 때 가장 최근에 저장된 소스 파일 버전을 기반으로합니다.
이 스크립트를 여러 파일로 나누고 개별적으로 실행하는 것이 좋습니다. 이렇게하면 실행 시간이 줄어 듭니다. (즉, 배치를 하나의 빌드 플레이버 스크립트로 분할하고 각 스크립트를 개별적으로 실행하여 문제를 일으키는 원인을 확인합니다).
해결 방법 은 이 답변, 섹션 3 을 참조하십시오 .
그것은 않습니다 내 환경에서 최소한의 bash에서, 그러나에 영향을 미치는 매우 불쾌한 방법 . 이 코드를 참조하십시오. 첫째 a.sh
:
#!/bin/sh
echo "First echo"
read y
echo "$y"
echo "That's all."
b.sh
:
#!/bin/sh
echo "First echo"
read y
echo "Inserted"
echo "$y"
# echo "That's all."
하다
$ cp a.sh run.sh
$ ./run.sh
$ # open another terminal
$ cp b.sh run.sh # while 'read' is in effect
$ # Then type "hello."
제 경우 출력은 항상 다음과 같습니다.
여보세요 여보세요 그게 다야. 그게 다야.
이것은 예측할 수 없으므로 위험합니다. 해결 방법 은 이 답변, 섹션 3 을 참조하십시오 .
[추가됨] 정확한 동작은 하나의 추가 줄 바꿈에 따라 달라지며 아마도 Unix 버전, 파일 시스템 등에 따라 달라집니다. 단순히 영향을보고 싶다면 b.sh 앞뒤에 "echo foo / bar"를 추가하면됩니다. "읽기"줄.
시도해보세요 ... "bash-is-odd.sh"라는 파일을 만듭니다.
#!/bin/bash
echo "echo yes i do odd things" >> bash-is-odd.sh
이는 bash가 실제로 "당신이가는대로"스크립트를 해석한다는 것을 보여줍니다. 실제로 장기간 실행되는 스크립트를 편집하면 임의의 문자를 삽입하는 등 예측할 수없는 결과가 발생합니다. 이유는 무엇입니까? bash는 마지막 바이트 위치에서 읽기 때문에 편집하면 현재 읽고있는 문자의 위치가 이동합니다.
Bash는 한마디로이 "기능"때문에 매우 안전하지 않습니다. bash 스크립트와 함께 사용할 때 svn 및 rsync는 기본적으로 결과를 "병합"하기 때문에 특히 문제가됩니다. rsync에는이를 수정하는 모드가 있습니다. svn과 git은 그렇지 않습니다.
해결책을 제시합니다. "/ bin / bashx"라는 파일을 만듭니다.
#!/bin/bash
source "$1"
이제 스크립트에서 #! / bin / bashx를 사용하고 항상 bash 대신 "bashx"로 실행하십시오. 이렇게하면 문제가 해결됩니다. 스크립트를 안전하게 재 동기화 할 수 있습니다.
@ AF7에서 제안 / 테스트 한 대체 (인라인) 솔루션 :
{
# your script
}
exit $?
중괄호는 편집을 방지하고 종료는 추가를 방지합니다. 물론 bash에 '-w'(전체 파일)와 같은 옵션이 제공되거나이를 수행 한 것이 있으면 훨씬 나아질 것입니다.
Break your script into functions, and each time a function is called you source
it from a separate file. Then you could edit the files at any time and your running script will pick up the changes next time it gets sourced.
foo() {
source foo.sh
}
foo
Good question! Hope this simple script helps
#!/bin/sh
echo "Waiting..."
echo "echo \"Success! Edits to a .sh while it executes do affect the executing script! I added this line to myself during execution\" " >> ${0}
sleep 5
echo "When I was run, this was the last line"
It does seem under linux that changes made to an executing .sh are enacted by the executing script, if you can type fast enough!
I don't have csh installed, but
#!/bin/sh
echo Waiting...
sleep 60
echo Change didn't happen
Run that, quickly edit the last line to read
echo Change happened
Output is
Waiting...
/home/dave/tmp/change.sh: 4: Syntax error: Unterminated quoted string
Hrmph.
I guess edits to the shell scripts don't take effect until they're rerun.
If this is all in a single script, then no it will not work. However, if you set it up as a driver script calling sub-scripts, then you might be able to change a sub-script before it's called, or before it's called again if you're looping, and in that case I believe those changes would be reflected in the execution.
An interesting side note - if you are running a Python script it does not change. (This is probably blatantly obvious to anyone who understands how shell runs Python scripts, but thought it might be a useful reminder for someone looking for this functionality.)
I created:
#!/usr/bin/env python3
import time
print('Starts')
time.sleep(10)
print('Finishes unchanged')
Then in another shell, while this is sleeping, edit the last line. When this completes it displays the unaltered line, presumably because it is running a .pyc
? Same happens on Ubuntu and macOS.
I'm hearing no... but what about with some indirection:
BatchRunner.sh
Command1.sh
Command2.sh
Command1.sh
runSomething
Command2.sh
runSomethingElse
Then you should be able to edit the contents of each command file before BatchRunner gets to it right?
OR
A cleaner version would have BatchRunner look to a single file where it would consecutively run one line at a time. Then you should be able to edit this second file while the first is running right?
usually, it uncommon to edit your script while its running. All you have to do is to put in control check for your operations. Use if/else statements to check for conditions. If something fail, then do this, else do that. That's the way to go.
참고URL : https://stackoverflow.com/questions/3398258/edit-shell-script-while-its-running
'code' 카테고리의 다른 글
SRC, OBJ 및 BIN 하위 디렉토리가있는 C 프로젝트 용 Makefile을 어떻게 만들 수 있습니까? (0) | 2020.10.15 |
---|---|
Mavericks로 느린 IOS 시뮬레이터 (0) | 2020.10.15 |
Android Studio의 "res"폴더로 이미지를 가져 오거나 복사하는 방법은 무엇입니까? (0) | 2020.10.14 |
Swift에서 'catch all'예외의 세부 사항을 인쇄하는 방법은 무엇입니까? (0) | 2020.10.14 |
JDK와 Java SDK의 차이점 (0) | 2020.10.14 |