| Linux/UNIX Linuxverfechter finden hier Weggefährten. |
Diskussion: Shell Script Probleme im Forum Linux/UNIX, in der Kategorie Operating Systems; Anzeige Hi, ich habe eine Date, die so aussieht: Code: (-514,519) und ich möchte in getrennte Variablen die -514 und ...
![]() |
| | #1 (permalink) |
| Senior Member Registriert seit: 26.03.06 ![]() Likes: 16 | Shell Script Probleme Anzeige Hi, ich habe eine Date, die so aussieht: Code: (-514,519) Soweit bin ich grad :D Code: position=`cat $CALIBRATE_FILE' positionX=... positionY=... |
| | |
| | #2 (permalink) |
| Moderator ![]() | so in etwa? echo "(-514,519)" | cut -d , -f 1 | sed "s/(//" echo "(-514,519)" | cut -d , -f 2 | sed "s/)//" müsstest du halt noch umbauen damits auf deine bedürfnisse passt. |
| | |
| HaBOT | - Anzeige - |
| |
| | #3 (permalink) |
| Moderator ![]() Registriert seit: 30.06.08 ![]() ![]() ![]() ![]() Likes: 227 | Wenn du bash hast sollte Folgendes gehen Code: position=`cat $CALIBRATE_FILE `
AR=( `echo $position | sed "s/(//" | sed "s/)//" | awk -F, '{print $1 " "$2}'` ) Code: ${AR[0]},
${AR[1]},
usw..
__________________ Wenn ein Gesetz nicht gerecht ist, dann geht die Gerechtigkeit vor dem Gesetz! Habo Blog - http://blog.hackerboard.de/ |
| | |
| | #4 (permalink) |
| Senior Member Themenstarter Registriert seit: 26.03.06 ![]() Likes: 16 | Danke schonmal. Ich habs mit der sed Variante gelöst: Code: #!/bin/sh LAPTOP_MODE_FILE=/etc/tabletmode CALIBRATE_FILE=/sys/devices/platform/hdaps/calibrate POSITION_FILE=/sys/devices/platform/hdaps/position MODE_TABLET=tablet MODE_LAPTOP=laptop xrandr_rotate=normal wacom_rotate=0 echo "Reading calibration file..." content=`cat $CALIBRATE_FILE` calibX=`echo $content | cut -d , -f 1 | sed "s/(//"` calibY=`echo $content | cut -d , -f 1 | sed "s/(//"` echo "Calibration:" $calibX $calibY echo "Starting main loop" while : do sleep 1 laptopMode=`cat $LAPTOP_MODE_FILE` echo $laptopMode if [ $laptopMode == $MODE_LAPTOP ] then xrandr_rotate=normal wacom_rotate=0 else echo "Reading position file..." content=`cat $POSITION_FILE` posX=`echo $content | cut -d , -f 1 | sed "s/(//"` posY=`echo $content | cut -d , -f 2 | sed "s/)//"` echo "Position:" $posX $posY absX=0 absY=0 if [ $posX -le 0 ] then absX=`echo $(( -1*$posX ))` else absX=$posY fi if [ $posY -le 0 ] then absY=`echo $(( -1*$posY ))` else absY=$posY fi echo "Absolute Position" $absX $absY if [ absY > absX ] then if [ $posY > 0 ] then xrandr_rotate=left wacom_rotate=2 else xrandr_rotate=right wacom_rotate=1 fi else if [ $posX < 0 ] then xrandr_rotate=normal wacom_rotate=0 else xrandr_rotate=inverted wacom_rotate=3 fi fi echo "XRAND ROTATE $xrandr_rotate" echo "XSETWACOM ROTATE $wacom_rotate" fi done Code: mathias@apprentice:~/Apps/scripts/rotate$ ./autorotate.sh Reading calibration file... Calibration: 0 0 Starting main loop laptop [: 82: ==: unexpected operator Reading position file... Position: -513 519 Absolute Position 513 519 XRAND ROTATE left XSETWACOM ROTATE 2 Code: if [ $laptopMode == $MODE_LAPTOP ] Code: STR1="test"
STR2="test"
if [ $STR1 == $STR2 ]
then
echo "MATCH"
else
echo "NO MATCH"
fi mfg serow |
| | |
| | #5 (permalink) |
| Senior Member Registriert seit: 10.03.07 ![]() Likes: 19 | Code: if [ $laptopMode == $MODE_LAPTOP ] Code: if [ "$laptopMode" = "$MODE_LAPTOP" ] dann klappts auch mit dem Nachbarn... ähm dem Vergleich. |
| | |
| | #6 (permalink) |
| Senior Member Themenstarter Registriert seit: 26.03.06 ![]() Likes: 16 | Sehr schön, danke. Shell ist etwas seltsam wenn man von Java kommt ... Hier mein fertiges Script. Ihr durft es gerne selbst verwenden, allerdings poste ich es in erster Linie um ein paar Verbesserungsvorschläge zu sammeln. Besonders was die CPU Belastung angeht. Die ist jetzt nicht soo wild, wär aber trotzdem schön, wann man da noch was schrauben könnte. Code: #!/bin/sh LAPTOP_MODE_FILE=/etc/tabletmode CALIBRATE_FILE=/sys/devices/platform/hdaps/calibrate POSITION_FILE=/sys/devices/platform/hdaps/position RESISTANCY=30 MODE_TABLET=tablet MODE_LAPTOP=laptop xrandr_rotate=normal wacom_rotate=0 #echo "Reading calibration file..." content=`cat $CALIBRATE_FILE` calibX=`echo $content | cut -d , -f 1 | sed "s/(//"` calibY=`echo $content | cut -d , -f 2 | sed "s/)//"` #echo "Calibration:" $calibX $calibY #echo "Starting main loop" while : do sleep 1 laptopMode=`cat $LAPTOP_MODE_FILE` #echo $laptopMode if [ "$laptopMode" = "$MODE_LAPTOP" ] then xrandr_rotate=normal wacom_rotate=0 else #echo "Reading position file..." content=`cat $POSITION_FILE` posX=`echo $content | cut -d , -f 1 | sed "s/(//"` posY=`echo $content | cut -d , -f 2 | sed "s/)//"` #echo "Position:" $posX $posY if [ $posX -le 0 ] then absX=`echo $(( -1*$posX ))` else absX=$posY fi if [ $posY -le 0 ] then absY=`echo $(( -1*$posY ))` else absY=$posY fi #echo "Absolute Position" $absX $absY minX=$(($calibX-$RESISTANCY)) minY=$(($calibY-$RESISTANCY)) maxX=$(($calibX+$RESISTANCY)) maxY=$(($calibX+$RESISTANCY)) xOutOfTollerance=0 xOutOfTollerance=1 if [ $posX -lt $minX ] then xrandr_rotate=left wacom_rotate=2 else if [ $posX -gt $maxX ] then xrandr_rotate=right wacom_rotate=1 else if [ $posY -lt $minY ] then xrandr_rotate=normal wacom_rotate=0 else if [ $posY -gt $maxY ] then xrandr_rotate=inverted wacom_rotate=3 fi fi fi fi fi echo `xrandr --output LVDS --rotation $xrandr_rotate` echo `xsetwacom set stylus Rotate $wacom_rotate` done Es liest die HDAPS Sensoren aus und dreht den Bildschirm (xrandr ... --rotate) und die wacom settinsg gleich mit. Es ne schöne Sache besondert für Tablet PCs cu serow |
| | |
| | #8 (permalink) |
| Senior Member Themenstarter Registriert seit: 26.03.06 ![]() Likes: 16 | Danke schön - hab heut Nach noch ein paar verbesserungen vorgenommen. z.B. die Berechnung von minX, maxX, ... aus der Schleife rausgenommen. Jetzt werden die xrandr und xsetwacom Kommandos nur ausgeführt wenns sie nötig sind: Code: #!/bin/sh LAPTOP_MODE_FILE=/etc/tabletmode CALIBRATE_FILE=/sys/devices/platform/hdaps/calibrate POSITION_FILE=/sys/devices/platform/hdaps/position RESISTANCY=30 MODE_TABLET=tablet MODE_LAPTOP=laptop xrandr_status=unknown wacom_status=unknown xrandr_rotate=normal wacom_rotate=0 if [ "$1" = "off" ] then echo `xrandr --output LVDS --rotation normal` echo `xsetwacom set stylus Rotate 0` echo `killall autorotate.sh` exit 0 fi content=`cat $CALIBRATE_FILE` calibX=`echo $content | cut -d , -f 1 | sed "s/(//"` calibY=`echo $content | cut -d , -f 2 | sed "s/)//"` minX=$(($calibX-$RESISTANCY)) minY=$(($calibY-$RESISTANCY)) maxX=$(($calibX+$RESISTANCY)) maxY=$(($calibY+$RESISTANCY)) while : do sleep 1 laptopMode=`cat $LAPTOP_MODE_FILE` if [ "$laptopMode" = "$MODE_LAPTOP" ] then xrandr_rotate=normal wacom_rotate=0 else content=`cat $POSITION_FILE` posX=`echo $content | cut -d , -f 1 | sed "s/(//"` posY=`echo $content | cut -d , -f 2 | sed "s/)//"` if [ $posX -lt $minX ] then xrandr_rotate=left wacom_rotate=2 else if [ $posX -gt $maxX ] then xrandr_rotate=right wacom_rotate=1 else if [ $posY -lt $minY ] then xrandr_rotate=normal wacom_rotate=0 else if [ $posY -gt $maxY ] then xrandr_rotate=inverted wacom_rotate=3 fi fi fi fi fi if [ "$xrandr_status" != "$xrandr_rotate" -o "$wacom_status" != "$wacom_rotate" ] then echo `xrandr --output LVDS --rotation $xrandr_rotate` echo `xsetwacom set stylus Rotate $wacom_rotate` echo "ROTATE: $xrandr_status" xrandr_status=$xrandr_rotate wacom_status=$wacom_rotate fi done |
| | |
| | #9 (permalink) |
| Registriert seit: 22.10.05 ![]() Likes: 3 | Wenn ich mich nicht ganz irre, könntest du Code: content=`cat $POSITION_FILE` posX=`echo $content | cut -d , -f 1 | sed "s/(//"` posY=`echo $content | cut -d , -f 2 | sed "s/)//"` Code: posX=`cut -d , -f 1 < $POSITION_FILE | sed "s/(//"` posY=`cut -d , -f 1 < $POSITION_FILE | sed "s/)//"` Code: content=`cat $CALIBRATE_FILE` calibX=`echo $content | cut -d , -f 1 | sed "s/(//"` calibY=`echo $content | cut -d , -f 2 | sed "s/)//"` Code: calibX=`cut -d , -f 1 < $POSITION_FILE | sed "s/(//"` calibY=`cut -d , -f 2 < $POSITION_FILE | sed "s/)//"` Code: echo `xrandr --output LVDS --rotation normal` echo `xsetwacom set stylus Rotate 0` echo `killall autorotate.sh` Das selbe trifft auch auf Code: echo `xrandr --output LVDS --rotation $xrandr_rotate` echo `xsetwacom set stylus Rotate $wacom_rotate` Von diesen kleinen "Ungeschliffenheiten" finde ich das Script aber besonderes für einen Anfänger sehr gelungen |
| | |
| | #10 (permalink) |
| Senior Member Themenstarter Registriert seit: 26.03.06 ![]() Likes: 16 | Danke schön. Ist ja nicht das erstmal dass ich programmiere aber immerhin das erstmal mit shell ![]() Ich habe zu dem Script nocht was dazugebastelt, und zwar werden jetzt die Pfeiltasten, die sich auf meinem X61 Tablet am Bildschirm befinden, mit dem Bildschirm rotiert. Code: #!/bin/sh LAPTOP_MODE_FILE=/etc/tabletmode CALIBRATE_FILE=/sys/devices/platform/hdaps/calibrate POSITION_FILE=/sys/devices/platform/hdaps/position RESISTANCY=50 MODE_TABLET=tablet MODE_LAPTOP=laptop xrandr_status=normal wacom_status=0 laptop_status=$MODE_LAPTOP xrandr_rotate=normal wacom_rotate=0 if [ "$1" = "off" ] then xrandr --output LVDS --rotation normal xsetwacom set stylus Rotate 0 killall autorotate.sh exit 0 fi calibX=`cut -d , -f 1 < $CALIBRATE_FILE | sed "s/(//"` calibY=`cut -d , -f 2 < $CALIBRATE_FILE | sed "s/)//"` echo "Calibration: $calibX $calibY" minX=$(($calibX-$RESISTANCY)) minY=$(($calibY-$RESISTANCY)) maxX=$(($calibX+$RESISTANCY)) maxY=$(($calibY+$RESISTANCY)) while : do sleep $1 laptopMode=`cat $LAPTOP_MODE_FILE` if [ "$laptopMode" = "$MODE_LAPTOP" ] then echo "Computer is in Laptop Mode" xrandr_rotate=normal wacom_rotate=0 laptop_status=$MODE_LAPTOP else echo "Computer is in Tablet Mode" if [ "$laptop_status" = "$MODE_LAPTOP" ] then xrandr_rotate="inverted" wacom_rotate=3 laptop_status=$MODE_TABLET fi posX=`cut -d , -f 1 < $POSITION_FILE | sed "s/(//"` posY=`cut -d , -f 2 < $POSITION_FILE | sed "s/)//"` echo "$posX -> [ $minX : $maxX ]" echo "$posY -> [ $minY : $maxY ]" if [ $posX -lt $minX ] then xrandr_rotate=left wacom_rotate=2 else if [ $posX -gt $maxX ] then xrandr_rotate=right wacom_rotate=1 else if [ $posY -lt $minY ] then xrandr_rotate=normal wacom_rotate=0 else if [ $posY -gt $maxY ] then xrandr_rotate=inverted wacom_rotate=3 fi fi fi fi fi echo "STATUS: $xrandr_status $wacom_status" echo "TARGET: $xrandr_rotate $wacom_rotate" if [ "$xrandr_status" != "$xrandr_rotate" -o "$wacom_status" != "$wacom_rotate" ] then xrandr_status=$xrandr_rotate wacom_status=$wacom_rotate xrandr --output LVDS --rotation $xrandr_status xsetwacom set stylus Rotate $wacom_status echo "ROTATE: $xrandr_status" if [ "$xrandr_rotate" = "right" ] then sudo setkeycodes 71 105 sudo setkeycodes 6d 103 sudo setkeycodes 6f 106 sudo setkeycodes 6e 108 fi if [ "$xrandr_rotate" = "left" ] then sudo setkeycodes 71 106 sudo setkeycodes 6d 108 sudo setkeycodes 6f 105 sudo setkeycodes 6e 103 fi if [ "$xrandr_rotate" = "normal" ] then sudo setkeycodes 71 103 sudo setkeycodes 6d 106 sudo setkeycodes 6f 108 sudo setkeycodes 6e 105 fi if [ "$xrandr_rotate" = "inverted" ] then sudo setkeycodes 71 108 sudo setkeycodes 6d 105 sudo setkeycodes 6f 103 sudo setkeycodes 6e 106 fi fi done EDIT: Achja, meine Abschalt-Methode gefällt mir auch nicht sonderlich. Geht das nicht schöner? mfg serow |
| | |
| | #11 (permalink) | |
| Registriert seit: 22.10.05 ![]() Likes: 3 | Auszug aus man bash, Kapitel Arrays: Zitat:
Code: export IFS="
"
buttons=("105 103 106 108" "106 108 105 103" "103 106 108 105" "108 105 103 106" "71 6d 6f 6e") Code: export IFS=" "
[ "$xrandr_rotate" = "right" ] && orientation = 0
[ "$xrandr_rotate" = "left" ] && orientation = 1
[ "$xrandr_rotate" = "normal" ] && orientation = 2
[ "$xrandr_rotate" = "inverted" ] && orientation = 3
i=0
while [ i -lt 4]; do
sudo setkeycodes ${buttons[4]} `echo ${buttons[$orientation] | cut -f $i`
i=$(($i + 1))
done | |
| | |
| | #12 (permalink) |
| Senior Member Themenstarter Registriert seit: 26.03.06 ![]() Likes: 16 | Ahja danke schön, das klappt zwar so noch nicht ganz aber wenn man dem cut per -d " " den delimiter mitgibts gehts: Code: #!/bin/bash
export IFS=" "
############################################################
## CONSTANTS ###############################################
############################################################
SLEEP_TIME=1
LAPTOP_MODE_FILE=/etc/tabletmode
MODE_TABLET=tablet
MODE_LAPTOP=laptop
CALIBRATE_FILE=/sys/devices/platform/hdaps/calibrate
RESISTANCY=50
POSITION_FILE=/sys/devices/platform/hdaps/position
XRANDR=("normal" "right" "inverted" "left")
WACOM=("0" "1" "3" "2")
BUTTONS=("103 106 108 105" "105 103 106 108" "108 105 103 106" "106 108 105 103" "71 6d 6f 6e")
############################################################
## VARIABLES ###############################################
############################################################
orientation=-1
next_orientation=-1
calibX=`cut -d , -f 1 < $CALIBRATE_FILE | sed "s/(//"`
calibY=`cut -d , -f 2 < $CALIBRATE_FILE | sed "s/)//"`
minX=$(($calibX-$RESISTANCY))
minY=$(($calibY-$RESISTANCY))
maxX=$(($calibX+$RESISTANCY))
maxY=$(($calibY+$RESISTANCY))
############################################################
## MAIN LOOP STARTS HERE ###################################
############################################################
while :
do
sleep $SLEEP_TIME
laptopMode=`cat $LAPTOP_MODE_FILE`
if [ "$laptopMode" = "$MODE_LAPTOP" ]
then
#echo "Computer is in Laptop Mode"
laptop_status=$MODE_LAPTOP
next_orientation=0
else
#echo "Computer is in Tablet Mode"
if [ "$laptop_status" = "$MODE_LAPTOP" ]
then
laptop_status=$MODE_TABLET
fi
posX=`cut -d , -f 1 < $POSITION_FILE | sed "s/(//"`
posY=`cut -d , -f 2 < $POSITION_FILE | sed "s/)//"`
if [ $minX -lt $posX -a $posX -lt $maxX -a $minY -lt $posY -a $posY -lt $maxY ]
then
next_orientation=2 #inverted
else
if [ $posY -gt $maxY -a $minX -lt $posX -a $posX -lt $maxX ]
then
next_orientation=2 #inverted
else
if [ $posY -lt $minY -a $minX -lt $posX -a $posX -lt $maxX ]
then
next_orientation=0 #normal
else
if [ $posX -lt $minX -a $minY -lt $posY -a $posY -lt $maxY ]
then
next_orientation=3 #left
else
if [ $posX -gt $maxX -a $minY -lt $posY -a $posY -lt $maxY ]
then
next_orientation=1 #right
else
next_orientation=$orientation
fi
fi
fi
fi
fi
fi
if [ "$orientation" != "$next_orientation" ]
then
orientation=$next_orientation
echo "Orientation: ${XRANDR[$orientation]}"
xrandr --output LVDS --rotation ${XRANDR[$orientation]}
xsetwacom set stylus Rotate ${WACOM[$orientation]}
i=0
while [ $i -lt 4 ]
do
sudo setkeycodes `echo ${BUTTONS[4]} | cut -d " " -f $(($i + 1))` `echo ${BUTTONS[$orientation]} | cut -d " " -f $(($i + 1))`
i=$(($i + 1))
done
fi
done Die manpage zu bash ist ja krass. Endlos lang und garnicht leicht zu verstehn |
| | |
| | #13 (permalink) |
| Registriert seit: 22.10.05 ![]() Likes: 3 | So wie ich das sehe, kann man diesen If-Sumpf nicht wirklich verkleinern. Was evtl. lesbarer wäre, wäre anstatt Code: if foobar then snafu fi Code: if foobar; then snafu fi |
| | |
![]() |
| - Anzeige - | |
| |
| Themen-Optionen | |
| Ansicht | |
| |
Ähnliche Themen | ||||
| Thema | Autor | Forum | Antworten | Letzter Beitrag |
| Werte von Shell script in aufrufendes Programm zurückgeben | blueflash | Linux/UNIX | 5 | 13.08.08 23:27 |
| Linux shell script als Win Anwendung | schmidtl_dd | Code Kitchen | 1 | 11.02.06 20:17 |
| Shell | Iker C. | Code Kitchen | 1 | 14.02.04 18:06 |
| Shell in VB | TheEvilOne | Code Kitchen | 11 | 09.12.02 09:57 |
| wie erstelle ich eine perl-shell in einem cgi-script | honkman | (Web-) Design und webbasierte Sprachen | 3 | 15.10.02 11:49 |