0

I need to print command in variable. I know how to do it:

a=$(uname -r)
echo $a

But if I have more commands in script and I need to print only kernel, how can I do it?

./script.sh -k
--------
kernel=5.13.0-28-generic

And also what if I want to print only 'help' ? Thanks!

  • 2
    This question is unclear to me. We have no idea what your script does or even what your intent is. You just want to print kernel? literally the word kernel or the kernel version? From your output it looks like the only thing your script outputs is kernel. If you want to save your script output in a variable it is the same a=$(./script.sh -k) – jesse_b Apr 03 '22 at 13:16
  • For example, I have script: a=$(uname -r) b=$(nproc) If I start it, it write me kernel version and cpu nums, but what if I want to start script with ./script.sh -k to print only kernel version without nproc, how I can do it? – lolilaliaa Apr 03 '22 at 13:20
  • 3
  • if -k is the only possible option or argument you'd ever send to your script, it can be simplified down to if [[ "$1" != "-k" ]] ; then b=$(noproc) ; echo $b ; fi, or even just [[ "$1" == "-k" ]] && exit on the line after echo $a, but if your options and script are more complicated than that, check steeldriver's link. – frabjous Apr 03 '22 at 14:11
  • Is the question about parsing parameters (and options)? – U. Windl Apr 05 '22 at 09:15

0 Answers0