Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use shellcheck in linux

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Xiaobian to share with you how to use shellcheck in linux, I believe most people still do not know how to use it, so share this article for your reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!

shellcheck

Shellcheck is one such tool. It can be used in a variety of scenarios, including online, command-line inspection, and editor configuration, described below.

online use

As the name suggests, it provides an online check address, https://www.shellcheck.net/, which can be accessed at the URL.

For example, you enter the content of your script:

#!/ bin/sh for n in {1..$ RANDOM} do str="" if (( n % 3 == 0 )) then str="fizz" fi if [ $[n%5] == 0 ] then str="$strbuzz" fi if [[ ! $str ]] then str="$n" fi echo "$str" done

shell

It will give you an error message or advice:

Line 2: for n in {1..$ RANDOM} ^-- SC2039: In POSIX sh, brace expansion is undefined. ^-- SC2039: In POSIX sh, RANDOM is undefined. Line 5: if (( n % 3 == 0 )) ^-- SC2039: In POSIX sh, standalone ((..)) is undefined. Line 9: if [ $[n%5] == 0 ] ^-- SC2039: In POSIX sh, $[..] in place of $((..)) is undefined. ^-- SC2007: Use $((..)) instead of deprecated $[..] ^-- SC2039: In POSIX sh, == in place of = is undefined. Line 11: str="$strbuzz" ^-- SC2154: strbuzz is referenced but not assigned. Line 13: if [[ ! $str ]] ^-- SC2039: In POSIX sh, [[ ]] is undefined.

Well, isn't it great that every possible error is pointed out. Novice shell writing inexplicable error, you can try to use Austria. Of course, many examples are not true errors, but some writing does not conform to POSIX standards, which should also be avoided.

command line using

Command line installation is also very simple (remember to use root privileges), ubuntu:

$ apt-get install shellcheck

Under Centos:

$ yum -y install epel-release

Under Fedora:

$ dnf install ShellCheck

The method of use is also very simple:

$ shellcheck myscript.sh

For example, the following is one of the most common mistakes beginners make:

//Source: Public Number [Programming Jewelry]//Author: Mr. Watch #!/ bin/bash if[ $# -eq 0 ] then echo "no para" else echo "$# para" fi exit 0

See the error message:

./ test.sh: line 4: if[ 0 -eq 0 ]: command not found ./ test.sh: line 5: syntax error near unexpected token `then' ./ test.sh: line 5: `then'

Just tell you that there is a grammar problem around then. What is the problem? Let's look at ShellCheck:

$ shellcheck test.sh In test.sh line 4: if[ $# -eq 0 ] ^-- SC1069: You need a space before the [.

It is clear from this that there is a lack of space in front.

editor using

Of course, you can also install it into your familiar editor, although they all have syntax highlighting functions, but there is no direct information prompt, install shellcheck tools, to achieve the effect of writing prompt.

Emacs, you can use Flycheck.

Sublime, you can use Sublime Linter.

Atom, you can use Linter.

vim , you can use ale or syntastic

Of course, many modern IDEs have this check function, only the editor here.

Syntastic, for example, actually supports syntax checking in multiple languages.

Installation process:

1. Install pathogen.vim

$ mkdir -p ~/.vim/autoload ~/.vim/bundle && \ curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim

And configure the following in the vimrc file:

execute pathogen#infect()

2. Install Syntastic

cd ~/.vim/bundle && \ git clone --depth=1 https://github.com/vim-syntastic/syntastic.git

3. Test installation

Open vim and enter

:Helptags

If there is no error, the installation is normal.

Configure the following in vimrc:

set statusline+=%#warningmsg# set statusline+=%{SyntasticStatuslineFlag()} set statusline+=%* let g:syntastic_always_populate_loc_list = 1 let g:syntastic_auto_loc_list = 1 let g:syntastic_check_on_open = 1

Commonly used:

:Errors Display Error Panel:lnext to Next Error:lprevious to Previous Error

More installation details can also be found at https://github.com/vim-syntastic/syntastic.

The above is the recommended installation method of the official website. You can also install Vundle (this is an old plug-in management method. You can try other plug-in management tools such as vim-plug) by adding:

Plugin 'scrooloose/syntastic'

Open vim Enter:

:PluginInstall

It can be installed.

Effect:

Shell check:

C syntax checker:

In fact, it supports syntax checking for almost all common programming languages.

Don't know where the vimrc file is?

Open vim and type:

:version

You can see:

system vimrc file: "$VIM/vimrc" user vimrc file: "$HOME/.vimrc" 2nd user vimrc file: "~/.vim/vimrc" user exrc file: "$HOME/.exrc" system gvimrc file: "$VIM/gvimrc" user gvimrc file: "$HOME/.gvimrc" 2nd user gvimrc file: "~/.vim/gvimrc" system menu file: "$VIMRUNTIME/menu.vim"

The difference between them is that the scope of effect is different. The vimrc for users naturally only takes effect for specific users.

The above is "how to use shellcheck in linux" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report