Osama special meaning of any character that immediately

Osama Tawfik Shahir  / OPEN SOURCE PROJECT4th GRADE IT / ISHIKUNIVERSITY Usingand_configuring_bash  Using &Configuring BASH Linux Shell Overview • The Linux shell refers to aprogram that allows the user to interact with the system by entering commandsin a text- based command-line environment (as opposed to graphical desktopenvironments). The shell environment is also referred to as “CLI” orCommand-Line Interface Even though the graphical desktop environments for Linuxhave made some very significant breakthroughs in manners of system management& administration, without a need to use the shell, many advanced and professionalLinux/UNIX users still prefer to work in a shell environment as it providesquick and easy access to Linux’s scripting power-tools, advanced monitoring& debugging tools and more. Aliases • The primary focus in this course will beon BASH, which is a modern, growing shell and the successor of the commonlyused SH in both Linux & UNIX systems. • Shell aliases provide us with a wayto: ØSubstitute short commands for long ones.

Ø Turn a series of commands into a single commandthat executes them. Ø Create alternate forms of existing commands. ØAdd options to different commands and use those syntaxes as default. • To viewthe aliases for the current user, run: alias • Create a new alias with thecommand: alias aliasname=value • To remove an alias, use: unalias aliasname The “which” Command • The “which” command displaysthe pathname to accessible commands. • The output given by “which” is theabsolute pathname to the command searched.

• “which” is very useful in timescommands do not return the expected results, it allows to search for them andsee where they are being executed from. # which vim /usr/bin/vim # which vim/usr/bin/vim which filenamewhich filename Quoting • Shell meta-characters, as discussedbefore, interpret in a special way in the shell. • There are a number of waysto override these special meanings and have these characters behave like anyother regular character; this is done by quoting: Ø’ ‘ – single quotes cancel the special meaning of ALL metacharacters withinthem.

Ø” ” – double quotes cancel the special meanings for all metacharacters, exceptfor $ Ø- backslash cancels the special meaning of any character that immediatelyfollows. • Note that quotes are metacharacters themselves as well. Command History • The BASH shell saves a history ofevery command executed from command line. • The history is saved into a file,located in: ~/.bash_history • By default, BASH saves a history of 128 lastcommands; this value and the location in which the history is saved can becustomized. • In order to display the command history, run: “history options”ØRunning “history -3″ will display the last 4 commands entered. Ø”history 3” will display all commands from the 3rd line in the history file tothe last commanded entered.

Best services for writing your paper according to Trustpilot

Premium Partner
From $18.00 per page
4,8 / 5
4,80
Writers Experience
4,80
Delivery
4,90
Support
4,70
Price
Recommended Service
From $13.90 per page
4,6 / 5
4,70
Writers Experience
4,70
Delivery
4,60
Support
4,60
Price
From $20.00 per page
4,5 / 5
4,80
Writers Experience
4,50
Delivery
4,40
Support
4,10
Price
* All Partners were chosen among 50+ writing services by our Customer Satisfaction Team

Command History • The history file can be searchedfor specific strings in numerous ways: Ø CTRL-r will open the history search line, then wecan type in the command or string we wish to search for; once the string wewant is in, hitting CTRL-r again will jump to the next search hit. ØAnother search method would be to display the history file’s contents # history… 40 id 41 ls 42 cd myDir # history … 40 id 41 ls 42 cd myDir Shell Variables •Variables are placeholders for information. • Two types of variables exist: ØLocal – these variables affect the current shell session only.

ØEnvironment – these variables affect any shell session; they are automaticallyinitiated every time a new session starts. • Shell variables can be eitheruser-defined or built-into the system, they can also be pre-defined and thencustomized later. • When a variable is created, it is Local and effective onlywithin the shell environment that created it. • In order for a variable to beavailable in other sessions as well, it must be exported. Shell Variables • By convention, variables in Linuxare defined in upper-case characters; this is not a must though, lower-casecharacters would work just as well.

• This example creates a variable namedMAILLOG and assigns the value “/var/log/maillog” to it: ØMAILLOG=/var/log/maillog • Once we have defined the variable, we can now applyit in commands, such as: “vim $MAILLOG” which will start vim and the argumentprovided will be “/var/log/maillog” which is a file. • In order to display thevalue of an existing variable we can use: “echo $MAILLOG” • Keep in mind: LinuxIS case-sensitive. Shell Variables • The “$” sign is a metacharacterwith the meaning of “substitute with value”, it is used to expand the assignedvalue of a variable. • When attempting to expand a variable, the shell willlook in both its local and environment variable lists and find the valueassigned to the variable we’ve used, $MAILLOG in our case. Local Shell Variables • User-defined variablesenable the user to determine both the variable name and its value.

• The syntaxfor creating a new variable is: Ø VAR=value • Make sure there are no spaces ineither side of the “=” mark. • The “unset” command removes a variable, thesyntax is: Øunset VAR • All currently set variables and their values can be displayed withthe “set” command. Environment Variables • Environment variables arecopied to child processes upon creation. • Every process has its own copy andno process can touch another’s memory.

• In order to turn a local variable intoan environment variable we’d use the “export” command; there are two methods ofdoing this ØFirst method: create a local variable then export it in two commands: §MAILLOG=/var/log/maillog ; export MAILLOG Ø Second method: create the new variable while exportingit: §export MAILLOG=/var/log/mail Environment Variables • Linux provides the userwith the ability to change and customize the values of the default environmentvariables. • Environment variables can be temporarily modified for the currentshell session only and until it is closed. • In order to make environmentvariable changes permanent, their values will need to be changed in theinitialization files. • We can view the environment variables by running thecommand: “env”. The PATH Variable • The PATH variable allows the shell tolocate commands in directories, in the order they are defined in the variable.• In order to add a new directory to the PATH variable, we’d use the followingcommand: ØPATH=$PATH:/new/directory/here/ • PATH is already exported, there is no need toexport it again after adding to it. Variables & Command Expansion • Commandexpansion is the ability to use a custom command output anywhere when writingshell commands.

Use the “$()” meta-character to declare a command expansionblock $( command ; command ; … ) # ls dir1 file1 file2 # VAR=$( ls ) # echo$VAR dir1 file1 file2 # ls dir1 file1 file2 # VAR=$( ls ) # echo $VAR dir1file1 file2 The Initialization Files • Initialization filescontain commands and variable settings that are executed on any shell that isstarted • There are two levels of initialization files: ØSystem wide: /etc/profile – accessible only by the sys-admin. ØUser-specific: ~/.bash_profile and ~/.bashrc – accessible by the owning user. §The .bash_profile file is loaded once in the beginning of every session.

§.bashrc is loaded every time a new shell is opened, for example when opening ashell via the graphical desktop environment. Ø Neither of these files must exist but if they doexist, they will be read and applied by the system. ØThese two files can be used by the owning user to customer their own workingenvironment. The /etc/profile File • When a user logs in, thesystem first reads and applies everything from the /etc/profile file into thatuser’s environment and only then reads the user’s .

bash_profile and/or .bashrc• The /etc/profile file is maintained by the sys-admin: ØExports environment variables Ø Exports PATH and default command path. ØSets the variable TERM for the default terminal type. ØDisplays the contents of the /etc/motd file. Ø Sets the default file creation permissions.

BASH Tab Completion • BASH has the ability toauto-complete command, directory and file names upon hitting the TAB key. • Aslong as the string we wish to auto-complete is not ambiguous, hitting TAB oncewill get the job done. • In such cases of ambiguous strings, we can hit TABtwice and it will list all of the options that begin with the string we haveprovided the command line with.

• Adding another character that would changeour string from ambiguous to unique would allow a single TAB to auto- completeit. Shell Scripts • Shell commands can be run as anindividual set of tasks, or a unified ‘flow’ of tasks. This is usually called a”Shell Script” • Shell Scripts can simply be a serial set of command ls ; df ;ps and can include flow control, arithmetic operators, variables and functions• The most basic qualifier for a shell script is that it is saved in a file Thefirst line of this file, should be the “#!” meta-character which indicateswhich type of shell script this file is #!/bin/bash command Shell Scripts – Conditions • In order to run two ormore commands, in a serial manner, we use the ‘;’ meta-character. • By usingthe “OR” (||) or “AND” (&&) meta-characters, we can add the appropriatelogical condition to our command-set The decision whether to run the nextcommand, is based on the “Exit Status” of the previous command, which is alsoviewable by reading the value of the special variable “$?”. Exit status valueof 0 means success or ‘true’.

When the value is bigger than 0 it is treated as’false’ # ls -l file && echo “My file exists” -rw-r–r– 1user staff 4 Jul 22 13:00 file My file exists Shell Scripts – Conditions • In order to runmultiple commands after a logical condition, we can use the “{ }”meta-characters to declare a ‘Command Set’ # ls file && { > echo”My file exists” > ls -l file > echo “This is good news”> } file My file exists -rw-r–r– 1 shaycohen staff 4 Jul 22 13:00 fileThis is good news # ls file && { > echo “My file exists”> ls -l file > echo “This is good news” > } file My fileexists -rw-r–r– 1 shaycohen staff 4 Jul 22 13:00 file This is good news ShellScripts – Conditions • Another way of using conditions is by using the ‘if’command if expression then command else command fi # if ls nosuchfile > then> echo “My file exists” > else > echo “My file does notexist” > fi ls: nosuchfile: No such file or directory My file does notexist # if ls nosuchfile > then > echo “My file exists” >else > echo “My file does not exist” > fi ls: nosuchfile: Nosuch file or directory My file does not exist Shell Scripts – Conditions •There are three main types of expressions – Logical Expression ” expression” expression can be any of the valid flags of the “test” command – ArithmeticExpression “(( expression ))” expression can be any valid flags of the “expr”command Use manual pages to find how to use the ‘expr’ and ‘test’ commands. Q:What will the following command return as output # expr 1+1 Shell Scripts – Loops • Bash supports three maintypes of loops – List loop “for” – Conditional loop “while” | “until” for VARin “value1” “value2” … do command $VAR done while expression do command done Shell Scripts – Loops # ls dir1 file1 file2 # forFILE in file1 file2 > do > ls $FILE && echo “$FILEExists” > done # ls dir1 file1 file2 # for FILE in file1 file2 > do> ls $FILE && echo “$FILE Exists” > done Command lineparsing • Before running given commands, Bash parse the given command andarguments and replaces any meta-characters with the evaluated value. • Use Bashwith the ‘-x’ flag to get detailed information about every command parsingresult # for FILE in $(ls) > do > echo “$((COUNT++)) – $FILE”> done 0 – dir1 1 – file1 2 – file2 # for FILE in $(ls) > do > echo”$((COUNT++)) – $FILE” > done 0 – dir1 1 – file1 2 – file2 # forFILE in dir1 file1 file2# for FILE in dir1 file1 file2 > echo “0 – dir1″>echo “0 – dir1” > echo “1 – file1″> echo “1 – file1” > echo “2 -file2″> echo “2 – file2” Exercise • Write your first shell script: hello.shThe script should print out “Hello World” to the terminal