search
HomeBackend DevelopmentPython Tutorial会一门脚本语言,学 bash 就显得浪费时间?

首先bash虽然有if_else,可以调系统命令。但是许多局限性比如不支持多线程,没有库。所以会py或者ruby再去学bash,这是没必要 的。

回复内容:

日常工作,很多工作都是很琐碎 很细节的,你想不到你会遇到什么具体的小任务。
这时,你总不能打开vim 写个python脚本。
很可能还没写完#! ,shell 可能已经搞定了任务了。

所以会shell,熟练掌握各种命令,会sed awk,给日常工作带来很大的效率提升。
相信每一个Linux下的讨饭吃的程序员都深有体会。

琐碎任务一次性的任务交给shell; 注定要扩展,代码量不小,要维护的任务交给python.
需要效率的工作交给C。从来就不冲突。 学习 bash,并不仅仅是学习 bash 本身的语法,而是要去学习一下各种命令行工具的使用方式……
说到底,一切都是为了提高生产效率啊! 有py shell和ruby shell等等一大堆东西 另外一个角度看, 是shell script 让你拥有system utility的能力, 所有已安装的程序都洗干净了排着队等你调戏。sed, awk, kill, cat, ps, mkdir, rm, rsync, ..... 数都数不过来的别人幸苦调教好的漂亮姑娘,而*nix与生俱来的特性(redirection, pipe ...)又让它们可以很好的协同工作(因此do one thing, well) 。

如果你的程序有很多custom logic, 需要面向复杂些的数据结构,shell 的笨拙就会比较明显,这个时候就去找sexy的Python吧。 用Linux,bash还是要懂的。但是bash的语法确实有点奇怪(通常都是把c的语法当做是标准语法,所以说一门语言语法奇怪,就是说这门语言跟c的语法规则相去甚远),所以要想写好用的script,最好是学一门基于c的比较现代的语言。我现在用的是Python,然后需要执行bash的命令,就调用os,基本上所以的bash命令都可以用os.system("")这种方式运行。迄今为止,我唯一发现不能用os.system运行的就是 cd. Python中可以用os.chdir("")来实现 bash 的 cd. 今天服务器异地迁徙,我的shell脚本成功运维了500台服务器,包括对对等拷贝虚拟机配置按IP修改,组件启停,日志备份,服务器运行状态监控。shell起码不需要python解释器,代码上又异常优雅,你觉得shell不舒服,还是受到传统命令式编程的框架桎梏。每次打开vim看到花花绿绿又十分简洁的shell都让人陶醉,谁他妈再让我写Java或者python 去搞运维我就跟谁急 如果你是写需要长期运营的脚本,强烈推荐用python(或ruby等其他),倒不是说这样会更快,更方便,而是可读和可维护会好些,类似的还有makefile等,bash、sed、awk和make中那些奇怪的语法和符号的学习维护成本真的不如python中多啰嗦几句,而且用python可以做到一招通杀大多数需求,不然你的脑子得在bash,sed,awk之间来回切换
但如果你的脚本可能只是运行个一次,或临时一条命令,用bash还是很方便的
所以,bash不用很精通,但要保证会到一定水平,日常的工作区分来做
然而你也不可避免会碰到阅读维护其他人代码的情况,所以还是大致要了解下,比如if的语法,至于if里面的-a -o -f之类的可以问度娘了 当然不是浪费时间了(不然你让我们这种会用shell的情何以堪...)

假设路上有一坨屎, 你是用铲子(shell)铲掉, 还是开个铲车(python)来搞?
当然, 也会有粪坑, 但是大部分情况下是路上横躺了一坨屎, 极有可能还是别人丢过来的.

而且shell也没那么恶心嘛. 真正的脚本呢. 在*nix服务器下调试程序,查看程序debug日志或者运行日志,如果不懂shell,蛋疼死你 bash和命令行都设计拙劣。但现状如此,你不学就寸步难行。
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What are the alternatives to concatenate two lists in Python?What are the alternatives to concatenate two lists in Python?May 09, 2025 am 12:16 AM

