Diving Into Bash: Difference between revisions

From HackRVA
Jump to navigation Jump to search
No edit summary
No edit summary
 
(15 intermediate revisions by 2 users not shown)
Line 4: Line 4:


== Where am I? ==
== Where am I? ==
At this point you should have your shell open and ready for input.
At this point you should have your [http://www.linfo.org/shell.html shell] open and ready for input.


To find which directory you are in, type the following command.
To find which directory you are in, type the following command.
Line 54: Line 54:




  cd /
== Permissions ==
  cd ~
Exit it out of the man page and you should see the output of your last command
   pwd
 
'''drwxr-xr-x 2 linux101 linux101  4096 Mar 10 11:29 myNewDirectory'''
 
The output is showing us the permissions, ownership, datestamp and Name of the directory.
You can read more on permissions [http://www.linux.org/threads/file-permissions-chmod.4094/ here.]
 
 
 
Change the permissions on your new directory and remove access to "Others."  You can do this by running the following command:
 
   chmod 750 myNewDirectory
   ls -l
   ls -l


   chmod 777 myNewDirectory
 
   ls -l
You should now see something similar to this:
   rmdir myNewDirectory
 
'''drwxr-x--- 2 linux101 linux101  4096 Mar 10 11:29 myNewDirectory'''
 
Read, Write and Execute is enabled for the User.  The Group has read and execute. Others should have no access.
 
 
 
[[File:LinuxPermissions.jpg]]
 
 
== Creating a File ==
 
Let's make sure we are in ''myNewDirectory''.
 
   cd ~/myNewDirectory
 
To create a new file we will use the [http://linux.die.net/man/1/touch touch] command.
 
Create a new file and view the contents of that file with the [http://linux.die.net/man/1/cat concatenate ] command.
 
  touch newFile
  cat newFile
 
The cat command should not return anything.  This is because the file is empty.  Try adding some content to the file.
 
  echo $'The ships hung in the sky in much the same way that bricks don\'t.\n\n\t ― Douglas Adams, The Hitchhiker\'s Guide to the Galaxy ' >> newFile
   cat newFile
 
Excellent! We should now have text content in our newFile.
 
== Text Editor ==
In this step by step we will be introducing you to the command line text editor vim, however there are several other good options such as emacs and nano
 
  $vim newFile
 
You should now be in the vim text editor.  Here are some good vim commands for your to know
 
  a - insert text behind the cursor            i - insert text ahead of the cursor
 
  k - move up    j - move down                l - move right    h - move left
 
  n dd - remove “n” of lines from cursor      n dw - remove “n” of words from cursor
 
  :q! - quit without making changes          :wq - write quit
 
  x - delete character at cursor              n p - paste “n” of times
 
  o - insert blank line under cursor          yy - copy
 
  :w - write file                            :n - move to line “n”
 
  (esc key) leave insert mode
 
Press the "A" key to get into insert mode and type the following into your text file:
 
  #!/bin/bash
 
   PS3='Choose one word: '
 
  #bash select
  select word in "linux" "bash" "scripting" "tutorial"
  do
    echo "The word you have selected is: $word"
  #Break, otherwise endless loop
    break
  done
 
  exit 0
 
Now press the "esc" key and then type ":wq" and press "Enter".  You should be back at the bash command prompt.
 
You have just created you first bash script.  Now lets change our file permissions and run it.
 
  $chmod 750 myscript
 
  $./myscript
 
This should be your output
 
  1) linux
  2) bash
  3) scripting
  4) tutorial
  Choose one word: 4
  The word you have selected is: tutorial
 
We are not going to get to get much into bash scripting here but this is a small example of what is possible from the command line in Linux
 
== Sudo ==
 
Often you need root permissions to make a change or execute certain applications on your system.  Sudo allows you to temporarily elevate your privelages
while not logged in as root. (You still must have the root password).
 
  $sudo [command] [-options] [arguments]
 
== Package Manager ==
 
One of those situations where you will often use sudo is installing new applications with a package manager.  In Debian the package
manager is apt.  We will install an application using the apt-get and sudo commands.
 
  $sudo apt-get install htop
 
The command prompt should prompt you for the root password and give you the option of whether to install the package or not.  Install the package and then
run it by typing:
 
  $htop
 
If you have any further questions please reference the slide show linked at the bottom of this page.
 
 


== External Links ==
== External Links ==
# linux.com : [https://www.linux.com/learn/tutorials/272346-bash-101-working-at-the-cli ''Bash 101: Working at the CLI''], Joe 'Zonker' Brockmeier
# [https://www.linux.com/learn/tutorials/272346-bash-101-working-at-the-cli ''Bash 101: Working at the CLI'']

Latest revision as of 17:51, 16 March 2016

Introduction to Linux(Linux 4 n00bs)


Where am I?

At this point you should have your shell open and ready for input.

To find which directory you are in, type the following command.

 pwd

This will output the directory that you are currently working in.

pwd stands for Print Working Directory.

Make a directory

Now, let's make a new directory!

Type the following command:

 mkdir myNewDirectory

mkdir stands for Make Directories.


If you type ls, you will see the directory you just created.

 ls

The ls command will list the contents of a directory.

Change Directory

To navigate between directories, you will need to use the Change Directory command. Take note of the output of pwd:

 cd myNewDirectory
 pwd
 cd ..
 pwd
 cd /
 pwd
 cd /home/linux101
 pwd
 ls -l

Notice that we've added a parameter to the end of the ls command. Passing parameters to a command can allow you to make that function behave differently.

If you compare the difference between ls and ls -l , you will see that ls -l gives you a more detailed view of this data (i.e. a long list.) You can discover more parameters of a command by looking at it's man page.


Yo dawg, take a look at the man page of the man command.

 man man

A man page will typically give you a synopsis, description, examples of the command and what options(parameters) it will take.


Permissions

Exit it out of the man page and you should see the output of your last command

drwxr-xr-x 2 linux101 linux101 4096 Mar 10 11:29 myNewDirectory

The output is showing us the permissions, ownership, datestamp and Name of the directory. You can read more on permissions here.


Change the permissions on your new directory and remove access to "Others." You can do this by running the following command:

 chmod 750 myNewDirectory
 ls -l


You should now see something similar to this:

drwxr-x--- 2 linux101 linux101 4096 Mar 10 11:29 myNewDirectory

Read, Write and Execute is enabled for the User. The Group has read and execute. Others should have no access.



Creating a File

Let's make sure we are in myNewDirectory.

 cd ~/myNewDirectory

To create a new file we will use the touch command.

Create a new file and view the contents of that file with the concatenate command.

 touch newFile
 cat newFile

The cat command should not return anything. This is because the file is empty. Try adding some content to the file.

 echo $'The ships hung in the sky in much the same way that bricks don\'t.\n\n\t ― Douglas Adams, The Hitchhiker\'s Guide to the Galaxy ' >> newFile
 cat newFile

Excellent! We should now have text content in our newFile.

Text Editor

In this step by step we will be introducing you to the command line text editor vim, however there are several other good options such as emacs and nano

 $vim newFile

You should now be in the vim text editor. Here are some good vim commands for your to know

 a - insert text behind the cursor            i - insert text ahead of the cursor
 
 k - move up    j - move down                 l - move right    h - move left
 
 n dd - remove “n” of lines from cursor       n dw - remove “n” of words from cursor
 
 :q! - quit without making changes           :wq - write quit
 
 x - delete character at cursor               n p - paste “n” of times
 
 o - insert blank line under cursor           yy - copy 
 
 :w - write file                             :n - move to line “n”
 
 (esc key) leave insert mode

Press the "A" key to get into insert mode and type the following into your text file:

 #!/bin/bash
 
 PS3='Choose one word: '
 
 #bash select
 select word in "linux" "bash" "scripting" "tutorial"
 do
   echo "The word you have selected is: $word"
 #Break, otherwise endless loop
   break
 done
 
 exit 0

Now press the "esc" key and then type ":wq" and press "Enter". You should be back at the bash command prompt.

You have just created you first bash script. Now lets change our file permissions and run it.

 $chmod 750 myscript
 
 $./myscript

This should be your output

 1) linux
 2) bash
 3) scripting
 4) tutorial
 Choose one word: 4
 The word you have selected is: tutorial
 

We are not going to get to get much into bash scripting here but this is a small example of what is possible from the command line in Linux

Sudo

Often you need root permissions to make a change or execute certain applications on your system. Sudo allows you to temporarily elevate your privelages while not logged in as root. (You still must have the root password).

 $sudo [command] [-options] [arguments]

Package Manager

One of those situations where you will often use sudo is installing new applications with a package manager. In Debian the package manager is apt. We will install an application using the apt-get and sudo commands.

 $sudo apt-get install htop

The command prompt should prompt you for the root password and give you the option of whether to install the package or not. Install the package and then run it by typing:

 $htop

If you have any further questions please reference the slide show linked at the bottom of this page.


External Links

  1. Bash 101: Working at the CLI