{"id":32955,"date":"2020-03-14T13:21:38","date_gmt":"2020-03-14T07:51:38","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=32955"},"modified":"2020-03-16T10:42:43","modified_gmt":"2020-03-16T05:12:43","slug":"how-to-replace-bash-with-python-as-your-go-to-command-line-language","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/how-to-replace-bash-with-python-as-your-go-to-command-line-language\/","title":{"rendered":"How to Replace Bash with Python as Your Go-To Command Line Language"},"content":{"rendered":"<p>I have a touch of a love and hate relationship with bash. I spend tons of your time within the terminal, and bash is my default \u201c<strong>programming language<\/strong>\u201c. Sometimes I tell folks that find, grep and xargs run their infrastructure, and that they laugh and laugh until they realize I\u2019m serious.<\/p>\n<p>Picking up some Python may be a perfect choice for system administrators. it is also great for anyone who has got to affect anything during a terminal but doesn\u2019t want to use bash, or has needs that are too complex for bash. Once a task goes beyond<\/p>\n[pastacode lang=\u201dbash\u201d manual=\u201dfind%20%24(pwd)%20-name%20%22*.txt%22%20%7C%20xargs%20-I%20%7B%7D%20echo%20%22do%20stuff%20with%20%7B%7D%22\u2033 message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>it\u2019s time to interrupt out the Python!<\/p>\n<p>There are tons of advantages to using Python as your attend instruction language.<\/p>\n<ol>\n<li>Python has many nice libraries to assist out with just about anything. that has handling system operations, reading files, listing directories, writing for loops, checking for exit codes, and so on.<\/li>\n<li>Autocomplete with IDEs. Seriously. Who wants to possess to memorize anything?<\/li>\n<li>Robust testing suite if that\u2019s your thing (and if it isn\u2019t , you ought to consider making it your thing).<\/li>\n<li>The iPython console. It\u2019s wonderful. It\u2019s amazing. I LOVE IT.<\/li>\n<li>Python is out there on most systems, and if it isn\u2019t you\u2019ll catch on with <strong>Miniconda.<\/strong><\/li>\n<li>Robust error checking with attempt to catch blocks.<\/li>\n<li>If you\u2019re employed on different operating systems you\u2019ll use Python libraries which will affect all that under the hood.<\/li>\n<li>Even if you\u2019ve got no programming ability Python is a simple language to urge started with.<\/li>\n<\/ol>\n<h3 id=\"lets-get-started\">Let\u2019s get Started<\/h3>\n<p>To get started,\u00a0<span data-name=\"first off|first|firstly|foremost|first of all\">first\u00a0<\/span><span data-name=\"you'll need|you will need|you'll have\">you will need\u00a0<\/span>to either have Python installed or install it with Miniconda.<\/p>\n<h3 id=\"check-if-you-have-ipython-installed\">Check if you have iPython installed<\/h3>\n[pastacode lang=\u201dbash\u201d manual=\u201dwhich%20python%0Awhich%20ipython\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>If both of those are successful, you\u2019re in business! If you\u2019ve got Python, but not iPython, you\u2019ll need to install it. you\u2019ll install it as a system package, but i actually recommend that you simply just install it with Miniconda.<\/p>\n<h3 id=\"install-miniconda\">Install Miniconda<\/h3>\n<p>Grab the installer for your OS here. I suggest getting the Python3 installation.<\/p>\n<p>Then it\u2019s just\u00a0<span data-name=\"a simple|an easy|a straightforward\">an easy\u00a0<\/span>installation.<\/p>\n[pastacode lang=\u201dbash\u201d manual=\u201dbash%20Miniconda3-latest-Linux-x86_64.sh\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Follow the prompts\u00a0<span data-name=\"and you'll|and you will|and you may\">and you will\u00a0<\/span>have Miniconda3 installed. Once\u00a0<span data-name=\"you have|you've got|you have got\">you\u2019ve got\u00a0<\/span>it installed\u00a0<span data-name=\"you'll want|you'll be wanting|you will need\">you\u2019ll be wanting\u00a0<\/span>to run an update, because\u00a0<span data-name=\"this is|this is often|this can be\">this is often\u00a0<\/span>tech and\u00a0<span data-name=\"of course|in fact|after all\">in fact\u00a0<\/span><span data-name=\"you want|you would like|you wish\">you would like\u00a0<\/span>to run an update. \ud83d\ude09<\/p>\n[pastacode lang=\u201dbash\u201d manual=\u201dconda%20update%20conda%0Aconda%20config%20\u2013add%20channels%20conda-forge%0Aconda%20update%20-y%20\u2013all%0Aconda%20install%20-y%20ipython\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h3 id=\"troubleshooting\">Troubleshooting<\/h3>\n<p>If you\u2019ve got trouble installing any packages here are some tips.<\/p>\n<ol>\n<li>Run <span style=\"color: #ff0000;\">conda clean \u2013all<\/span> and check out again.<\/li>\n<li>Make sure you\u2019re using the right channel.<\/li>\n<li>Run <span style=\"color: #ff0000;\">conda update -y \u2013all<\/span><\/li>\n<li>Try to install as little as possible to your global conda space. Instead create environments for various tasks and projects, which we\u2019ll get into next.<\/li>\n<\/ol>\n<h3 id=\"create-environments-with-conda\">Create Environments with Conda<\/h3>\n<p>If you have ever used virtualenv, pipenv (is that a thing?), Rbenv, plenv, anyenv or any of the opposite various envs that have popped up over the years, this may sound very familiar to you. the thought is that different projects should have their own isolated software environments.<\/p>\n[pastacode lang=\u201dbash\u201d manual=\u201dconda%20create%20-n%20my-project%20ipython%20package1%20package2%20package2\u2033 message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><em>If you\u2019re like me and like to have iPython readily availabe make sure you install it to any new environments!<\/em><\/p>\n<h3 id=\"pythonlibrariesforsystemadministration\"><span id=\"python-libraries-for-system-administration\">Python Libraries for System Administration<\/span><\/h3>\n<p>Before we get into the examples let\u2019s just list some handy packages along side their docs.<\/p>\n<p>My attend package is that the <strong>os<\/strong> package. you\u2019ll use it to list directories, check if files exist, check if symlinks exist, make directories, run system commands, get and set environmental variables, and more. It\u2019s great!<\/p>\n<p>My second package for running system commands that do not exist as handy python libraries is that the <strong>subprocess<\/strong> module.<\/p>\n<p>The <strong>shutil<\/strong> has file operations that are not within the os library.<\/p>\n<p>The <strong>pprint<\/strong> library prints out complex data structures with nice indentation.<\/p>\n<p>The <strong>pytest<\/strong> library let\u2019s you test your Python code, because let\u2019s face it, nothing ever works correctly the primary (few) times.<\/p>\n<h3 id=\"howdoiexecutemycode\"><span id=\"how-do-i-execute-my-code\">How Do I Execute my Code?<\/span><\/h3>\n<p>Finally! Code!<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-large wp-image-32962\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2020\/03\/Screenshot-2019-12-13-10.33.52-1024x389.png\" alt=\"\" width=\"1024\" height=\"389\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2020\/03\/Screenshot-2019-12-13-10.33.52-1024x389.png 1024w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2020\/03\/Screenshot-2019-12-13-10.33.52-300x114.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2020\/03\/Screenshot-2019-12-13-10.33.52-768x292.png 768w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2020\/03\/Screenshot-2019-12-13-10.33.52-1536x584.png 1536w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2020\/03\/Screenshot-2019-12-13-10.33.52-990x377.png 990w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2020\/03\/Screenshot-2019-12-13-10.33.52-1320x502.png 1320w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2020\/03\/Screenshot-2019-12-13-10.33.52.png 1930w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>\u00a0<\/p>\n<p>When you\u2019re using Python for system administration you\u2019ll dive straight into the iPython console, or write scripts then execute them with<span style=\"color: #ff0000;\"> python name-of-script.py.<\/span><\/p>\n<p>If you favor to write down your scripts you\u2019ve got numerous choices, and it\u2019s truly a matter of private preference. i exploit <strong>PyCharm<\/strong>, which is paid, but\u00a0<strong>Visual Studio Code and Atom <\/strong>are equally excellent free choices.<\/p>\n<p>I find that it depends on what I\u2019m performing on . Sometimes I just open up the iPython console and begin typing, and other times i want something more robust with tests and whatnot.<\/p>\n<p>If you\u2019re using either the iPython console or any of the editors I listed above, you\u2019ll have autocomplete. Autocomplete is awesome! With iPython simply start typing your function and press tab to urge an inventory of potential functions you\u2019ll want.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-large wp-image-32963\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2020\/03\/Screenshot-2019-12-13-10.49.07-1024x124.png\" alt=\"\" width=\"1024\" height=\"124\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2020\/03\/Screenshot-2019-12-13-10.49.07-1024x124.png 1024w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2020\/03\/Screenshot-2019-12-13-10.49.07-300x36.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2020\/03\/Screenshot-2019-12-13-10.49.07-768x93.png 768w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2020\/03\/Screenshot-2019-12-13-10.49.07-1536x186.png 1536w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2020\/03\/Screenshot-2019-12-13-10.49.07-2048x248.png 2048w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2020\/03\/Screenshot-2019-12-13-10.49.07-990x120.png 990w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2020\/03\/Screenshot-2019-12-13-10.49.07-1320x160.png 1320w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>I cannot express how much I love autocomplete.<\/p>\n<h2 id=\"get-help\">Get Help<\/h2>\n<p>You can go to any of the doc pages for any library, but if you know the name of either the library or the function you can bring it up in iPython.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-32964\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2020\/03\/Screenshot-2019-12-13-10.55.14.png\" alt=\"\" width=\"562\" height=\"162\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2020\/03\/Screenshot-2019-12-13-10.55.14.png 562w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2020\/03\/Screenshot-2019-12-13-10.55.14-300x86.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2020\/03\/Screenshot-2019-12-13-10.55.14-559x162.png 559w\" sizes=\"(max-width: 562px) 100vw, 562px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-32965\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2020\/03\/Screenshot-2019-12-13-10.55.55-1024x821.png\" alt=\"\" width=\"1024\" height=\"821\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2020\/03\/Screenshot-2019-12-13-10.55.55-1024x821.png 1024w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2020\/03\/Screenshot-2019-12-13-10.55.55-300x240.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2020\/03\/Screenshot-2019-12-13-10.55.55-768x616.png 768w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2020\/03\/Screenshot-2019-12-13-10.55.55-1536x1231.png 1536w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2020\/03\/Screenshot-2019-12-13-10.55.55-990x793.png 990w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2020\/03\/Screenshot-2019-12-13-10.55.55-1320x1058.png 1320w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2020\/03\/Screenshot-2019-12-13-10.55.55.png 1874w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>You can bring up the help menu in most IDEs and text editors too, but that will be specific to your editor.<\/p>\n<h2 id=\"examples\">Examples<\/h2>\n<p>First you will need to import your packages<\/p>\n[pastacode lang=\u201dbash\u201d manual=\u201dimport%20os%0Aimport%20subprocess%0Aimport%20shutil%0Afrom%20pprint%20import%20pprint\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Here are some examples of common file and directory operations.<\/p>\n[pastacode lang=\u201dbash\u201d manual=\u201d%23%20Check%20if%20a%20path%20exists%0Aos.path.exists(\u2018%2Fpath%2Fon%2Ffilesystem\u2019)\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[pastacode lang=\u201dbash\u201d manual=\u201d%23%20List%20the%20contents%20of%20a%20directory%0A%23%20This%20returns%20a%20list%0Adir_list%20%3D%20os.listdir()%0Afor%20item%20in%20dir_list%3A%0A%20%20%20%20print(item)\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[pastacode lang=\u201dbash\u201d manual=\u201d%23%20Get%20the%20Absolute%20Path%20name%20of%20a%20file%20(file%20%2B%20current%20working%20dir)%0Aos.path.abspath(\u2018some-file\u2019)\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[pastacode lang=\u201dbash\u201d manual=\u201d%23Get%20the%20basename%20-%20returns%20file%0Aos.path.basename(\u2018%2Fpath%2Fto%2Ffile\u2019)\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[pastacode lang=\u201dbash\u201d manual=\u201d%23%20Split%20a%20directory%20path%20-%20platform%20independent%0Aos.path.split(os.getcwd())%0A%23%20Out%5B17%5D%3A%20(\u2018%2FUsers\u2019%2C%20\u2019jillian\u2019)\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[pastacode lang=\u201dbash\u201d manual=\u201d%23%20Split%20a%20directory%20path%20-%20platform%20independent%0Aos.path.split(os.getcwd())%0A%23%20Out%5B17%5D%3A%20(\u2018%2FUsers\u2019%2C%20\u2019jillian\u2019)\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[pastacode lang=\u201dbash\u201d manual=\u201d%23%20Check%20if%20a%20path%20is%20a%20symlink%0Aos.path.islink()\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Move files and directories around<\/p>\n[pastacode lang=\u201dbash\u201d manual=\u201d%23%20Copy%20a%20directory%0A%23%20cp%20-rf%0Ashutil.copytree(\u2018src\u2019%2C%20\u2019dest\u2019)\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[pastacode lang=\u201dbash\u201d manual=\u201d%23%20Copy%20a%20file%0A%23%20cp%20-rf%0Ashutil.copyfile(\u2018file1\u2019%2C%20\u2019file2\u2032\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[pastacode lang=\u201dbash\u201d manual=\u201d%23%20Move%20a%20directory%0A%23%20mv%0Ashutil.move(\u2018src\u2019%2C%20\u2019dest\u2019)\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Not everything is going to be available through python libraries, such as installing system libraries, so run a few system commands!<\/p>\n[pastacode lang=\u201dbash\u201d manual=\u201d%23%20Run%20an%20arbitrary%20system%20command%0Acommand%20%3D%20%22echo%20\u2019hello\u2019%22%0Aresult%20%3D%20subprocess.run(command.split(\u2018%20\u2019)%2C%20stdout%3Dsubprocess.PIPE%2C%20stderr%3Dsubprocess.PIPE)%0A%23Print%20the%20stdout%20and%20stderr%0Aprint(result.stdout)%0Aprint(result.stderr)\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Write to files!<\/p>\n[pastacode lang=\u201dbash\u201d manual=\u201d%23%20Write%20to%20a%20file%20(and%20create%20it%20if%20it%20doesn\u2019t%20exist)%0A%23%20echo%20%22hello%22%20%3E%20hello.txt%0Af%3D%20open(%22hello.txt%22%2C%22w%2B%22)%0Af.write(%22hello!%22)%0Af.close()\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[pastacode lang=\u201dbash\u201d manual=\u201d%23%20Append%20to%20a%20file%0A%23%20echo%20%22hello%22%20%3E%3E%20hello.txt%0Af%20%3D%20open(%22hello.txt%22%2C%20%22a%2B%22)%0Af.write(%22hello%20again!%22)%0Af.close()\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>\u00a0<\/p>\n<h2 id=\"writesometests\"><span id=\"write-some-tests\">Write some tests!<\/span><\/h2>\n<p>Tests mostly work by using a function called assert, which is essentially saying make sure this is true and if not die loudly.<\/p>\n[pastacode lang=\u201dbash\u201d manual=\u201ddef%20test_system_command()%3A%0A%20%20%20%20%22%22%22Test%20the%20exit%20code%20of%20a%20system%20command%22%22%22%0A%20%20%20%20command%20%3D%20%22echo%20\u2019hello\u2019%22%0A%20%20%20%20result%20%3D%20subprocess.run(command.split(\u2018%20\u2019)%2C%20stdout%3Dsubprocess.PIPE)%0A%20%20%20%20assert%20result.returncode%20%3D%3D%200\u2033 message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Put this function in a file called\u00a0<span style=\"color: #ff0000;\"><code><span style=\"color: #ff0000;\">test_my_code.py<\/span><\/code><\/span>\u00a0and run as\u00a0<span style=\"color: #ff0000;\"><code><span style=\"color: #ff0000;\">pytest test_my_code.py<\/span><\/code><\/span>.<\/p>\n<h2 id=\"wrap-up\">Wrap Up:<\/h2>\n<p>That\u2019s it for my main tips and tricks for using Python as your go-to bash replacement. subsequent time you would like to write down a loop in bash, consider breaking out the iPython console and seeing what you\u2019ll come up with instead!<a href=\"http:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.xda-developers.com%2Fhow-to-download-android-11-developer-preview-for-google-pixel-and-other-android-devices%2F&t=How%20to%20download%20Android%2011%20Developer%20Preview%20for%20Google%20Pixel%20and%20other%20Android%20devices\"><br \/>\n<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have a touch of a love and hate relationship with bash. I spend tons of your time within the terminal, and bash is my default \u201cprogramming language\u201c. Sometimes I tell folks that find, grep and xargs run their infrastructure, and that they laugh and laugh until they realize I\u2019m serious. Picking up some Python [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":33043,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4148],"tags":[86761,86766],"class_list":["post-32955","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python","tag-replace-bash-with-python"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/32955","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=32955"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/32955\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/33043"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=32955"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=32955"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=32955"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}