There are many methods to connect two lists in Python: 1. Use operators, which are simple but inefficient in large lists; 2. Use extend method, which is efficient but will modify the original list; 3. Use the = operator, which is both efficient and readable; 4. Use itertools.chain function, which is memory efficient but requires additional import; 5. Use list parsing, which is elegant but may be too complex. The selection method should be based on the code context and requirements.

Python: Efficient Ways to Merge Two ListsPython: Efficient Ways to Merge Two ListsMay 09, 2025 am 12:15 AM

There are many ways to merge Python lists: 1. Use operators, which are simple but not memory efficient for large lists; 2. Use extend method, which is efficient but will modify the original list; 3. Use itertools.chain, which is suitable for large data sets; 4. Use * operator, merge small to medium-sized lists in one line of code; 5. Use numpy.concatenate, which is suitable for large data sets and scenarios with high performance requirements; 6. Use append method, which is suitable for small lists but is inefficient. When selecting a method, you need to consider the list size and application scenarios.

Compiled vs Interpreted Languages: pros and consCompiled vs Interpreted Languages: pros and consMay 09, 2025 am 12:06 AM

Compiledlanguagesofferspeedandsecurity,whileinterpretedlanguagesprovideeaseofuseandportability.1)CompiledlanguageslikeC arefasterandsecurebuthavelongerdevelopmentcyclesandplatformdependency.2)InterpretedlanguageslikePythonareeasiertouseandmoreportab

Python: For and While Loops, the most complete guidePython: For and While Loops, the most complete guideMay 09, 2025 am 12:05 AM

In Python, a for loop is used to traverse iterable objects, and a while loop is used to perform operations repeatedly when the condition is satisfied. 1) For loop example: traverse the list and print the elements. 2) While loop example: guess the number game until you guess it right. Mastering cycle principles and optimization techniques can improve code efficiency and reliability.

Python concatenate lists into a stringPython concatenate lists into a stringMay 09, 2025 am 12:02 AM

To concatenate a list into a string, using the join() method in Python is the best choice. 1) Use the join() method to concatenate the list elements into a string, such as ''.join(my_list). 2) For a list containing numbers, convert map(str, numbers) into a string before concatenating. 3) You can use generator expressions for complex formatting, such as ','.join(f'({fruit})'forfruitinfruits). 4) When processing mixed data types, use map(str, mixed_list) to ensure that all elements can be converted into strings. 5) For large lists, use ''.join(large_li

Python's Hybrid Approach: Compilation and Interpretation CombinedPython's Hybrid Approach: Compilation and Interpretation CombinedMay 08, 2025 am 12:16 AM

Pythonusesahybridapproach,combiningcompilationtobytecodeandinterpretation.1)Codeiscompiledtoplatform-independentbytecode.2)BytecodeisinterpretedbythePythonVirtualMachine,enhancingefficiencyandportability.

Learn the Differences Between Python's 'for' and 'while' LoopsLearn the Differences Between Python's 'for' and 'while' LoopsMay 08, 2025 am 12:11 AM

ThekeydifferencesbetweenPython's"for"and"while"loopsare:1)"For"loopsareidealforiteratingoversequencesorknowniterations,while2)"while"loopsarebetterforcontinuinguntilaconditionismetwithoutpredefinediterations.Un

Python concatenate lists with duplicatesPython concatenate lists with duplicatesMay 08, 2025 am 12:09 AM

In Python, you can connect lists and manage duplicate elements through a variety of methods: 1) Use operators or extend() to retain all duplicate elements; 2) Convert to sets and then return to lists to remove all duplicate elements, but the original order will be lost; 3) Use loops or list comprehensions to combine sets to remove duplicate elements and maintain the original order.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment