{{tag>Bash Script}}
= Bash Read lire affecter plusieurs variables en une seul fois
Voir aussi :
* Heredoc
-r : Prevents backslash escapes from being interprete
-t : Timeout
-n : Max char
-s : Secret / Silent
-p : Prompt
-a : Array
NOTE : Dans les scripts bash il est recommandé d'utiliser ''read -r''
Exemple :
read a b c d <<< $(psql -U osiris -d osiris4 -At -c 'select urlbl,urlarbl,urlimport,urlarimport,urlventes,urlarventes,urlmessages,max(dateactivation),login,password,loginmlp,motdepassemlp from credentials pc join diffuseur pd using(groupeid) group by urlbl,urlarbl,urlimport,urlarimport,urlventes,urlarventes,urlmessages,login,password,loginmlp,motdepassemlp limit 1' | sed -e 's/|/ /g')
Possible aussi d'utiliser mkfifo et changer [[https://fr.wikipedia.org/wiki/Internal_Field_Separator|IFS]]
Autres liens :
* [[http://stackoverflow.com/questions/2746553/bash-script-read-values-from-stdin-pipe]]
* [[http://unix.stackexchange.com/questions/33858/bash-read-command-and-stdin-redirection]]
* [[http://www.linuxtopia.org/online_books/advanced_bash_scripting_guide/x13082.html]]
* [[http://superuser.com/questions/276531/clear-stdin-before-reading]]
Changement de la variable IFS
old_IFS="$IFS"
export IFS=","
Exemple 1
unset a b c
read a b c <<< "AA BB CC"
echo -e "a: $a\nb: $b\nc: $c"
Exemple 2
unset a b c
IFS=',' read a b c <<< "AA,BB,CC"
echo -e "a: $a\nb: $b\nc: $c"
Exemple 3
unset a b c
mkfifo /tmp/fifo
echo "AA,BB,CC" > /tmp/fifo &
IFS=',' read a b c < /tmp/fifo
echo -e "a: $a\nb: $b\nc: $c"
rm /tmp/fifo
Autre - exemple while read
while IFS=$' \t\n' read ID NAME NOT_USED
do
echo -e "\t $ID $NAME"
done < <(openstack image list -f value)
== Autres exemples
Trier des fichiers par ordre alphabétique à partir d'un champ à droite
#! /bin/bash
set -e
set -o nounset
dir="$1"
decoupe_col()
{
local line="$*"
IFS='_' read a b c <<< "${line/.tif/}"
echo "${c}_${b}_${a}"
}
decoupe_line()
{
while read line
do
decoupe_col "$line"
done < <(find "$dir" -maxdepth 1 -type f -name "*.tif")
}
trie()
{
decoupe_line |sort
}
trie |awk -F'_' '{print $3"_"$2"_"$1".tif"}'
=== Mot de passe
read -s PASSWORD
echo $PASSWORD
=== Read et find
source : https://www.shellcheck.net/wiki/SC2044
while IFS= read -r -d '' file
do
(( count++ ))
echo "Playing file no. $count"
play "$file"
done < <(find mydir -mtime -7 -name '*.mp3' -print0)
echo "Played $count files"