Open main menu

CDOT Wiki β

Changes

ULI101 Week 10

222 bytes added, 21:32, 4 September 2019
m
Adds ULI101 and ULI101-2018 categories
* <code>.bash_logout</code> :: Executed when you log out
=== <code>/etc/profile</code> ===
This file can only be modified by the root user. Affects the environment of all users, regardless of their default shell. Bash users can change their environment by modifying the <code>.bash_profile</code> or the <code>.bashrc</code> files. Different shells have different configuration files. Other configuration files such as <code>.profile</code> exist - read comments in your <code>.bash_rc</code> file to find out more
=== <code>.bashrc</code> and <code>.bash_profile</code> ===
Located in the user’s home directory. These files are executed every time a user logs in or creates a new shell. Things vary depending whether the shell is interactive or not. By modifying either one of these files, each user can change his individual working environment. They can be used for the following:
* Mapping new keys on the keyboard
== Shell History ==
Many shells keep a history of recently executed command lines in a file. This history is used by users to save time, when executing same or similar commands over and over. Bash uses the up/down arrow keys. Use the Ctrl+r to search by keyword. Bash stores it’s history in the <code>.bash_history</code> file
== Alias ==
A way to create “shortcuts” or temporary commands in UNIX. Stored in memory, while the user is logged in. Usually found in the <code>.bash_profile</code>. Syntax:
<code>alias clearfile=”cat /dev/null &gt;”</code>
= Shell Variables =
Shell variables a classified in 2 groups:
Variables can be read/write or read-only. Name of a variable can be any sequence of letters and numbers, but it must not start with a number.
== Common Shell Variables ==
Shell environment variables shape the working environment whenever you are logged in. Common shell variables include:
{|
| <code>PS1</code>
| primary prompt
|-
| <code>PWD</code>
| present working directory
|-
| <code>HOME</code>
| absolute path to user’s home
|-
| <code>PATH</code>
| list of directories where executables are
|-
| <code>HOST</code>
| name of the host
|-
| <code>USER</code>
| name of the user logged in
|-
| <code>SHELL</code>
| current shell
|}
The <code>set</code> command will display all available variables
== The <code>PATH </code> variable ==
* <code>PATH</code> is an environment variable present in Unix/Linux operating systems, listing directories where executable programs are located
* Use <code>./</code> prefix or modify the <code>PATH</code> as needed
== Assigning a Value ==
Syntax: <code>name=value</code>
$
</source>
 == Read-Only Variables ==
* Including the keyword <code>readonly</code> before the command assignment prevents you from changing the variable afterwards. For example: <code>readonly phone=“123-4567”</code>
* If no variable name is supplied a list of defined read only variables will be displayed
== Removing Variables ==
<code>variable=</code>
Read-only variables cannot be removed, you must log out for them to be cleared.
== Variable Substitution ==
Whenever you wish to read a variable (its contents), use the variable name preceded by a dollar sign ($). This is commonly called ''variable substitution''.
Alice
</source>
 
 
= Introduction to Shell Scripting =
=== Usefulness of Shell programming ===
* Scope ranges from simple day-to-day tasks to large database driven CGI applications.
* <code>$#</code> represents the number of parameters (not including the script name)
== <code>echo</code> command ==
* Displays messages to the terminal followed by a newline. Use the <code>-n</code> option to suppress the default newline.
* Arguments are usually double quoted.
== <code>read</code> command ==
* The <code>read</code> command allows obtaining user input and storing it in a variable. Everything is captured until the Enter key is pressed.
echo Hello $name
</source>
 == Using Logic ==
The purpose of the if statement is execute a command or commands based on a condition. The condition is evaluated by a test command, represented below by a pair of square brackets
fi
</source>
 == <code>test</code> Command ==
The <code>test</code> command can be used in two ways:
* Use <code>=</code> and <code>!=</code> to compare strings, for example: <code>[ ”$name” = ”Bob” ]</code>
* Use <code>-z</code> and <code>-n</code> to check string length, for example: <code>[ ! -z “$name” ]</code>* Use <code>-gt</code>, <code>-lt</code>, <code>-eq</code>, <code>-ne</code>, <code>-le</code>, <code>-ge</code> for number, for examplenumbers like: <code>[ ”$salary” -gt 100000 ]</code>
Common file test operations include:
{|
| <code>-e</code>
| file exists
|-
| <code>-d</code>
| file exists and is a directory
|-
| <code>-s</code>
| file exists and has a size greater than zero
|-
| <code>-w</code>
| file exists and write permission is granted
|}
Check <code>man test</code> for more details
== Using Loops ==
A for loop is a very effective way to repeat the same command(s) for several arguments such as file names. The syntax and example of for command is shown below:
done
</source>
 
[[Category:ULI101]]
[[Category:ULI101-2018]]