#!/bin/sh
## Example: a typical script with several problems
for f in $(ls *.m3u)
do
grep -qi hq.*mp3 $f \
&& echo -e 'Playlist $f contains a HQ file in mp3 format'
done
#!/bin/sh
## Example: The shebang says 'sh' so shellcheck warns about portability
## Change it to '#!/bin/bash' to allow bashisms
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
#!/bin/bash
## Example: ShellCheck can detect some higher level semantic problems
while getopts "nf:" param
do
case "$param" in
f) file="$OPTARG" ;;
v) set -x ;;
esac
done
case "$file" in
*.gz) gzip -d "$file" ;;
*.zip) unzip "$file" ;;
*.tar.gz) tar xzf "$file" ;;
*) echo "Unknown filetype" ;;
esac
if [[ "$$(uname)" == "Linux" ]]
then
echo "Using Linux"
fi
#!/bin/bash
## Example: ShellCheck can detect many different kinds of quoting issues
if ! grep -q backup=true.* "~/.myconfig"
then
echo 'Backup not enabled in $HOME/.myconfig, exiting'
exit 1
fi
if [[ $1 =~ "-v(erbose)?" ]]
then
verbose='-printf "Copying %f\n"'
fi
find backups/ \
-iname *.tar.gz \
$verbose \
-exec scp {} “myhost:backups” +
ShellCheck
finds bugs in your shell scripts.
You can cabal
, apt
, dnf
, pkg
or brew install
it locally right now.
Paste a script to try it out:
📄
Your Editor (
Ace – loading 800kb of JS)
▼
▲
#!/usr/bin/env bash
mkdir -p release
pushd release
case "$1" in
install)
ninja && sudo ninja install
installed=1
;;
llvm)
cmake ../llvm -G Ninja -DLLVM_TARGETS_TO_BUILD='X86' -DLLVM_CCACHE_BUILD=On -DLLVM_ENABLE_PROJECTS='clang;libcxx;libcxxabi;compiler-rt;lld' -DCMAKE_BUILD_TYPE=Release -DLLVM_BUILD_LLVM_DYLIB=On -DCMAKE_CXX_FLAGS='-D_GLIBCXX_USE_CXX11_ABI=1' ../llvm && \
ninja
;;
ccls)
cmake .. -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_EXPORT_COMPILE_COMMANDS=On -DSYSTEM_CLANG=On -DUSE_SHARED_LLVM=On -DCMAKE_PREFIX_PATH="$HOME/git/llvm-project/release;$HOME/git/llvm-project/release/tools/clang;$HOME/git/llvm-project/llvm;$HOME/git/llvm-project/clang" && \
ninja
;;
rdp)
cmake .. -G Ninja -DCMAKE_EXPORT_COMPILE_COMMANDS=On && \
ninja
;;
fish)
cmake .. -G Ninja -DCMAKE_EXPORT_COMPILE_COMMANDS=On && \
ninja
;;
copyq)
cmake .. -G Ninja -DCMAKE_EXPORT_COMPILE_COMMANDS=On && \
ninja
;;
obs)
cmake .. -G Ninja -DUNIX_STRUCTURE=0 -DCMAKE_EXPORT_COMPILE_COMMANDS=On && \
ninja
;;
*)
cmake .. -G Ninja -DUNIX_STRUCTURE=0 -DCMAKE_EXPORT_COMPILE_COMMANDS=On && \
ninja
;;
esac && \
case "$2" in
install)
[ -z $installed ] && sudo ninja install
;;
esac
popd
ln -sf release/compile_commands.json .
If you paste a script in the editor above, this window will show shellcheck output.