Technical – BASH On Windows
Technical – BASH On Windows.
BASH On Windows? If you are a frequent visitor to Brent’s World you will know that while I have had a Windows d 7 desktop in the past I am a long time Mac user. So it should come as no surprise that I would want to BASH On Windows, Right? WRONG! In this case I am referring the ability to run BASH (Bourne Again Shell scripts, inside Linux on Windows!
I thought I would give it a try and run one of the scripts that I have posted on the blog and see if this BASH On Windows really works!
I will reinvent the wheel by blogging how to install BASH On Windows, as tit has been already heavily blogged, instead I will link to the blog I used to install it, it is detailed and straight forward. To read Abhishek Prakash’s great blog on installing Linux Bash Shell On Windows 10, click HERE.
So after downloading Ubuntu, it was time to play! Launching for the first time, you are asked to enter a username and password. Keep this handy! As I do not yet know how to reset lost passwords J Something to learn and blog at a later date?
So let’s see how well this works, let’s pick a BASH script from the blog, The Great 8 Ball Oracle will work nicely.
You should be put in your home directory when launching Ubuntu, but just to make sure we will print the working directory by entering the pwd command. Rememeber Linux is a case sensitive OS, so you must enter the command in all lowercase PWD, or Pwd will not work.
$ pwd
The shell should return output that looks like this.
/home/<username>
If it returns something else change directories to your home directory by using the cd command and the ~ shortcut for your home directory.
$ cd ~
The BASH script should return you to the prompt if it was successful. You can then print the working directory again.
Once we have confirmed that we are in our home directory, let us load the text editor of our choice. I use nano. We will name the file 8-ball.sh. At your shell prompt enter.
$ nano 8-ball.sh
Enter the script as shown in the blog (Do not enter the line numbers!)
Once you have finished entering the script, hold down the control key and press X
You will then be asked to “Save modified buffer?”
Answer Y for Yes and press enter to keep the filename 8-ball.sh.
You will then be returned to the main shell prompt.
If you type ls -l, you will now see 8-ball.sh in your directory
$ ls -l
total 4
-rw-rw-rw- 1 <username> <group> <size> <date / time> 8-ball.sh
The output of the long list command shows you the file permissions, owner, group, file size in bytes, date and time the file was last written to and the filename.
The format of the file permissions are:
Owner permissions -rw (read and write) Group Permissions -rw (read and write) World Wide (Others) (Permissions -rw (read and write)
In order for us to be able to execute the script, we need to add the eXecute permission. We can add this permission to either the owner of the file (In this case you.), to a specific group (It should match your username), or the world wide permissions.
There is no reason we cannot grant everyone the permission to execute this script so let’s add the execute permission world wide. At the shell prompt type the following command.
$ chmod ugo+x 8-ball.sh
If the command was successful, you will be taken back to the shell prompt. Type ls -l again and press enter.
The permissions should now look like this
$ ls -l
Total 4
-rwx-rwx-rwx 1 <user-name> <group name> 2672 Aug 6 03:03 8-ball.sh
$
There is now an x for eXecute in the owner, group, and world wide (other) blocks. The script is now executable by everyone!
So let’s go ahead and run the script. Since your home directory is not in your execution path, we will need to precede the filename by ./ , This tells Linux to start the path in the current directory. Otherwise we would have to tell linux where the file resides from the root of the file system and use the full path. In this case /home/<username>/. So it is just easier to use ./ .
If the script was typed in correctly, you should see the following output.
I am very happy to finally see cross scripting funcionality incorporated into Windows. Not only will this make it easier for developers to edit and compile code on their Windows machines without the need for virtual machines. It should also give system administrators that are supporting both Windows and *Nix servers the ability to write scripts to perform tasks on both their Windows and Linux machines.
We will explore installing and maintaining additional BASH features in future blogs. Such as installing two cows so we can have our random sayings displayed when opening BASH On Windows. Thank you for stoping by Brent’s World. I hope you enjoyed this weeks technical blog. If you wish to be notified by email when new content is posted. Please register by clicking HERE!
Comments
Technical – BASH On Windows — No Comments
HTML tags allowed in your comment: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>