Skip to content

Latest commit

 

History

History

0x03-shell_variables_expansions

0x03. Shell, init files, variables and expansions

In this project, i learned about linux expansion, shell arithmetic ,shell initialization files and shell variables

commands

man or help:

  • printenv
  • set
  • unset
  • export
  • alias
  • unalias
  • .
  • source
  • printf

Learning Objectives

General

  • What happens when you type $ ls -l *.txt

Shell Initialization Files

  • What are the /etc/profile file and the /etc/profile.d directory
  • What is the ~/.bashrc file

Variables

  • What is the difference between a local and a global variable
  • What is a reserved variable
  • How to create, update and delete shell variables
  • What are the roles of the following reserved variables: HOME, PATH, PS1
  • What are special parameters
  • What is the special parameter $??

Expansions

  • What is expansion and how to use them
  • What is the difference between single and double quotes and how to use them properly
  • How to do command substitution with $() and backticks

Shell Arithmetic

  • How to perform arithmetic operations with the shell

The alias Command

  • How to create an alias
  • How to list aliases
  • How to temporarily disable an alias

Requirements

General

  • Allowed editors: vi, vim, emacs
  • All your scripts will be tested on Ubuntu 20.04 LTS
  • All your scripts should be exactly two lines long ($ wc -l file should print 2)
  • The first line of all your files should be exactly #!/bin/bash
  • A README.md file, at the root of the folder of the project, describing what each script is doing
  • You are not allowed to use &&, || or ;
  • You are not allowed to use bc, sed or awk
  • All your files must be executable

More Info

Read your /etc/profile, /etc/inputrc and ~/.bashrc files.

Look at some files in the /etc/profile.d directory.

Note: You do not have to learn about awk, tar, bzip2, date, scp, ulimit, umask, or shell scripting, yet.

Tasks

  1. <o> : A script that creates an alias.
    • Name of alias: ls
    • Value: rm *
  2. Hello you : A script that prints hello user, where user is the current Linux user.
  3. The path to success is to take massive, determined action : A script that adds /action to the PATH. /action should be the last directory the shell looks into when looking for a program.
  4. If the path be beautiful, let us not ask where it leads : A script that counts the number of directories in the PATH.
  5. Global variables : A script that prints all the enviroment variables.
  6. Local variables : A script that lists all local variables and enviroment variables, and functions.
    • Name of variable : HOLBERTON
    • Value : Betty
  7. Local variable : A script that creates a new local variable.
  8. Global variable : A script that creates a new global variable.
    • Name of variable : HOLBERTON
    • Value : Betty
  9. Every addition to true knowledge is an addition to human power : A script that prints the results of the addition of 128 with the value stored in the enviroment variable TRUEKNOWLEDGE, followed by a new line.
    • Remember to export variable TRUEKNOWLEDGE : export TRUEKNOWLEDGE=1209
    • Run command this way: ./8-true_knowledge | cat -e
  10. Divide and rule : A script that prints the result of POWER divide by DIVIDE, followed by a new line.
    • POWER and DIVIDE are environment variables.
    • Variables values;
    • export POWER=42784
    • export DIVIDE=32
    • Run command this way: ./9-divide_and_rule | cat -e
  11. Love is anterior to life, posterior to death, initial of creation, and the exponent of breath : A script that displays the result of BREATH to the power of LOVE.
    • BREATH and LOVE are enviroment variables.
    • The script should display the result, followed by a new line.
  12. There are 10 types of people in the world -- Those who understand binary, and those who don't : A script that converts a number from base 2 to base 10.
    • The number in base 2 is stored in the enviroment variable BINARY.
    • The script should display the number in base 10, followed by a new line.
  13. Combination : A script that prints all possible combinations of two letters, except oo.
    • Letters are lower cases, from a to z.
    • One combination per line.
    • The output should be alpha ordered, starting with aa.
    • Do not print oo.
    • Your script file should contain maximum 64 characters.
  14. Floats : A script that prints a number with two decimal places, followed by a new line.
    • The number will be stored in the enviroment variable NUM.
  15. Decimal to Hexadecimal : A script that converts a number from base 10 to base 16.
    • The number is base 10 is stored in the enviroment variable DECIMAL.
    • The script should display the number in base 16, followed by a new line.
  16. Everyone is a proponent of strong encryption : A script that encodes and decodes text using the rot13 encryption. Assume ASCII.
  17. The eggs of the brood need to be an odd number : A script that prints every other line from the input, starting with the first line.
  18. I'm an instant star. Just add water and stir. : A script that adds the two numbers stored in the enviroment variables WATER and STIR and prints the results.
    • WATER is in base water.
    • STIR is in base stir.
    • The result should be in base behlnort.