linux Part 5
Linux Shell Continued
Basic Shell
The shell can be logged into using a GUI Terminal. If you don’t have a GUI terminal setup because you want to save on hardware resources then you can login locally in to a text based console. Additionally if you wish to remotely login in using a text base protocol such as telnet or ssh.
The default shell in most distributions is the BASH shell “Bourne again shell” which is based on a older shell called the Bourne shell.
To open a terminal window use ctrl alt + t
Automation / scripts
Shell scripts allow us to automate tasks, a script allows us to execute a series of commands.
Command Line – Variables
Using a name or label to refer to some other quantity such as a value or command, like in algebra this is known as a variable. Variables are used to make the code more readable for users, however variables can be used in more advanced programming or scripting when the user is in a situation when they dont actually know the value before you execute the program. Variables act like a placeholder that gets resolved at the execution of the script. Variables are areas of memory that can be used to store information and are referred to by name.
When ever the shell sees something with a dollar sign and the name it assumes it is a variable and it tries to find out what was assigned to that variable and substitutes the proper values.
To create a variable the variable has to have a name followed immediately by an equal sign, for example:
age=
after the equal sign the information that needs to be stored is then going to be assigned, so if you wanted to assign the age variable for example:
age=40
Rules you need to know when naming variables:
Variables must start with a letter.
A name must not contain any embedded spaces, you can use underscores.
You can not use any punctuation marks
When a user starts a shell session some variables are already started by the startup file to see all the variables that are in the users environment you will use the command:
printenv
When you see the output from the command you will see the environment variables all in uppercase
LC_ADDRESS=de_DE.UTF-8
LC_NAME=de_DE.UTF-8
LC_MONETARY=de_DE.UTF-8
PWD=/home/rwelsh
LOGNAME=rwelsh
Quoting
Meta Characters
The most common meta characters are the space / blank character, this tells the shell to use separate arguments between those things, the shell does not pass any spaces / blanks to any commands, instead the shell is going to use those blanks and spaces to separate and identify any individual arguments for that particular command. One blank separates the same way as 10 or a 100 blanks does, any spaces are treated the same. Other shell meta characters include:
dollar sign ($)
star (*)
semi-colon (;)
greater-than symbol (>)
question mark (?)
ampersand (&)
pipe (|)
Quoting is the act of protecting the shell metacharacters from benign treated specially by the Shell.A quoted blank does not separate arguments where as an unquoted blank will, a quoted sem-colon does not separate commands the way a unquoted quoting is used to prevent the shell from acting on and expanding the meta characters, the quoting causes the shell to ignore that special meaning of the character so that the character gets treated like plain text, this means it can be treated as an argument if it needs to be.Quoting can be some with double quotes “, single ‘ or backslash \ characters, technically quoting is only applied to individual shell metacharacters protected from the shell and not to the entire command line argument, it would look better to surround the complete argument with matching quote marks.
Backslash may also be used to quote or turn off that special character’s ability following each character one at a time. Quotes and backslashes tell the shell which part of the input to treat as ordinary plain text characters and which to treat as special characters.
The quoting delimits or identifies this string of characters and the quoting mechanism is removed and is not part of the string that is passed to the command. Only the characters that are being quoted are going to be passed into the command. This allows the shell to remove the quoting mechanism before passing that delimited text as an argument back into the command and allows it to be understood.
Example 1
echo Hello\; World
output would be Hello; World
Example 2
echo “I have \$1200”
I have $1200 dollar
Proper Command usage
Opening Terminal 3 different ways in ubuntu
1.Right click on your desktop and select open in terminal
2.Press ctrl * alt & t
3.Goto application bar/ panel click the dots, then in the search bar type terminal, you will be presented with terminal, also ubuntu will offer more terminals downloadable via Ubuntu software repository.
Open a Terminal
press ctrl & + or – to correct the font size
In the terminal type ls to list the directory contents, you should see something like the following listed
rwelsh@Ubuntu01:~$ ls
Desktop Documents Downloads Music Pictures Public snap Templates Videos
The dollar is indicating that you are in the home directory
If you type the command pwd which stands for present working directory it will show you the full path all the way to the directory where you are currently in.
rwelsh@Ubuntu01:~$ pwd
/home/rwelsh
CD Change Directory
Now we will CD to a the Documents folder and create a file using the touch command:
rwelsh@Ubuntu01:~/Documents$ touch test1.txt
rwelsh@Ubuntu01:~/Documents$ ls
test1.txt
Using the ls command to see more details about the files in the Directory
ls -la
-rw-rw-r– 1 rwelsh rwelsh 0 Sep 18 12:29 test1.txt
In the current directory if we use the command printenv, you will see the output scroll to the bottom it will show on the last line , it will show us the directory we were in before moving to the document folder.
OLDPWD=/home/rwelsh
Now if we type in cd.. while in the Documents folder we will change to the directory we were in before we switched to documents.
/home/rwelsh
Now if we run the printenv it will show the previous directory, which was:
OLDPWD=/home/rwelsh/Documents
The concept of quoting
Running 2 commands on one line,
echo hello; ls
Now it will print hello and list the current directory
rwelsh@Ubuntu01:~$ echo hello; ls
hello
Desktop Documents Downloads Music Pictures Public snap Templates Videos
Man and Info and info pages
Their are 1000s of commands and options in linux which makes it almost impossible to remember them all. Manual (Man) Pages are a set of pages explaining every command available on the system, these pages explain what the command does and how to execute them and what command line and syntax the command will accept.
Man pages are invoked by typing man and then the command you want information on. For example:
man ls
User Commands
NAME
ls – list directory contents
SYNOPSIS
ls [OPTION]… [FILE]…
If you are not sure exactly what you are looking for then you can use the following command, man -k and the term you are looking for:
man -k ls
If you wish to search for a particular keyword with in a man page you can use the following command
/<search term>
you can use “n” for next to got to the next page to look for more search term results
Long and Short commands
Lots of commands often have 2 versions, a long and a short, for example if you want to see all files in a directory including hidden:
ls -a is the short command
ls – – all
both do exactly the same but the file 2nd variant ls –all is just more readable
The advantage of the short variant is that it is easier to chain together the commands and quicker to type out.
In addition to other man pages their is other documentation their is info documentation, this can be accessed by typing,
info ls
Info pages are like man pages but they are much more detailed, they are divided into different nodes or pages and works like a web browser. You can use:
p to go back /previous
n to go to next / go forward
q will exit the info page
There are other options in the info page.