November 25, 2024

Here is something fun to do using Linux Terminal – automatically open and close the CD/DVD tray all through a couple of commands.

The command to open the tray is :

eject

The command to close the tray is :

eject -t

In fact, let’s try to do something awesome, make a shell script that will open and close the CD/DVD tray say 3 times when the script is run. [Note  : don’t try to do such stuff on work computers etc, this is purely for demonstration ;)]

Shell script as follows :

#!/bin/bash
# Closes and opens CD/DVD tray through script
# Demonstrated by guys at ihaveapc.com for fun

echo " Opening and closing CD/DVD tray three times, to abort press Ctrl-C anytime"

cdremote=0

while [ $cdremote -le 2 ]
do

eject
eject -t
cdremote=$[ $cdremote + 1 ]

done

echo "Hope you had fun :) "

What the above code does is define a variable named cdremote and make it as 0. Till it becomes 2 (0,1,2 which is three times), a while loop simply runs the commands eject and eject -t to open and close the CD/DVD tray automatically.

Here is what the output will be (the messages will be displayed before and after the fun is over) :

Something for geek mischief : Try to change the script so that the CD/DVD tray will open and close automatically without stopping at all. A very basic virus/prank if you will or even a geek party trick. 😉

Do note that the eject -t command may not work on all systems like some laptops but for desktops this does work fairly well.

[ To get an idea about what are shell scripts and how to make as well as use them, go to this post].

Cheers.

By admin

Related Post