2010-10-03 1 views
1

À mon école, je veux automatiser le processus consistant à dire aux élèves d'aller en classe et de jouer une chanson cinq minutes avant le début des cours à la fin du repas.Port Bash to Batch

J'ai écrit un script Bash qui va jouer l'annonce, une chanson aléatoire. Après cinq minutes, le script va éteindre la musique et tuer VLC. J'ai besoin que ce script tourne sous Windows car c'est le système d'exploitation utilisé pour les annonces. Il n'a pas besoin d'être Batch, mais je dois pouvoir l'exécuter sur Windows.

Voici le script que j'ai écrit:

#!/bin/bash 
#Copyright (c) 2010, Jason Cook 
#All rights reserved. 

#Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 

#Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
#Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 
#Neither the name of the Jason Cook nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 

#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 

#Set Arguments 
song=$((RANDOM % 21)) 
volume_fade_counter=0 

echo "Released under The BSD License" 
echo 

echo "11:55" 
echo "Playing \"Get to Class\"" 
echo "Playing song number $song" 
vlc /home/jason/Documents/Announcements-Lunch/get-to-class.mp3 /home/jason/Documents/Announcements-Lunch/$song.mp3 & 

echo "Waiting for five minutes" 
sleep 360 

echo "Fading audio" 
while [ $volume_fade_counter -le 100 ] ; do 
aumix -v -1 
volume_fade_counter=$(($volume_fade_counter + 1)) 
done 

echo "Killing VLC" 
pkill vlc 
exit 
+0

Quels sont les problèmes spécifiques que vous avez avec le portage? Avez-vous besoin de conseils sur les outils à utiliser? Il est peu probable que les utilisateurs de StackOverflow portent simplement votre script, mais nous vous aidons avec des questions et des problèmes spécifiques. – DarkDust

+0

Les problèmes que je suis ne sachant pas le premier penser à Batch. Si quelqu'un m'a dit comment faire disparaître le son et attendre que VLC commence cinq minutes avant de passer à l'étape suivante. Je n'ai pas posté cette attente que les gens à porto cela pour moi (mais ce serait incroyable), j'ai posté cela pour de l'aide sur Batch. – Jason

+0

Vous pouvez sauvegarder '$!' Après avoir lancé vlc en arrière-plan pour être sûr de tuer le bon processus. (Voir "Paramètres spéciaux" dans la page de manuel de bash.) –

Répondre

5

Jetez un oeil à Cygwin, une couche UNIX libre au-dessus de Windows avec un vaste ensemble de paquets disponibles. Il inclut bash pour exécuter des scripts bash directement.

1

est ici un vbscript (non testé, juste traduit directement)

Set WshShell = CreateObject("WScript.Shell") 
Randomize 
min=0 
max=32767 
song=CInt((max-min+1)*Rnd+min) 
volume_fade_counter=0 
WScript.Echo "Released under The BSD License" 
WScript.Echo "11:55" 
WScript.Echo "Playing ""Get to Class""" 
WScript.Echo "Playing song number " & song 
strPath1="C:\home\jason\Documents\Announcements-Lunch\get-to-class.mp3" 
strPath2="C:\home\jason\Documents\Announcements-Lunch\" & song &".mp3" 
WshShell.Run("vlc "&strPath1&" "&strPath2,0) 
WScript.Echo "Waiting for 5 min" 
WScript.Sleep 18000 
WScript.Echo "Fading audio" 
Do While volume_fade_counter <= 100 
    WScript.Run("aumix -v -1",0) 
    volume_fade_counter=volume_fade_counter+1 
Loop 
Set WshShell = Nothing 
WScript.Echo "Killing vlc" 
strComputer = "." 
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
Set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process Where Name = 'vlc.exe'") 
For Each objProcess in colProcessList 
    objProcess.Terminate() 
Next 
utilisation

:

C:\test> cscript //nologo myscript.vbs 
+0

+1 pour coder un bot nommé user131527 qui effectue des traductions automatiques de langue. – Dave

+0

J'espère essayer cela au déjeuner. Je vais vous dire comment/si ça va. – Jason

1

Jetez aussi un coup d'œil à MSYS http://www.mingw.org/wiki/msys

MSYS est une collection d'utilitaires GNU tels comme bash, make, gawk et grep pour permettre la construction d'applications et de programmes qui dépendent des outils UNIX traditionnels. Il est destiné à compléter MinGW et les déficiences de la coquille cmd. Là où je travaille, nous l'utilisons pour exécuter des scripts shell sous Windows.