더 이상 Bash 스크립팅을 사용할 수 있습니까?
대학생으로 2 학년을 마쳤 기 때문에 "실제"지식이 부족합니다. 저는 첫해에 Java를 배웠고 Java를 계속 사용했으며 두 번째로 C 및 간단한 Bash 스크립팅을 선택했습니다. 이번 여름에 저는 Perl을 배우려고 노력하고 있습니다. 나는 과거에 파이썬을 조금 다루었습니다.
제 질문은 이제 Python, Ruby, Perl 등과 같은 매우 읽기 쉽고 쓰기가 쉬운 스크립팅 언어가 있는데 왜 Bash 스크립트를 작성하는 사람이 있습니까? 내가 놓친 것이 있습니까? 내 리눅스 상자에 펄과 파이썬이 있다는 것을 알고 있습니다. 충분히 유비쿼터스가 아닌가? Bash에서 다른 hll보다 더 쉬운 일이 정말로 있습니까?
무엇보다도 bash는 당신이하고있는 대부분 의 일이 다양한 프로그램들 사이에서 통신하고 배관 할 때 유용 합니다 (대부분도 표준입니다). 그리고 bash (또는 적어도 POSIX shell )는 사용할 수 있지만 Perl과 Python은 사용할 수없는 많은 환경 이 있습니다.
물론 대화 형 bash와 스크립팅 된 bash를 구분해야합니다. Ubuntu는 최근 기본 스크립트 셸 ( #!/bin/sh) 이 훨씬 더 빠르기 때문에 대시 로 전환했습니다 . 그러나 bash 에는 유용한 대화식 기능이 있습니다. 대시에는 없습니다 ( zsh 가 여전히 더 좋습니다, IMHO).
Bash는 시스템 관리, 웹 애플리케이션 배포, 데이터 크 런칭, 정상 백업, 심지어 는 일상적인 관리에 매우 유용 합니다. BASH와 같은 "베테랑 IT 군인"을 판단하기에는 너무 이르다고 생각합니다.
인터넷 검색 수정 :
또한 성공적인 OS-X 프로그램 인 TextMate 에는 내부에 많은 Bash 기계가 포함되어 있음을 언급하고 싶습니다 .
bash와 python의 실제 차이점은 python은 범용 스크립팅 언어 인 반면 bash는 단순히 수많은 작은 (그리고 종종 매우 빠른) 프로그램을 시리즈로 실행하는 방법이라는 것입니다. 파이썬은 이것을 할 수 있지만 최적화되어 있지 않습니다. 프로그램 (sort, find, uniq, scp)은 매우 복잡한 작업을 매우 간단하게 수행 할 수 있으며 bash를 사용하면 이러한 작업이 파일 또는 장치에서 출력을 플러시하고 파이핑과 매우 간단하게 상호 운용 할 수 있습니다.
Python은 동일한 프로그램을 실행할 수 있지만 동일한 작업을 수행하기 위해 python 스크립트에서 bash 스크립팅을 수행해야하며, 그러면 python과 bash가 모두 붙어 있습니다. 둘 다 그들 자체로는 괜찮지 만 이들의 혼합은 IMHO를 개선하지 않습니다.
Bash 스크립트를 사용하면 명령을 수동으로 입력 할 때 와 동일한 언어 를 사용하여 명령 줄 작업 을 자동화 할 수 있습니다.
$ PATH를 나열하는 간단한 예
세게 때리다
#!/bin/sh
echo $PATH
파이썬
import os
print os.getenv("path")
많은 작업에서 bash는 매우 간결 할 수 있습니다. 현재 디렉토리의 모든 파일에서 문자열을 검색하고 해당 행을 인쇄하는 코드 :
bash에서 :
grep -ir "search_term" .
Python에서 :
for file in os.listdir('.'):
file_content = open(file).readlines().split('\n')
for line in file_content:
if line.count("search_term") > 0:
print file, ": ", line
파이썬이 많은 작업에서 bash만큼 자연스럽지 않다는 강력한 주장은 아무도 파이썬 인터프리터를 쉘로 사용하지 않는다는 단순한 사실이라고 생각합니다. 분명히 이것은 가능합니다.
python> import os, subprocess
python> os.chdir(os.path.expanduser("~/work"))
python> subprocess.call(["vim","README"])
분명히 셸에 파이썬을 사용하는 것은 터무니 없습니다. Bash는 다른 프로그램을 실행하도록 설계되었습니다. 파이썬은 계산을 위해 설계되었습니다. 대부분의 작업은 어느 언어로든 수행 할 수 있지만 항상 python보다 bash에서 더 쉽게 수행 할 수있는 작업이 있으며 그 반대의 경우도 마찬가지입니다.
글쎄, bash로 작성할 때 스크립트의 명령 줄에서 가능한 모든 도구를 직접 사용할 수 있습니다. 다른 언어를 사용하는 경우 먼저 해당 명령을 실행하고 결과 등을 얻어야합니다. (예를 들어) 프로세스 목록을 가져오고 grep을 통해 실행되고 일부 결과를 얻는 간단한 스크립트는 다른 언어에서 훨씬 더 복잡합니다. 따라서 bash는 여전히 빠른 작업을 작성하는 데 좋은 도구입니다.
많은 시작 스크립트는 전통적으로 셸 스크립트로 작성되며 이러한 스크립트에서 벗어나는 경향이없는 것 같습니다.
쉘 스크립트는 다른 프로세스를 시작하고 입력 / 출력을 결합하는 데 완벽하게 적합합니다. 다른 스크립팅 언어가 그다지 강력하지 않은 영역입니다.
그 외에도 Perl, Python 및 Ruby와 같은 언어가 점점 더 보편화 되더라도 모든 유닉스 계열 시스템에서 실제로 보장 되는 유일한 언어 는 bourne 쉘입니다 (반드시 bash 형식은 아님).
다른 사람들의 말과는 별도로, 나는 Bash를 배우는 주된 이유가 무엇인지 지적하고 싶습니다. 그것은 (거의) 표준 Linux 쉘 입니다.
다른 스크립팅 언어는 확실히 유용하고 훨씬 더 강력 할 수도 있지만, 터미널이 앞에있을 때 다루게 될 것은 ... Bash입니다.
Linux 시스템을 관리하려면 I / O, 파이프 및 프로세스를 관리하고, 변수를 지정하고 사용할 수 있으며, 적어도 일부 루프 및 조건 평가를 수행 할 수 있어야합니다.
Bash는 스크립팅 언어 일뿐만 아니라 쉘입니다.
작업 제어 언어 및 셸
본문 : 셸 스크립트스크립팅 언어의 주요 클래스는 시스템 프로그램의 동작을 시작하고 제어하는 것과 관련된 작업 제어 자동화에서 성장했습니다. (이런 의미에서 쉘을 IBM JCL (Job Control Language)의 후손으로 생각할 수 있습니다. 이는 정확히이 목적을 위해 사용되었습니다.) 이러한 언어의 많은 인터프리터는 Unix 쉘이나 MS-DOS COMMAND.COM. AppleScript와 같은 다른 프로그램은 스크립트를 작성하기 위해 영어와 유사한 명령을 사용합니다. 이것은 Mac OS X의 Cocoa 프레임 워크와 결합되어 사용자가 AppleScript 및 Cocoa 객체를 사용하여 전체 애플리케이션을 빌드 할 수 있도록합니다.
Python, Ruby and Perl are great, but are more general tools; in certain cases (embedded devices or other minimalistic systems) they can be considered bloat or in other situations they might pose a security risk (for environments where you want very high security and try to eliminate any unnecessary package).
In some cases shell scripts will perform much better due to closer integration with OS.
Also, bash is production proven for myriad of administrative tasks and it is easy to find scripts that will cover very complicated scenarios (exceptions) and handle them gracefully.
Easier, probably not. I actually prefer perl to bash scripting in many cases. Bash does have one advantage, though, especially on Linux systems: it's all but guaranteed to be installed. And if it's not, its largely-compatible father (sh) will be, cause almost all system scripts are written for sh. Even perl isn't that ubiquitous, and it's everyfreakingwhere.
Bash (and the original Bourne sh and myriad derivatives) is - from one perspective - an incredibly high-level language. Where many languages use simple primitives, shell primitives are entire programs.
That it might not be the best language to express your tasks, doesn't mean it is dead, dying, or even moribund.
If you do lots of GUI stuff, you'll probably only meet bash whenever you're doing some sort of customization on your own machine. Various hacks and stuff. If you use the command line to do stuff, bash is just indispensable. In fact, being good on the command line requires bash or some other shell familiarity.
I get miles out of having learned Bash when I wanted to navigate around my harddrive quickly. I wrote a navigation/menu interface that let me beam to different folders and files quickly and easily. Writing it in bash was simple and easy. And there's lots of easily accessed, and free, stuff that'll show you how.
Also, learning Bash is great for understanding how Unix and some of the core stuff really works -- and how far we've come with tools like Python.
I'm a perl guy, but the number of the bash (or ksh) functions I use and create on a daily basis is quite significant. For anything involved, I'll write a perl script, but for navigating the directory structure, and specifically for manipulating environment variables bash/ksh/... are indispensable.
Again, especially for environment variables nothing beats shell, and quite a few programs use environment variables. In Perl, I have to write a bash alias or function that calls the Perl script, which writes out a temporary bash script, which then gets sourced after Perl exits in order to make the change in the same environment I'm launching from.
I've done this, especially for heavy-lifting on path variables. But there's no way to do it in just Perl (or python or ruby... or C-code for that matter).
You may get a true bournesh over at freshmeat (on Mac OS X, for example, /bin/sh is not a true bournesh).
How to identify a traditional Bourne shell? ... A simple check for an often undocumented but characteristic feature: You can use the circumflex ^ (caret) as replacement for | (pipe).
see: http://www.in-ulm.de/~mascheck/bourne/
In my experience, Perl meets something like 99% of any need that might require a shell script. As a bonus, it is possible to write code that runs on Windows sans Cygwin. If I won't have a Perl install on a Windows box I want to target, I can use PAR::Packer or PerlApp to produce an executable. Python, Ruby and others should work just as well, too.
However, shell scripting isn't all that complicated--at least things that you should be scripting in a shell aren't all that complicated. You can do what you need to do with a fairly shallow level of knowledge.
Learn how to read and set variables. How to create and call functions. How to source other files. Learn how flow control works.
And most important, learn to read the shell man page. This may sound facetious, but I am 100% serious--don't worry about cramming every detail of shell scripting into your brain, instead learn to find what you need to know in the man page quickly and efficiently. If you find yourself using shell scripting often, the pertinent info will naturally stick in your brain.
So, yes, basic shell is worth learning.
As mentioned, the GNU tools are great, and are easiest to use within the shell. It is especially nice if your data is already in a linear or tabular form of plain text. Just as an example, the other day I was able to build a script to create an XHTML word cloud of any text file in 8 lines of Bourne Shell, which is even less powerful (but more widely supported) than Bash.
What I don't get is why people say bash when they mean any bourne-shell compatible shell.
When writing shell scripts: always try to use constructs that also work in older bourne shell interpreters as well. It will save you lots of trouble some day.
And yes, there is plenty of use for shell scripts today, as the shell always exist on all unixes, out of the box, contrary to perl, python, csh, zsh, ksh (possibly?), and so on. Most of the time they only add extra convenience or different syntax for constructs like loops and tests. Some have improved redirection features.
Most of the time, I would say that ordinary bourne shell works equally well.
Typical pitfall: if ! test $x -eq $y works as expected in bash that has a more clever builtin "if" operator, but the "correct" if test ! $x -eq $y should work in all environments.
참고URL : https://stackoverflow.com/questions/2872041/is-there-any-use-for-bash-scripting-anymore
'program story' 카테고리의 다른 글
| JavaScript의 루프 타이머 (0) | 2020.11.03 |
|---|---|
| 키와 관련하여 사전 정렬 (0) | 2020.11.03 |
| Android에서 DigestUtils를 사용하여 메서드를 찾을 수 없음 (0) | 2020.11.03 |
| Asp.net MVC4 : 컨트롤러와 작업 모두에 대한 권한 부여 (0) | 2020.11.03 |
| 작업자 스레드를 통해 ObservableCollection을 업데이트하려면 어떻게해야합니까? (0) | 2020.11.03 |