1-Intro_linux_bash
1-Intro_linux_bash
• Shell: a program that interpret a command given by user and execute it.
• Bash: popular UNIX shell: command line interface and scripting language.
• easy to create and very useful to perform some tasks when dealing with
climate data.
Why do we use Linux and bash?
• Shell (Bash Script)
• Command-line interface • Powerful scripting language
(CLI): most powerful and (compiled several commands
flexible way to interact with and run it at once)
the computer • Reproducibility
• Commands tell the the • Save time
computer what to do • Portable and almost installed
• Combined commands (i.e. get everywhere
faster) • Managing jobs
• managing external programs
Some applications
• Getting information
• Networking
Prompt alima@aimsit:~$
• command line ls
• option –l
• argument Desktop
• cursor
Getting information & display command: echo
Commands:
whoami $ echo Hello AIMS
hostname Hello AIMS
date $ echo Hello AIMS
pwd Hello AIMS
cal
Task: Try out the different effects of quoting by print out the variable $HOME.
Getting out of trouble
Whenever you need help with a command type “man” and the
echo “AIMS stands for African Institute for Mathematical Sciences ” >
aims.txt
echo “This course is about Linux basis .” >> aims.txt
cat aims.txt
use the tab key (make like easier)
Manipulating files (2) : list, rename, copy, delete
• list files and directories
• ls
• Task: What does ls -l do? Try also ls –lrt.
• rename a file
• mv aims.txt limbe.txt
• copy a file
• cp aims.txt test.txt
• delete a file
• rm test.txt
• Some tips:
• Ctrl+ C Cancel
• Enter run command
• Up arrow display history
• Left/right arrows command for editing
Working with directories
• create a directory
• mkdir climate
• mkdir –p /home/alima/Desktop/climate
• move file to directories
• mv aims.txt climate
• Task: copy aims.txt to the folder climate located in the Desktop
• list file within a directory
• ls climate/
• change name of a directory
• mv climate/ climate_lab
Working with directories (2)
• changing directories (navigation)
• cd Desktop
• cd or $ cd ~ or cd $HOME
• cd .. (goes one directory up)
• delete a directory
• rm –r climate_data
• rmdir climate_data
• Some important rules
• avoid spaces when giving file names;
• Dot is not advisable in file naming
• Do not use weird characters (i.e. ) in file naming
Print file contents and search for files
• display a file content
• cat aims.txt
• head aims.txt (display first few lines of the file, 10 by default)
• tail -20 aims.txt (display last 20 lines of the file)
• less aims.txt
• more aims.txt
Networking
• download a file at an URL
• wget https://download.linux.org/rocky-8.4-x86_64.iso)
Pipes, special characters, quoting
• pipe: chain filter commands
• ls | sort
• ls | sort –r
• seq 1000 9999| grep 55
• ls –l |less
• special characters
# comments
; separate command
“ ” interpret text literally but except some special characters (e.g. ${var})
‘’ preserves all special characters within
\ Escape: quoting next character (e.g. echo \#)
var=42
echo "\${var} = ${var}"
echo '\${var} = ${var}'
Bash: special characters (2)
❑ $( ) Command substitution $ echo "today = $(date)"
old version: ` ` (backticks) today = Fri 02 Jun 2023 07:11:17 PM
CAT
❑ [] part of test builtin or array element (e.g. if [ 2 -gt 0 ] ; then echo yes ; fi)
Bash: Wildcards
* match any multiple characters in a file (directory) name
e.g. list all files starting with output
$ ls output*
e.g. list all files ending with txt
$ ls *txt
e.g. list all files starting with n or p
$ ls [n,p]*
? match any single character
$ ls /bin/?ash
^ to negate the wildcard match
e.g. list all files not starting with a ls [^a]*
Let do some practices
• Task 1: create a directory called computing_lab in your Desktop.
create a file within this directory, name it test.sh and write this text: ‘#!/bin/bash’
to the file ; rename this file to (new.sh) and display the content of this file.
var1=AIMS
Variable name Variable content
• array1+= (CLIMATE)
file
group other
directory user
Bash: File permissions (3)
• Task:
• -rwxrwxrwx : What does this code stands for?
• drw- ------:???
• Symbols meanings: u: user; g: group; o: other; r: read; w: write; x: execute;
+ add permission; - take away permission.
• chmod [options] file (change access rights for named file)
• chmod [user/group/others/all]+[permission] [file]
• chmod go+rwx list (give read, write and execute permissions on the file
called list to group and other)
• Code meaning:
a : all; u: user; g: group; o: other; + add; - remove
Some quick moves
• Use tab completion or see possible list of files in a directory
• Recall commands with up arrow
• Use man command name to know more about any command
• clear
Run a program
• Make sure the program has executable permissions
• Use “./” to run the program (e.g. ./myscript.sh)
• bash myscript.sh
File editors
example:
#!/bin/bash
## Example : Loop over generated integer sequence
counter=1
for i in {1..10} ; do
echo "loop no. ${counter}: ${i}"
let counter+=1
done
while Loops
while condition in list
do
command
done
• Iterates command(s) as long as condition is true
example:
#!/bin/bash
## Purpose: Loop until max is reached
max=10
i=1
while (( ${max} >= ${i} )) ; do
echo "${i}"
let i+=1
done
Functions
function my_name () • Stores a series of commands for later or
{ repetitive execution
commands • Functions are called by writing the name
}
example
#!/bin/bash
## Purpose: Demonstrating features of functions
## pass today’s date
Function today ()
{
echo " $(date) "
}
today
Exercises
• Write a bash script named exercise1.sh which print years from 1900 to 2000
using a for loops.
• Do the same as in exercise1.sh using a while loops (print the years by step
of 10) and call the script exercise2.sh .
• Write a script that download today’s forecast from Global Forecasting (GFS)
data for by step of 6 hours and save output to your Desktop. Name the script
exercise3.sh (link to the data:
http://www.ftp.ncep.noaa.gov/data/nccf/com/gfs/prod/gfs/)