Welcome to PC Scripts Daily, third edition! Yeah, I know. I didn't post yesterday. I was busy working on JavaScript.
PC Scripts Enhancements
I wrote some JavaScript to customize PC Scripts. There is a new Page Rendering section on the sidebar. If you click Move Sidebar, the sidebar will be moved to the right. If you click it again, it will move back. If you click Remove Sidebar, the sidebar will be removed. If PC Scripts isn't being rendered correctly (sometimes, the sidebar is displayed twice or three times), clicking Reframe should fix the error. Unfortunatly, your settings are not saved. I'm still working on that.
Apple September Event
On September 1st, I will be releasing PC Scripts Daily later in the day to allow time for me to watch and write about the event.
My Buyers' Guide
If you want to buy something like an iPod Touch or iMac, leave a comment on one of the PC Scripts Daily editions and I will keep track of the item on PC Scripts Daily.
Bash Tutorial - Part 2 - I/O, Variables, and Scripts
If you want to be able to do useful things with Bash, you need to know how to get input from & output information to the terminal. Without that, your programs can calculate things and be very accurate & reliable but it would be impossible to know what the output of these calculations are.
Before you learn I/O, you need to know how to use variables. Variables are small pieces of information stored during a terminal session. As soon as the terminal window or session is closed, all set variables are destroyed. To create a variable, you would type <name of variable>=<value>. For example, if I wanted to create a variable named foo with a value of 1, I would type foo=1. The variable name is not foo, though. It's $foo. This makes it very easy to distinguish between variables & strings.
The echo command outputs data to a command line. If you want to create the well known Hello World program, you would use the echo command. The syntax of the echo command is: echo <what you want to display>. Below is the code & the output:
|
Code:
|
Output:
|
echo "Hello World!"
|
Hello World!
|
To
echo a variable, you would type
echo $<name of variable>. For example, if I wanted to output the contents of
foo (which is 1), I would type
echo $foo.
What's the point of programming if you can't get input from anywhere? There is no point! The best way to get input from the terminal is by using the read command. The syntax for this command is read <name of variable>. This will allow the user to type in a value for the specified variable. If the variable specified already exists, it will overwrite the existing value. If it doesn't, it will be created. For example, to ask the user to type in a value for foo2, the following code will be used. In the output, all bold text is typed in by the user:
|
Code:
|
Output:
|
echo "Type in a value for foo2:"
read foo2
echo "foo2 is now $foo2"
|
Type in a value for foo2:
Hi
foo2 is now Hi
|
To make Bash run a bunch of commands without you having to type them in every time, you can create a script. Once you create a script and run it in Bash, Bash will act as if each line of the script is a command. Using this, you can make programs that other people can use without much effort. To create a script, create a new file with your favorite editor. When you name it, make sure you put .sh at the end (i.e. foo.sh). Type any command you want Bash to execute into the file, one command per line. Before you can run your script, you have to make it executable. Otherwise, Bash will treat it like a regular file, not as a list of commands that it can execute. To make it executable, type chmod u+x <name of file>. This will tell Bash that you are allowed to execute the file. If you want everyone on your system to be able to execute your script, drop the u (user) from the command and the added permissions will apply to everyone. Once you have made your script executable, you (and anyone else you have allowed) can execute the script. To do this, you will need the full path of the script, the .. shortcut, or the . shortcut. If your pwd is not anywhere near the file, you can execute the script by giving the full path. For example, if the script was in your ~/Scripts directory, it can be executed by typing ~/Scripts/foo.sh. If the script is in the parent directory, you can execute it by typing ../foo.sh. If your script is in the pwd, you can execute it by typing ./foo.sh.
Next Edition
That's all for today! Check back tomorrow (hopefully) for Bash tutorial, part 3 and some more stuff!