Linux shell programming : variables and functions
site.sh
:
#!/bin/sh site_name="bogotobogo" year=2019 echo "$site_name : $year"
Lets run the code:
$ ./site.sh bogotobogo : 2019
The shell provides a way to protect variables as read-only by using the readonly
command. After a variable is marked read-only, we cannot change the value of the variable.
#!/bin/sh site_name="bogotobogo" readonly site_name site_name="google"
Let's run the script:
$ chmod +x rd_only.sh $ ./rd_only.sh ./rd_only.sh: 5: ./rd_only.sh: site_name: is read only
As we can see from the output, we cannot assign to the variable declared as readonly.
Once we unset a variable, we cannot access the variable:
#!/bin/sh # rd_only_unset.sh site_name="bogotobogo" readonly site_name year=2015 echo "site_name : $site_name, year : $year" unset year echo "site_name : $site_name, year : $year"
Run it:
$ ./rd_only_unset.sh site_name : bogotobogo, year : 2015 site_name : bogotobogo, year :
If we try to unset
a readonly
variable, we get an error message:
#!/bin/sh # rd_only_unset.sh site_name="bogotobogo" readonly site_name year=2015 echo "site_name : $site_name, year : $year" unset year unset site_name echo "site_name : $site_name, year : $year"
In the code, we cannot unset the variable "site_name" because it is set as readonly
:
$ ./rd_only_unset.sh site_name : bogotobogo, year : 2015 ./rd_only_unset.sh: 10: unset: site_name: is read only
When we want to add two numbers, we may need to use $(( expression )):
#!/bin/bash sum=0 for i in {1..10} do sum=$(( sum+i )) done echo $sum
If we drop "$", we get an error. Basically, we are trying to assign a value via "=" to "sum", and "$" is the one that tells shell to extract the value of "sum+i". So, we cannot drop it.
We can use another form:
#!/bin/bash sum=0 for i in {1..10} do sum=$(( $sum+$i )) done echo $sum
A Bash function can't return a string directly but instad we can use echo
.
The following code takes an arg and returns a new string with "hello" prepended:
### f1.sh #!/bin/bash function myFunc() { echo "hello $1" } myFunc $1
Output:
$ ./f1.sh foo hello foo
The code below returns an input string via "echo":
### f2.sh #!/bin/bash function myFunc() { echo "$1" } echo "hello $(myFunc $1)"
Output:
$ ./f2.sh foo hello foo
To return a value, a function can use shared vaiables:
### f3.sh #!/bin/bash retval=10 function myFunc() { retval=$(( $retval+100 )) } myFunc echo "retval=$retval"
Output:
$ ./f3.sh retval=110
Here is a sample script (leap.sh) with a function that gets one arg (year) and returns either 0 (non-leap year) or 1 (leap year). The script itself gets multiple arguments and passes one arg at a time in a for loop:
#!/bin/bash isLeap() { year=$1 if [ $(( $year % 4 )) -eq 0 ]; then if [ $(( $year % 100 )) -eq 0 ]; then if [ $(( $year % 400 )) -eq 0 ]; then return 1 else return 0 fi else return 0 fi fi } # Leap year # No: 1800, 1900, 2100, 2200, 2300, 2500 # Yes: 2000, 2400 for year in $@ do isLeap $year retval=$? if [ $retval -eq 0 ]; then echo "$year : No" else echo "$year : Yes" fi done
Output:
$ ./leap.sh 1800 1900 2000 2100 2200 2300 2400 2500 1800 : No 1900 : No 2000 : Yes 2100 : No 2200 : No 2300 : No 2400 : Yes 2500 : No
Linux - system, cmds & shell
- Linux Tips - links, vmstats, rsync
- Linux Tips 2 - ctrl a, curl r, tail -f, umask
- Linux - bash I
- Linux - bash II
- Linux - Uncompressing 7z file
- Linux - sed I (substitution: sed 's///', sed -i)
- Linux - sed II (file spacing, numbering, text conversion and substitution)
- Linux - sed III (selective printing of certain lines, selective definition of certain lines)
- Linux - 7 File types : Regular, Directory, Block file, Character device file, Pipe file, Symbolic link file, and Socket file
- Linux shell programming - introduction
- Linux shell programming - variables and functions (readonly, unset, and functions)
- Linux shell programming - special shell variables
- Linux shell programming : arrays - three different ways of declaring arrays & looping with $*/$@
- Linux shell programming : operations on array
- Linux shell programming : variables & commands substitution
- Linux shell programming : metacharacters & quotes
- Linux shell programming : input/output redirection & here document
- Linux shell programming : loop control - for, while, break, and break n
- Linux shell programming : string
- Linux shell programming : for-loop
- Linux shell programming : if/elif/else/fi
- Linux shell programming : Test
- Managing User Account - useradd, usermod, and userdel
- Linux Secure Shell (SSH) I : key generation, private key and public key
- Linux Secure Shell (SSH) II : ssh-agent & scp
- Linux Secure Shell (SSH) III : SSH Tunnel as Proxy - Dynamic Port Forwarding (SOCKS Proxy)
- Linux Secure Shell (SSH) IV : Local port forwarding (outgoing ssh tunnel)
- Linux Secure Shell (SSH) V : Reverse SSH Tunnel (remote port forwarding / incoming ssh tunnel) /)
- Linux Processes and Signals
- Linux Drivers 1
- tcpdump
- Linux Debugging using gdb
- Embedded Systems Programming I - Introduction
- Embedded Systems Programming II - gcc ARM Toolchain and Simple Code on Ubuntu/Fedora
- LXC (Linux Container) Install and Run
- Linux IPTables
- Hadoop - 1. Setting up on Ubuntu for Single-Node Cluster
- Hadoop - 2. Runing on Ubuntu for Single-Node Cluster
- ownCloud 7 install
- Ubuntu 14.04 guest on Mac OSX host using VirtualBox I
- Ubuntu 14.04 guest on Mac OSX host using VirtualBox II
- Windows 8 guest on Mac OSX host using VirtualBox I
- Ubuntu Package Management System (apt-get vs dpkg)
- RPM Packaging
- How to Make a Self-Signed SSL Certificate
- Linux Q & A
- DevOps / Sys Admin questions
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization