shell - Comment définir une variable à la sortie d'une commande dans Bash

Mots clés : bashshellcommand-linebashunixposixbashbashbash

meilleur 4 Réponses shell - Comment définir une variable à la sortie d'une commande dans Bash

vote vote

94

OUTPUT=$(ls -1) echo "${OUTPUT}"  MULTILINE=$(ls \    -1) echo "${MULTILINE}" 
vote vote

87

$(sudo run command) 
#!/bin/bash  VAR1="$1" VAR2="$2"  MOREF=`sudo run command against "$VAR1" | grep name | cut -c7-`  echo "$MOREF" 
vote vote

70

myPi=`echo '4*a(1)' | bc -l` echo $myPi  3.14159265358979323844 
myPi=$(bc -l <<<'4*a(1)') 
SysStarted=$(date -d "$(ps ho lstart 1)" +%s) echo $SysStarted  1480656334 
df -k / Filesystem     1K-blocks   Used Available Use% Mounted on /dev/dm-0         999320 529020    401488  57% / 
array=($(df -k /)) 
declare -p array declare -a array='([0]="Filesystem" [1]="1K-blocks" [2]="Used" [3]="Available" [ 4]="Use%" [5]="Mounted" [6]="on" [7]="/dev/dm-0" [8]="999320" [9]="529020" [10]= "401488" [11]="57%" [12]="/")' 
echo ${array[9]} 529020 
{ read -r _;read -r filesystem size using avail prct mountpoint ; } < <(df -k /) echo $using 529020 
declare -p avail filesystem mountpoint prct size using declare -- avail="401488" declare -- filesystem="/dev/dm-0" declare -- mountpoint="/" declare -- prct="57%" declare -- size="999320" declare -- using="529020" 
{ read -a head;varnames=(${head[@]//[K1% -]});varnames=(${head[@]//[K1% -]});   read ${varnames[@],,} ; } < <(LANG=C df -k /) 
declare -p varnames ${varnames[@],,}  declare -a varnames=([0]="Filesystem" [1]="blocks" [2]="Used" [3]="Available" [4]="Use" [5]="Mounted" [6]="on") declare -- filesystem="/dev/dm-0" declare -- blocks="999320" declare -- used="529020" declare -- available="401488" declare -- use="57%" declare -- mounted="/" declare -- on="" 
{ read foo ; read filesystem dsk[{6,2,9}] prct mountpoint ; } < <(df -k /) declare -p mountpoint dsk declare -- mountpoint="/" declare -a dsk=([2]="529020" [6]="999320" [9]="401488") 
users=() while IFS=: read -u $list user pass uid gid name home bin ;do     ((uid>=500)) &&         printf -v users[uid] "%11d %7d %-20s %s\n" $uid $gid $user $home done {list}</etc/passwd 
echo -n "${users[@]}"        1000    1000 user         /home/user ...       65534   65534 nobody       /nonexistent 
echo ${!users[@]} 1000 ... 65534  echo -n "${users[1000]}"       1000    1000 user       /home/user 
{     read -u $list -a head          # read header in array `head`     varnames=(${head[@]//[K1% -]}) # drop illegal chars for variable names     while read -u $list ${varnames[@],,} ;do         ((pct=available*100/(available+used),pct<10)) &&             printf "WARN: FS: %-20s on %-14s %3d <10 (Total: %11u, Use: %7s)\n" \                 "${filesystem#*/mapper/}" "$mounted" $pct $blocks "$use"      done  } {list}< <(LANG=C df -k) 
while IFS=\; read -u $list -a myvar ;do     echo ${myvar[2]} done {list}<<"eof" foo;bar;baz alice;bob;charlie $cherry;$strawberry;$memberberries eof 
#!/bin/bash  declare free=0 total=0 used=0 mpnt='??'  getDiskStat() {     {         read _         read _ total used free _ mpnt     } < <(         df -k ${1:-/}     ) }  getDiskStat $1 echo "$mpnt: Tot:$total, used: $used, free: $free." 
shell=$(cat /etc/passwd | grep $USER | cut -d : -f 7) echo $shell /bin/bash 
shell=$(grep $USER </etc/passwd | cut -d : -f 7) 
shell=$(sed </etc/passwd "s/^$USER:.*://p;d") echo $shell 
while IFS=: read -a line ; do     [ "$line" = "$USER" ] && shell=${line[6]}   done </etc/passwd echo $shell /bin/bash 
while IFS=: read loginname encpass uid gid fullname home shell;do     [ "$loginname" = "$USER" ] && break   done </etc/passwd echo $shell $loginname ... 
myPi=$(bc -l <<<'4*a(1)' myRay=12 myCirc=$(bc -l <<<" 2 * $myPi * $myRay ") 
myStarted=$(date -d "$(ps ho lstart 1)" +%s) mySessStart=$(date -d "$(ps ho lstart $$)" +%s) 
bc -l <<<$'3*4\n5*6' 12 30  date -f - +%s < <(ps ho lstart 1 $$) 1516030449 1517853288 
coproc bc -l echo 4*3 >&${COPROC[1]} read -u $COPROC answer echo $answer 12  echo >&${COPROC[1]} 'pi=4*a(1)' ray=42.0 printf >&${COPROC[1]} '2*pi*%s\n' $ray read -u $COPROC answer echo $answer 263.89378290154263202896  printf >&${COPROC[1]} 'pi*%s^2\n' $ray read -u $COPROC answer echo $answer 5541.76944093239527260816 
source shell_connector.sh  tty /dev/pts/20  ps --tty pts/20 fw     PID TTY      STAT   TIME COMMAND   29019 pts/20   Ss     0:00 bash   30745 pts/20   R+     0:00  \_ ps --tty pts/20 fw  newConnector /usr/bin/bc "-l" '3*4' 12  ps --tty pts/20 fw     PID TTY      STAT   TIME COMMAND   29019 pts/20   Ss     0:00 bash   30944 pts/20   S      0:00  \_ /usr/bin/bc -l   30952 pts/20   R+     0:00  \_ ps --tty pts/20 fw  declare -p PI bash: declare: PI: not found  myBc '4*a(1)' PI declare -p PI declare -- PI="3.14159265358979323844" 
newConnector /bin/date '-f - +%s' @0 0 myDate '2000-01-01'   946681200 myDate "$(ps ho lstart 1)" boottime myDate now now read utm idl </proc/uptime myBc "$now-$boottime" uptime printf "%s\n" ${utm%%.*} $uptime   42134906   42134906  ps --tty pts/20 fw     PID TTY      STAT   TIME COMMAND   29019 pts/20   Ss     0:00 bash   30944 pts/20   S      0:00  \_ /usr/bin/bc -l   32615 pts/20   S      0:00  \_ /bin/date -f - +%s    3162 pts/20   R+     0:00  \_ ps --tty pts/20 fw 
eval "exec $DATEOUT>&-" eval "exec $DATEIN>&-" ps --tty pts/20 fw     PID TTY      STAT   TIME COMMAND    4936 pts/20   Ss     0:00 bash    5256 pts/20   S      0:00  \_ /usr/bin/bc -l    6358 pts/20   R+     0:00  \_ ps --tty pts/20 fw 
vote vote

65

func (){     ls -l } 
var="ls -l" eval $var 
var=$(ls -l)      OR  var=`ls -l` 
echo "$var" 
echo $var 

Questions similaires