Re: aus Skript heraus Xterm öffnen und darin die Fortsetzung des Skripts ausführen
- From: Lukas Gantert <admin@xxxxxxxxxx>
- Date: Fri, 17 Jun 2011 23:35:55 +0000 (UTC)
mhenn :
Hallo Lukas,
was du suchst steht (wie beinahe immer :)) in den manpages (man xterm):
-ls This option indicates that the shell
that is started in the xterm window will be a login
shell (i.e., the first character of argv[0] will be a
dash, indicating to the shell that it should read the
user's .login or .profile).
The -ls flag and the loginShell resource are
ignored if -e is also given, because xterm does not
know how to make the shell start the given com‐ mand
after whatever it does when it is a login shell - the
user's shell of choice need not be a Bourne shell after
all. Also, xterm -e is supposed to provide a
consistent functionality for other applications that
need to start text-mode programs in a window, and if
loginShell were not ignored, the result of ~/.profile
might interfere with that.
If you do want the effect of -ls and -e simultaneously,
you may get away with something like
xterm -e /bin/bash -l -c "my
command here"
Das habe ich überlesen...
Finally, -ls is not completely ignored, because xterm
-ls -e does write a /var/log/wtmp entry (if
configured to do so), whereas xterm -e does not.
Demnach koennte das z.B. so aussehen:
xterm -e '/bin/bash -l -c "echo \"There you are!\"; sleep 3"'
ok.
bzw. Wenn dein Skript "foo" heisst, kannst du dir folgenden Starter
dafuer schreiben:
xterm -bg skyblue2 -fg white -fn -*-*-*-r-*-*-24-*-*-*-m-*-iso8859-*\
-sl 1000 -e '/usr/bin/env bash -i -c "./foo -a -12 15"'
ok.
Oder wenn du die Parameter dynamisch uebergeben willst: xterm -bg
skyblue2 -fg white -fn -*-*-*-r-*-*-24-*-*-*-m-*-iso8859-*\
-sl 1000 -e "/usr/bin/evn bash -i -c './foo $*'"
ok.
Oder, eingebaut in dein komplettes Skript:
#!/usr/bin/env bash
# learning tool, for children: to caculate # french version # 20070216
by Lukas Gantert, GPL
# 20080522 division added
# 20081107 cleaned
# 20081108 - division by zero is now forbidden # - new limit
definitions for rand-numbers (min, max) # 20081127 count number of
questions, points and errors added # bugs: displays questions, points,
errors in plural also when only one count # todo: - automatic
changing of learning levels, ...
Help () {
clear
echo -e " \033[1mNOM\033[0m
`basename $0` :
Apprendre à calculer. Un jeu interactif.
\033[1mUTILISATION\033[0m
`basename $0` avec une seule option pour le type d'opération et deux
arguments en chiffre réel pour les valeurs minimales et maximals des
chiffres à calculer (default: -10 et 10). Une fois dans le jeu, on
peut afficher le resultat en appuyant la touche \"m\" (montrer). Pour
quitter, appuyer \"q\" ou \"Ctrl-C\".
\033[1mOPTIONS\033[0m
-a addition
-s substration
-m multiplication
-d division
-h aide
\033[1mEXEMPLE\033[0m
\"`basename $0` -a -12 15\" peut donner des calculations entre
minimal -12 + -12 et maximal 15 + 15 et tous qui est entre les deux."
exit
}
export Option=$1
export Min=${2:--10}
export Max=${3:-10}
[[ Min -gt Max ]] && x=$Min Min=$Max Max=$x export x
# take options
case $Option in
-a ) Operator=+ ;; # for additions
-s ) Operator=- ;; # for substractions -m ) Operator=* ;; # for
multiplications -d ) Operator=/ ;; # for divisions -h ) Help ;; # for
help
* ) Help ;; # for help
esac
export Operator
Calc () {
# set two random-numbers:
Number_1=$(( RANDOM % ( Max - Min + 1 ) + Min )) Number_2=$(( RANDOM
% ( Max - Min + 1 ) + Min ))
# special cases - division:
if [ "$Operator" = "/" ]; then
# don't accept zero as divisor ($Number_2): [ $Number_2 -eq 0 ]
&& Calc
# only outputs integers as results:
Number_1=$(( $Number_1 * $Number_2 ))
fi
# calculate
Result=$(( $Number_1 $Operator $Number_2 )) 2>/dev/null
}
export questions=1
export points=0
export errors=0
Check () {
printf "%$((5-${#Number_1}))s %s %+4s %$((5-${#Number_2}))s %s = "\
"" "$Number_1" "$Operator" "" "$Number_2" read Answer # quit:
case ${Answer:-e} in
q ) echo -e "\033[0m" && exit
;;
# show result:
m ) printf "%$((5-${#Number_1}))s %s %+4s %$((5-${#Number_2}))s %s
= %s\n %s %s %s\n\n"\
"" "$Number_1" "$Operator" "" "$Number_2" "$Result"\ "$questions
questions," "$points points," "$errors fautes" questions=$((
questions + 1 ))
Calc # new
Check
;;
# answer correct:
$Result ) points=$(( points + 1 ))
printf "\033[32mbravo !\033[0m %s %s %s\n\n"\ "$questions
questions," "$points points," "$errors fautes" questions=$((
questions + 1 ))
Calc # new
Check
;;
# answer not correct:
* ) errors=$(( errors + 1 ))
printf "\033[31maiioh !\033[0m %s %s %s\n\n"\ "$questions
questions," "$points points," "$errors fautes" # again: Check
;;
esac
}
declare -fx Calc
declare -fx Check
xterm -bg skyblue2 -fg white -fn '-*-*-*-r-*-*-24-*-*-*-m-*-iso8859-*'
-e '/usr/bin/env bash -i -c "Calc; Check"'
Funktioniert auf Anhieb! Wow!
Geaendert wurde:
Einige exports, damit das xterm auch die vorher gesetzten Variablen
Operator usw. und die Funktionen auch kennt (ja, es ist eklig).
Frage nebenbei: Was ist der Unterschied von export -f und declare -fx ?
In der Lösung von PointedEars müssen nur die Parameter des Skriptaufrufes
exportiert werden.
Shebang in /usr/bin/env (portabler)
ok.
Der bash-Aufruf mittels -i statt -l (hat beim Testen eher das
Gewuenschte geliefert), wobei sich das ja laut Manpage aufheben muesste.
Vielleicht hab ich da selbst nicht genau gelesen :) ... es funktioniert
jedenfalls so.
Ich lese jetzt aber nicht die ganze Manpage von bash ;)
Wie schon der Vorposter gesagt hat: Zwei verschiedene Dateien (ein
Starter, eine Datei, die das Skript selbst enthaelt) waeren sauberer.
Das sehe ich auch so.
Denkbar waere auch mittels here-document (cat > /tmp/calc_for_children$$
<< word_that_never_occurs_in_text
<HIER DAS SKRIPT>
word_that_never_occurs_in_text
xterm [...] bash -i -c /tmp/calc_for_children$$ [...])
Aber das geht jetzt vermutlich zu weit ... ist auch nicht sinnvoll. Mach
am besten zwei Dateien.
Ich liebe here-documents. Mein momentaner Knüller ist die in ein Shell-
Skript eingebettete Manpage (hier ein gekürztes, aber immer noch zu langes
Beispiel ohne Bezug zu meiner ursprünglichen Frage):
#########################################
function Help {
Scriptname="Scriptname.sh" Version=1.2.3.4.5 Main_Dir="$HOME"
man -l -- - << EOF
..TH $Scriptname 1 $Version
..UC 4
..SH NAME
$Scriptname \- an interactive health-data-logger.
..SH SYNOPSIS
..B $Scriptname
[options] [directory]
..SH DESCRIPTION
..I $Scriptname
is a interactive n interactive health-data-logger. Etc.
..SH ACTIONS
Press the appropriate key to perform an action.
..SS weight
To append the current wight of the selected member together with a
date-time-stamp to the members wight-file.
..SS size
To append the current size of the selected membere together with a
date-time-stamp to the members size-file.
..SS comment
To note some comments (diseases, vaccinations, ... ). For now, its only
the archaic "cat" editor. He allows multiple lines but not a navigation in
the text. Use Ctrl-d twice to quit. The comment will be appended to the
comment-file together with a date-time-stamp.
..SS report
This action creates automatically a html-report for the selected member.
..SH FILES
If
..I $Scriptname
is called without any directory as argument, the default is $Main_Dir.
..SH AUTHOR
Lukas Gantert
http://nofalab.ch/shellscripts.html
..SH BUGS
a lot of
..SH SEE ALSO
everywhere
EOF
}
Help
#############################################
Mal schauen.
Uebringes:
es fuxt mich vs. es fuchst mich
childrens vs. children
:) - (no offense!)
Merci ! Meine sprachlichen Fähigkeiten sind begrenzt, nicht nur beim
programmieren.
Beste Gruesse,
Michael
Vielen Dank nochmals für die ausführlichen und erhellenden Erklärungen.
Auch beste Gruesse,
Lukas
.
- References:
- Prev by Date: Re: aus Skript heraus Xterm öffnen und darin die Fortsetzung des Skripts ausführen
- Next by Date: Re: aus Skript heraus Xterm öffnen und darin die Fortsetzung des Skripts ausführen
- Previous by thread: Re: aus Skript heraus Xterm öffnen und darin die Fortsetzung des Skripts ausführen
- Next by thread: Re: aus Skript heraus Xterm öffnen und darin die Fortsetzung des Skripts ausführen
- Index(es):
Relevant Pages
|