I've got the following shell script (let say get_includes.sh
):
#!/usr/bin/env bash includes=($(grep ^#include file.c | grep -o '"[^"]\+"' | tr -d '"')) echo "0: ${includes[0]}" echo "1: ${includes[1]}"
which aims at finding relative include files in source code file.
So for given file like this (file.c
):
#include "foo.h" #include "bar.h" #include <stdio.h> int main(void) { printf("Hello World\n"); return 0; }
It'll return the following results which are correct:
$ ./get_includes.sh 0: foo.h 1: bar.h
The code works as expected, however shellcheck
complains about the following issues:
$ shellcheck get_includes.sh In get_includes.sh line 2: includes=($(grep ^#include file.c | grep -o '"[^"]\+"' | tr -d '"')) ^-- SC2207: Prefer mapfile or read -a to split command output (or quote to avoid splitting). For more information: https://www.shellcheck.net/wiki/SC2207 -- Prefer mapfile or read -a to spli... https://www.shellcheck.net/wiki/SC2236 -- Use -n instead of ! -z.
So:
- I can't quote command substitution, as I expect the command to expand to an array.
- I don't want to ignore the warning, I'd like to correct it.
I'm using Bash 4.
So, how I can correct the above line to satisfy shellcheck
? If possible, I'd like to keep it in one-liner.
I've tried the following approaches which failed:
$ (grep ^#include file.c | grep -o '"[^"]\+"' | read -a myarr; echo $myarr) (nothing is printed) $ (grep ^#include file.c | grep -o '"[^"]\+"' | mapfile myarr; echo $myarr) (nothing is printed)