NetAdminTools.com
 
SignalQ Sites:
NetAdminTools - Coprolite - SpotBridge - NAW
RoboCoop - AreWeDown - SolarPower - SysAdminTools
Xfig - Gold Loaf - GeekPapa - FixGMC - FixRambler
Categories:
GNU/Linux | Homebrew designs | Perl | Ruby | Administration | Backup/Recovery | Bugs/Fixes | Certification | Database | Email | File/Print | Hardware | Information Grab Bag | Interoperability | GNU/Linux ABCs | Monitoring | Name Resolution | Network Services | Networking | Remote Control | Security | Desktop | Web | BSD | Solaris | GIAGD | ERP | REALbasic

Last 30 Days | Last 60 Days | Last 90 Days | All Articles | GNU/Linux Reference OS Build | MCJ How-to | MCJ Presentation Config | Keywords | RSS



Categories:
·GNU/Linux
·Homebrew designs
·Perl
·Ruby
·Administration
·Backup/Recovery
·Bugs/Fixes
·Certification
·Database
·Email
·File/Print
·Hardware
·Information Grab Bag
·Interoperability
·GNU/Linux ABCs
·Monitoring
·Name Resolution
·Network Services
·Networking
·Remote Control
·Security
·Desktop
·Web
·BSD
·Solaris
·GIAGD
·ERP
·REALbasic
·All Categories


Writing a Bash For Loop
Topic:GNU/Linux   Date: 2005-08-03
Printer Friendly: Print

spacerspacer
<<  <   >  >>

Subject

The power to our lab machines is controlled by a device we built in 2001. It uses the serial port to control a bank of relays. For more information on the device and how to build it, see this article. Now, it turns out that we often need to simply turn on all machines and then turn them all off when we are done. While we had used a TCL/TK Wish interface with buttons for individual machines, it is useful to have a command that turns off every box, or turns them all on. To further complicate things, there needs to be a delay between turning on each relay because of voltage transients. It also gave us a chance to brush up on Bash. Here is a simple script to turn on all of the relays:

[usr-1@srv-1 ~]$ cat allon
#!/bin/bash
for ((i=100;i<=115;i+=1)); do
echo $i
sleep 1
echo $i > /dev/ttyS1
done
[usr-1@srv-1 ~]$

Let's run the script after making it executable:

[usr-1@srv-1 ~]$ chmod 700 allon
[usr-1@srv-1 ~]$ ./allon
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
[usr-1@srv-1 ~]$

Here is a script to turn them all off again:

[usr-1@srv-1 ~]$ cat alloff
#!/bin/bash
for ((i=200;i<=215;i+=1)); do
echo $i
sleep 1
echo $i > /dev/ttyS1
done
[usr-1@srv-1 ~]$

Let's make the script executable and run it:

[usr-1@srv-1 ~]$ chmod 700 alloff
[usr-1@srv-1 ~]$ ./alloff
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
[usr-1@srv-1 ~]$


People:
Places:
Things:
Times:





Please read our Terms of Use and our Privacy Policy
Microsoft, Windows, Windows XP, Windows 2003, Windows 2000, and NT are either trademarks or registered trademarks of Microsoft Corporation. NetAdminTools.com is not affiliated with Microsoft Corporation. Linux is a registered trademark of Linus Torvalds, and refers to the Linux kernel. The operating system of most distributions that contain the Linux kernel is GNU/Linux. All logos and trademarks in this site are property of their respective owner. Copyright 1997-2010 NetAdminTools.com