0

I have a shell script name count.sh; inside this script I have multiple variables, like $INPUT_1, $INPUT_2, $OUTPUT_1, $OUTPUT_2; each variable represents a file. I want to count the lines of each file with wc -l , I tried wc -l $INPUT_1 and it worked, but what I want to do is to find a way to apply wc -l on every variable whose name contains the keyword INPUT or OUTPUT, similar principle with ls *keyword*. I am not sure if it is possible.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Jinn
  • 5
  • 1
  • `ls` is not a keyword at all -- it's an external command; if it didn't exist in `/bin` or `/usr/bin` it would just be a "command not found" error. – Charles Duffy Oct 01 '20 at 15:44
  • BTW, instead of `INPUT_1` / `INPUT_2` / etc, have you considered using an array? `inputs=( [1]="something" [2]="something else" )`, then it's just `for input in "${inputs[@]}"; do ...` – Charles Duffy Oct 01 '20 at 15:45
  • It's messy, but you can use `set | sed -n '/^[_[:alpha:][[:alnum:]_]*=/s/=.*//p'` to get a list of all variables, exported and not exported, and you can then filter that to select the names you want (`grep -E -e 'INPUT|OUTPUT'`) and then you have variable names — and if you get `varname=INPUT_1`, you can use `${!varname}` to get the value of `$INPUT_1`. It isn't a good way of doing things, though. – Jonathan Leffler Oct 01 '20 at 16:09
  • Actually the variable names are created by others, they may be $INPUT_AB, $OUTPUT_g, etc. The only certain thing is that the variables all contain keyword INPUT/OUTPUT. I want to write a shell script count.sh so when others execute count.sh in their scripts, all variables including the keyword INPUT or OUTPUT will selected and counted lines (each variable contain a specific file) – Jinn Oct 01 '20 at 16:11

0 Answers0