{"id":33280,"date":"2019-09-17T13:45:10","date_gmt":"2019-09-17T13:45:10","guid":{"rendered":"https:\/\/blog.jetbrains.com\/go\/?p=1790"},"modified":"2019-10-01T08:58:45","modified_gmt":"2019-10-01T08:58:45","slug":"shell-scripting-with-goland-2019-2","status":"publish","type":"go","link":"https:\/\/blog.jetbrains.com\/zh-hans\/go\/2019\/09\/17\/shell-scripting-with-goland-2019-2","title":{"rendered":"Shell Scripting with GoLand 2019.2"},"content":{"rendered":"<p>Go developers may rely at times on shell scripts to perform various tasks. In this post, we\u2019ll have a look at the new builtin support for Shell scripting in GoLand 2019.2.<\/p>\n<p>First, we need a new shell script file, let\u2019s call it <em>hello.sh.<\/em><\/p>\n<p>As you have probably noticed already, the IDE will also prompt you to install a helper application. This is called <em><a href=\"https:\/\/github.com\/koalaman\/shellcheck\" target=\"_blank\" rel=\"noopener\">ShellCheck<\/a><\/em>, and it\u2019s going to help you check your shell scripts against common issues and look for possible optimizations.<\/p>\n<p><!--more--><\/p>\n<p>After we click the <em>Install<\/em> link, we can go on and start working on our script. We could skip installing the tool for now, of course, but having static analysis on our scripts is always better.<\/p>\n<p>As a small note, if you are using Windows like I am, make sure to change the line endings to LF.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-1796\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2019\/09\/go-new-shell-script-optimized.gif\" alt=\"new shell script\" width=\"1500\" height=\"700\" \/><\/p>\n<p>Starting the shell script with the traditional #! will also give us our first chance to interact with completion. Let\u2019s use bash as our interpreter.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-1791\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2019\/09\/go-file-header-completion-optimized.gif\" alt=\"shell script file header completion\" width=\"1500\" height=\"700\" \/><\/p>\n<p>The IDE will be able to suggest not only keywords but also directory names and relative\/full paths.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-1792\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2019\/09\/go-code-completion-optimized.gif\" alt=\"shell scripting code completion\" width=\"1500\" height=\"700\" \/><\/p>\n<p>And since we have installed <em>ShellCheck<\/em> already, let\u2019s see it in action and use the inspections\/quick-fix system that is similar to all the other programming languages that we support.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-1794\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2019\/09\/go-shell-script-quick-fix-optimized.gif\" alt=\"shell script quick-fix\" width=\"1500\" height=\"700\" \/><\/p>\n<p>You can also get help on the items that are being completed, by invoking the <em>Quick Documentation<\/em> feature. And that\u2019s not all. If you want an explanation of a more complex command, you can use <em>Alt+Enter<\/em> on the line with the command, then choose the <em>Explain shell<\/em> feature, and select the expression to you wish to have explained. This will open a browser window using the <a href=\"https:\/\/explainshell.com\/\" target=\"_blank\" rel=\"noopener\"><em>ExplainShell<\/em><\/a> website to describe the selected command.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-1798\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2019\/09\/go-explain-shell-optimized.gif\" alt=\"explain shell\" width=\"1500\" height=\"700\" \/><\/p>\n<p>Do you want to run the scripts? Then you have probably noticed there\u2019s a familiar green arrow at the top of the script and the same shortcuts you are used to will work as well. In my case, I need to run the script via WSL, which will hopefully be a bit more integrated in the future.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-1793\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2019\/09\/go-run-shell-script-optimized.gif\" alt=\"run shell script\" width=\"1500\" height=\"700\" \/><\/p>\n<p>Finally, to help make it easier to maintain the scripts, the IDE integrates with <a href=\"https:\/\/github.com\/mvdan\/sh\" target=\"_blank\" rel=\"noopener\"><em>Shfmt<\/em><\/a>, an open-source tool meant to standardize the formatting of shell scripts. To call it, invoke the built-in formatter tool and it will prompt you to install it automatically.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-1795\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2019\/09\/go-format-shell-script-optimized.gif\" alt=\"format shell script\" width=\"1500\" height=\"700\" \/><\/p>\n<p>So, why would you use an IDE for working on shell scripts? Because, as we\u2019ve seen, shell scripting does not need to be complex or inconsistent. GoLand all our other JetBrains IDEs are here to help you \u2013 even in cross-platform environments.<\/p>\n<p>Please let us know your thoughts on this in the comments section below, on <a href=\"https:\/\/youtrack.jetbrains.com\/issues\/Go\" target=\"_blank\" rel=\"noopener\">our issue tracker<\/a>, or <a href=\"https:\/\/twitter.com\/GoLandIDE\" target=\"_blank\" rel=\"noopener\">on Twitter<\/a>. And if there are any topics that you\u2019d like to see covered in the future on this blog, we\u2019ll be happy to do so.<\/p>\n","protected":false},"author":828,"featured_media":0,"comment_status":"open","ping_status":"open","template":"","categories":[808],"tags":[754,3954],"cross-post-tag":[],"acf":[],"_links":{"self":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/go\/33280"}],"collection":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/go"}],"about":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/types\/go"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/users\/828"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/comments?post=33280"}],"version-history":[{"count":0,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/go\/33280\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/media?parent=33280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/categories?post=33280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/tags?post=33280"},{"taxonomy":"cross-post-tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/cross-post-tag?post=33280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}