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

mod rewrite - Using "?" when rewriting the URL -

.htaccess: Transfer name to index.php if not directory public -

Admob integration with pygame in android -