Create variable BASH command -


i try customize little bash script special proprose.

i'm confuse bash scripting when come time assigne command variable.

my broken code:

if [ -n "$2" ]     top=`| head -n $2` fi awk '{print $17, ">", $19;}' $logs_folder$i | sort -g | uniq -c | sort -r -g $top 

so default return line if user specified number, add head command

use array form instead:

if [ -n "$2" ]     top=(head -n "$2") else     top=(cat) fi awk '{print $17, ">", $19;}' "$logs_folder$i" | sort -g | uniq -c | sort -r -g | "${top[@]}" 

and try add more quotes ("").

actually can't save pipe variable , let bash parse normal way when it's expanded can replace command instead (cat).

you use eval it's pretty delicate:

if [ -n "$2" ]     top="| head -n \"\$2\"" fi eval "awk '{print $17, \">\", \$19;}' \"\$logs_folder\$i\" | sort -g | uniq -c | sort -r -g $top" 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -