0% found this document useful (0 votes)
36 views6 pages

ELEC423 - Tutorial 04 - Handouts

The document discusses Linux environment and shell variables. It provides examples of how environment variables are accessible by child processes, while shell variables are only accessible within the current shell. Shell variables are defined with a NAME=VALUE format and environment variables are exported using export NAME=VALUE.

Uploaded by

BhaskarAllayer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views6 pages

ELEC423 - Tutorial 04 - Handouts

The document discusses Linux environment and shell variables. It provides examples of how environment variables are accessible by child processes, while shell variables are only accessible within the current shell. Shell variables are defined with a NAME=VALUE format and environment variables are exported using export NAME=VALUE.

Uploaded by

BhaskarAllayer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

11/7/19

The Internet of Things:


Architecture and Applications
(ELEC423)

Dr Valerio Selis

[email protected]

Survey results
Pyt hon: funct ions and classes

Pyt hon: working with files, date and time

Pyt hon: control flow and exception

Pyt hon: datatypes, operations and operators

Pyt hon scri pting

Linux: Shell script ing

Linux: Troubleshooting in Linux

Linux: Ownership and permissions

Linux: Environment and shell variables

Linux: Shell commands


00
. 0 % 1 0.0 0% 2 0.0 0% 3 0.0 0% 4 0.0 0% 5 0.0 0% 6 0.0 0% 7 0.0 0% 8 0.0 0% 9 0.0 0%

Response rate: 43%

Environment and Shell variables


Linux uses two types of dynamic values which affect
programs or processes’ behaviours:
• Environment variables: which are defined for the current
shell and can be used by child shells or processes
• Shell variables: which are available only for the shell in
which these were defined
By convention shell and environmental variables are
defined by using capital letters, e.g.:
• Environment variables:
export KEY=value
export KEY="value1 and value2"
export KEY=value1:value2
• Shell variables:
KEY=value or KEY="v1 and v2" or KEY=v1:v2

1
11/7/19

Environment and Shell variables


Example of environment variable:
Shell Shell
~ $ export TEST="Hello" ~ $ echo $TEST
~ $ echo $TEST Hello
Hello ~ $ exit
~ $ bash exit
~ $

Example of shell variable:


Shell Shell
~ $ TEST="Hello" ~ $ echo $TEST
~ $ echo $TEST
Hello ~ $ exit
~ $ bash exit
~ $

?? Question
?
/

/bin /dev /home /media /proc /sbin /sys /usr

/boot /etc /lib /opt /root /srv /tmp /var

wpa_supplicant pi

wpa_supplicant.conf halloween.txt

$ ifconfig wlan0
wlan0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.1.10 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::a02b:f89d:3cee:8c77 prefixlen 64 scopeid 0x20<link>
ether b8:27:eb:00:00:01 txqueuelen 1000 (Ethernet)
RX packets 9458 bytes 678102 (662.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 6200 bytes 1792703 (1.7 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

Answer
T="/"
Ls $t
-bash: Ls: command not found
bash
cat $T/ect/wpa_supplicant
cat: /ect/wpa_supplicant: No such file or directory
exit
cd
pwd
/home/pi
ls
halloween.txt
echo ~/pi/file.txt
/home/pi/pi/file.txt
ifconfig wlan0 | grep "inet"
inet 192.168.1.10 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::a02b:f89d:3cee:8c77 prefixlen 64 scopeid 0x20<link>
export T="~/"
bash
cat T/halloween.txt
cat: T/halloween.txt: No such file or directory
exit

2
11/7/19

Shell scripting
Shebang followed by the interpreter
#!/bin/bash
# Print Hello World to the standard output Comment
echo "Hello World"
# Assign the output of the echo command to a variable
VARIABLE=$(echo "Hello World")
# Redirect the output of the echo command to a file
echo $VARIABLE > file.txt
# Ask the user to provide an input
echo "Provide an input: "
# Store the standard input value in the “input” variable
read input
# Show the given input
Double quotation
echo "The input given is: $input"

Use to access to the


value of the variable

Single vs Double Quotes


• Single quotes ('): this preserve the literal value of
each character
• Double quotes ("): similar to single quotes, but
with the exception of $, `, \ characters, where the
backslash indicate to use the literal value of the
following character
• Example1: pre-defined variable TEST="fun":
$ echo 'The content of TEST is $TEST'

The content of TEST is $TEST


$ echo "The content of TEST is $TEST"

The content of TEST is fun

Single vs Double Quotes


• Example2: execute a command:
$ echo 'ls `pwd`'
ls `pwd`
$ echo "ls `pwd`"
Desktop Documents Downloads MagPi
Music Pictures Public Templates Videos

• Example3: using the backslash: String is not terminated,


$ echo '\'' expecting the last '

>
$ echo "\""

"

3
11/7/19

Single vs Double Quotes


• Example4: show double quote:
$ echo '"'
"
$ echo "\""
"

• Example5: show single quote: String is not terminated,


$ echo ''' expecting the last '

>
$ echo "'"

'

10

Question
echo '"'"'"'"'

??
?
11

Answer

echo '"'"'"'"'

$ echo '"'"'"'"'

$ echo '"' "'" '"'

"'"

12

4
11/7/19

Shell scripting Retrieve the number of


attributes
#!/bin/bash
# Check if a filename has been given in input
if [ $# != 1 ];then
echo "Usage: $0 file"
exit 1 Script name
fi
# Check if the file exists Exit with an error code
if [ ! -f $1 ];then
echo "No such file $1"
exit 1 Filename given in input
as first attribute
if
# Show and count the number of lines Initialise the line counter
lc=0
while read;do Built-in variable for the
echo "$REPLY" read command
lc=$[ $lc + 1 ]
done < $1 Same as
echo "The file $1 has $lc lines" lc=$((lc+1))
exit 0
Exit with a normal code

13

Shell scripting Definition of the


wifi_info function
#!/bin/bash
wifi_info() {
essid=$(iwconfig $1 | grep "ESSID" | tr "\"" " " | awk '{print $5}')
bssid=$(iwconfig $1 | grep "Point" | awk '{print $6}')
echo "The AP name is $essid and its MAC address is $bssid" Pipe
}
# Check if a filename has been given in input
if [ $# != 2 ];then
echo "Usage: $0 <network type> <interface>"
echo " e.g.: $0 wifi wlan0"
exit 1
fi
# Show the arguments given in input
for arg in "$@";do
echo $arg
done
if [ "$1" = "wifi" ];then Retrieve the attributes
wifi_info $2 given in input
elif [ "$1" = "ethernet" ];then
echo "Sorry not implemented yet!"
else $ iwconfig wlan0
exit 1 wlan0 IEEE 802.11 ESSID:”eduroam"
Mode:Managed Frequency:2.437 GHz Access Point: 00:11:22:33:44:55
fi
Bit Rate=72.2 Mb/s Tx-Power=31 dBm
exit 0 Retry short limit:7 RTS thr:off Fragment thr:off
Power Management:on
Link Quality=61/70 Signal level=-49 dBm
Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0
Tx excessive retries:0 Invalid misc:0 Missed beacon:0

14

Shell scripting
Strings

• Basic operation with strings:


• Obtain the string length:
${#name}
• Convert the string as a list of words:
${name}
• Extract the substring of length (LEN) from a string starting
after a specified position (POS):
${name:$POS:$LEN}
The length is optional, if omitted, it will extract the
substring from POS to the end of the string.

15

5
11/7/19

? Question
Create a shell script that uses the output from the
traceroute command, transform it to a list and,
print out the IP count and the IP address.
traceroute to google.com (216.58.211.174), 30 hops max, 60 byte packets
1 192.168.43.1 (192.168.43.1) 2.366 ms 3.029 ms 3.128 ms
2 169.254.252.10 (169.254.252.10) 49.557 ms 49.822 ms 51.895 ms
3 172.16.220.1 (172.16.220.1) 51.596 ms 51.668 ms 51.371 ms
4 172.16.220.10 (172.16.220.10) 49.817 ms 40.445 ms 50.942 ms
5 dub08s01-in-f14.1e100.net (216.58.211.174) 50.580 ms 38.572 ms 37.716 ms

Example:
1 192.168.43.1
2 169.254.252.10

??
3 172.16.220.1
4 172.16.220.10
5 216.58.211.174

?
16

Next class?

Today at 1 p.m. in the


Electrical Engineering &
Electronics Building,
ELEC402 PC lab
(Building 235).

17

You might also like