Bash Scripts: Unterschied zwischen den Versionen
aus Metalab Wiki, dem offenen Zentrum für meta-disziplinäre Magier und technisch-kreative Enthusiasten.
Zur Navigation springenZur Suche springen
Amir (Diskussion | Beiträge) |
Keine Bearbeitungszusammenfassung |
||
(6 dazwischenliegende Versionen von einem anderen Benutzer werden nicht angezeigt) | |||
Zeile 30: | Zeile 30: | ||
<pre> | <pre> | ||
#!/bin/bash | #!/bin/bash | ||
PREPARE=; ENTER=; CLEAN=; VERBOSE=; | PREPARE=; ENTER=; CLEAN=; VERBOSE=; X=; Y=; | ||
RUN=/bin/bash | RUN="/bin/bash" | ||
CONFIG= | CONFIG= | ||
DISPLAY= | |||
function verbose { [ $VERBOSE ] && echo $@; $@ 1> /dev/null; } | function verbose { [ $VERBOSE ] && echo $@; $@ 1> /dev/null; } | ||
Zeile 38: | Zeile 39: | ||
function error { echo "error: $1" >&2; exit 1; } | function error { echo "error: $1" >&2; exit 1; } | ||
function usage { | function usage { | ||
[ "$1" ] && error "$1" | |||
cat <<EOF | cat <<EOF | ||
Usage: chrtsetup [ | Usage: chrtsetup [OPTIONS] [CHRTCONF] | ||
-v, --verbose print actions | -v, --verbose print actions | ||
-p, --prepare perform mount sequence only | -p, --prepare perform mount sequence only | ||
-c, --clean perform umount sequence only | -c, --clean perform umount sequence only | ||
-e, --enter enter chroot only | -e, --enter enter chroot only | ||
-x DISPLAY configures the xsession defined by DISPLAY | |||
to allow all connections from localhost | |||
(xhost +localhost) and sets the DISPLAY | |||
variable inside the chroot accordingly to | |||
support connecting X11 clients from inside the | |||
chroot to your X11 server. | |||
-y DISPLAY does the same like -x but additionally sets the | |||
window manager name of the xsession to a | |||
non-reparenting window manager (wmname LG3D) to | |||
avoid problems with some jdk versions running | |||
with awesome wm. (shouldn't interfere with | |||
conventional use-cases :) | |||
-r, --run=COMMAND run specified COMMAND after entering the chroot | -r, --run=COMMAND run specified COMMAND after entering the chroot | ||
CHRTCONF is the name of the chroot configuration stored in /etc/chrtsetup/. | CHRTCONF is the name of the chroot configuration stored in /etc/chrtsetup/. | ||
If neither -p, -c nor -e is set | If neither -p, -c nor -e is set -pce is assumed. | ||
Examples: | Examples: | ||
chrtsetup ubuntu | chrtsetup ubuntu # executed setup defined in /etc/chrtsetup/ubuntu, enter chroot and clean up after | ||
chrtsetup -c ubuntu | chrtsetup -c ubuntu # undo mounts performed to setup the chroot | ||
chrtsetup -pe ubuntu | chrtsetup -pe ubuntu # mount, enter but do not clean up after | ||
chrtsetup -x localhost:0.0 --run="xterm" ubuntu # configure the xsession and start xterm inside the chroot | |||
EOF | EOF | ||
Zeile 94: | Zeile 109: | ||
done | done | ||
} | } | ||
function setupX { | |||
RUN="DISPLAY=$1 $RUN" | |||
export DISPLAY=$1 | |||
xhost +localhost | |||
[ $Y ] && wmname LG3D | |||
} | |||
### main | |||
[ $# -eq 0 ] && usage | [ $# -eq 0 ] && usage | ||
eval set -- "`getopt -o vpecr --long verbose,prepare,enter,clean,run -n 'chrtsetup' -- \"$@\"`" | eval set -- "`getopt -o vpecr:x:y: --long verbose,prepare,enter,clean,run: -n 'chrtsetup' -- \"$@\"`" | ||
while true ; do | while true ; do | ||
case "$1" in | case "$1" in | ||
Zeile 105: | Zeile 129: | ||
-c|--clean) CLEAN=0 ; shift ;; | -c|--clean) CLEAN=0 ; shift ;; | ||
-r|--run) RUN="$2" ; shift 2;; | -r|--run) RUN="$2" ; shift 2;; | ||
-x|--x) X=0; DISPLAY="$2" ; shift 2;; | |||
-y|--y) Y=0; DISPLAY="$2" ; shift 2;; | |||
--) shift ; break ;; | --) shift ; break ;; | ||
esac | esac | ||
Zeile 110: | Zeile 136: | ||
CONFIG=$1 | CONFIG=$1 | ||
[ -z $CONFIG ] && usage | [ -z $CONFIG ] && usage "CONFIG is mandatory" | ||
[ ! "$CLEAN" -a ! "$PREPARE" -a ! "$ENTER" ] && CLEAN=set PREPARE=set ENTER=set | [ ! "$CLEAN" -a ! "$PREPARE" -a ! "$ENTER" ] && CLEAN=set PREPARE=set ENTER=set | ||
source "/etc/chrtsetup/$CONFIG" | source "/etc/chrtsetup/$CONFIG" | ||
[ "$X" -o "$Y" ] && setupX $DISPLAY | |||
[ $PREPARE ] && prepare | [ $PREPARE ] && prepare | ||
[ $ENTER ] && chroot "$CHROOT" "$RUN" | [ $ENTER ] && chroot "$CHROOT" bash -c "$RUN" | ||
[ $CLEAN ] && clean | [ $CLEAN ] && clean | ||
</pre> | </pre> | ||
== gstrtpcast == | == gstrtpcast == | ||
Grab audio from a pulse monitor src and stream it via rtp. though pulseaudio is capable of streaming sound via rtp it is not able to compress the stream. additionally its command line tools are crap and its not nearly as flexible as gstreamer. | Grab audio from a pulse monitor src and stream it via rtp. though pulseaudio is capable of streaming sound via rtp it is not able to compress the stream. additionally its command line tools are crap and its not nearly as flexible as gstreamer. | ||
You need to load the module for the alsa dummy device (snd_dummy) before running the script. | |||
<pre> | <pre> | ||
Zeile 152: | Zeile 177: | ||
esac | esac | ||
done | done | ||
CLIENTS="$1" | |||
[ -n $DEVICE ] && DEVICE=`pacmd list-sources | fgrep 'alsa_output.platform-snd_dummy' | awk -F '[<>]' '{ print $2 }'` | [ -n $DEVICE ] && DEVICE=`pacmd list-sources | fgrep 'alsa_output.platform-snd_dummy' | awk -F '[<>]' '{ print $2 }'` | ||
Zeile 170: | Zeile 196: | ||
[[Kategorie:CLI]] | [[Kategorie:CLI]] | ||
[[Kategorie:Howto]] |
Aktuelle Version vom 23. Januar 2013, 20:46 Uhr
chrtsetup
set up mounts and enter a chroot
chrtsetup configuration file to set up a unionfs of a disk image and a filesystem tree:
#!bin/bash # only $CHROOT and $MOUNTS are relevant to chrtsetup ROOT=/home/user/ubuntu_unionfs IMG=$ROOT/ext3_1GB.dd IMG_MNT=$ROOT/diskimage SHARED=$ROOT/shared FS=$ROOT/chroot CHROOT=$ROOT/chroot #from to fstype mount-options do-chroot MOUNTS=`echo -e "\ /dev $CHROOT/dev none bind false \n\ $ROOT/shared $CHROOT/root/shared none bind false \n\ $IMG $IMG_MNT ext3 loop false \n\ unionfs $CHROOT unionfs dirs=$IMAGE_MNT=rw:$FS=ro false \n\ none /proc proc defaults true \n\ none /sys sysfs defaults true \n\ none /dev/pts devpts defaults true \n\
the script:
#!/bin/bash PREPARE=; ENTER=; CLEAN=; VERBOSE=; X=; Y=; RUN="/bin/bash" CONFIG= DISPLAY= function verbose { [ $VERBOSE ] && echo $@; $@ 1> /dev/null; } function warn { [ $VERBOSE ] && echo "warn: $1" >&2; } function error { echo "error: $1" >&2; exit 1; } function usage { [ "$1" ] && error "$1" cat <<EOF Usage: chrtsetup [OPTIONS] [CHRTCONF] -v, --verbose print actions -p, --prepare perform mount sequence only -c, --clean perform umount sequence only -e, --enter enter chroot only -x DISPLAY configures the xsession defined by DISPLAY to allow all connections from localhost (xhost +localhost) and sets the DISPLAY variable inside the chroot accordingly to support connecting X11 clients from inside the chroot to your X11 server. -y DISPLAY does the same like -x but additionally sets the window manager name of the xsession to a non-reparenting window manager (wmname LG3D) to avoid problems with some jdk versions running with awesome wm. (shouldn't interfere with conventional use-cases :) -r, --run=COMMAND run specified COMMAND after entering the chroot CHRTCONF is the name of the chroot configuration stored in /etc/chrtsetup/. If neither -p, -c nor -e is set -pce is assumed. Examples: chrtsetup ubuntu # executed setup defined in /etc/chrtsetup/ubuntu, enter chroot and clean up after chrtsetup -c ubuntu # undo mounts performed to setup the chroot chrtsetup -pe ubuntu # mount, enter but do not clean up after chrtsetup -x localhost:0.0 --run="xterm" ubuntu # configure the xsession and start xterm inside the chroot EOF exit 2 } function chrtify { cmd=$1 doChroot=$2 if [ "$doChroot" == "true" ]; then echo "chroot -- $CHROOT $cmd" else echo $cmd fi } function doUmount { from=$1 to=$2 fstype=$3 opts=$4 doChroot=$5 umount=`chrtify 'umount' $doChroot` verbose $umount -t $fstype $to || error "umount failed: $@" } function doMount { from=$1 to=$2 fstype=$3 opts=$4 doChroot=$5 mount=`chrtify 'mount' $doChroot` verbose $mount -t $fstype -o $opts $from $to || error "mount failed: $@" } function isMounted { from=$1 to=$2 fstype=$3 opts=$4 doChroot=$5 grepMounts=`chrtify "grep -qF /etc/mtab -e" $doChroot` $grepMounts "$from $to $fstype" } function clean { echo "$MOUNTS" | tac | while read mnt; do isMounted $mnt && doUmount $mnt \ || warn "$mnt not mounted" >&2 done } function prepare { echo "$MOUNTS" | while read mnt; do isMounted $mnt || doMount $mnt \ && warn "$mnt already mounted" >&2 done } function setupX { RUN="DISPLAY=$1 $RUN" export DISPLAY=$1 xhost +localhost [ $Y ] && wmname LG3D } ### main [ $# -eq 0 ] && usage eval set -- "`getopt -o vpecr:x:y: --long verbose,prepare,enter,clean,run: -n 'chrtsetup' -- \"$@\"`" while true ; do case "$1" in -v|--verbose) VERBOSE=0 ; shift ;; -p|--prepare) PREPARE=0 ; shift ;; -e|--enter) ENTER=0 ; shift ;; -c|--clean) CLEAN=0 ; shift ;; -r|--run) RUN="$2" ; shift 2;; -x|--x) X=0; DISPLAY="$2" ; shift 2;; -y|--y) Y=0; DISPLAY="$2" ; shift 2;; --) shift ; break ;; esac done CONFIG=$1 [ -z $CONFIG ] && usage "CONFIG is mandatory" [ ! "$CLEAN" -a ! "$PREPARE" -a ! "$ENTER" ] && CLEAN=set PREPARE=set ENTER=set source "/etc/chrtsetup/$CONFIG" [ "$X" -o "$Y" ] && setupX $DISPLAY [ $PREPARE ] && prepare [ $ENTER ] && chroot "$CHROOT" bash -c "$RUN" [ $CLEAN ] && clean
gstrtpcast
Grab audio from a pulse monitor src and stream it via rtp. though pulseaudio is capable of streaming sound via rtp it is not able to compress the stream. additionally its command line tools are crap and its not nearly as flexible as gstreamer. You need to load the module for the alsa dummy device (snd_dummy) before running the script.
#!/bin/bash ENCODER=" vorbisenc ! rtpvorbispay "; DEVICE="/dev/video0" GST_OPTS= SET_SINK= CLIENTS= VERBOSE= TEMP=`getopt -o e:d:g:vs --long encoder:,device:,gst-opts:,verbose,set-default-sink \ -n 'gstrtpcast' -- "$@"` [ $? != 0 ] && exit 1 eval set -- "$TEMP" while true ; do case "$1" in -e|--encoder) ENCODER="$2" ; shift 2 ;; -d|--device) SRC_NAME="$2" ; shift 2 ;; -g|--gst-opts) GST_OPTS="$GST_OPTS $2" ; shift 2 ;; -v|--verbose) VERBOSE=0 ; shift ;; -s|--set-default-sink) SET_SINK=0; shift ;; --) shift ; break ;; *) echo "Internal error!" ; exit 1 ;; esac done CLIENTS="$1" [ -n $DEVICE ] && DEVICE=`pacmd list-sources | fgrep 'alsa_output.platform-snd_dummy' | awk -F '[<>]' '{ print $2 }'` [ $SET_SINK ] && pacmd "set-default-sink $[ `echo $SRC_NAME | awk -F '.' '{ print $3 }'` + 1 ]" 1> /dev/null; echo -e "---\n" echo "cast: $DEVICE"; echo "to: $CLIENTS"; echo "encoder: $ENCODER" echo -e "\n---\n" [ $VERBOSE ] && GST_OPTS="$GST_OPTS -v" echo "gst-launch $GST_OPT pulsesrc device=\"$DEVICE\" ! $ENCODER ! multiudpsink clients=\"$CLIENTS\"" gst-launch $GST_OPTS pulsesrc device="$DEVICE" ! $ENCODER ! multiudpsink clients="$CLIENTS";