Skip to main content

Shell Script: Find Number Of Arguments Passed


Many times , when we create shell scripts we try to do repetitive tasks through functions. Some functions take arguments & we have to check the no. of arguments that are passed to it.

Each bash shell function has the following set of shell variables:

[a] All function parameters or arguments can be accessed via $1, $2, $3,..., $N.
[b] $* or $@ holds all parameters or arguments passed to the function.
[c] $# holds the number of positional parameters passed to the function.
[d] An array variable called FUNCNAME ontains the names of all shell functions currently in the execution call stack.

Example

Create a shell script as follows:
#!/bin/bash
# Purpose: Demo bash function
# -----------------------------
 ## Define a function called test()
test(){
  echo "Function name:  ${FUNCNAME}"
  echo "The number of positional parameter : $#"
  echo "All parameters or arguments passed to the function: '$@'"
  echo
}

## Call or invoke the function ##
## Pass the parameters or arguments  ##
test linuxtechtips
test 1 2 3 4 5
test "this" "is" "a" "test"

Run it as follows:
$ chmod +x script.name.here
$ ./script.name.here


Comments

Popular posts from this blog

Virtual Box and Alt/Tab Keys

I use virtual box for all my testing activities. It comes too often that I have a virtual box VM window open & I want to switch to my host machine to see some stuff like tutorials etc.. If you press the alt+tab combination it just works inside the VM & doesn't switches to host machine. In these scenarios you can press the host key once ( not hold it ) & then whatever you press goes to host machine. So in general where host key is the default Right Ctrl, just press Right Ctrl once & now press the alt+tab & it will switch you out to host machine. This is really helpful when you have the VM windows open or you're working on seamless mode. Hope it help others too.

CentOS / Redhat : Configure CentOS as a Software Router with two interfaces

Linux can be easily configured to share an internet connection using iptables. All you need to have is, two network interface cards as follows: a) Your internal (LAN) network connected via eth0 with static ip address 192.168.0.1 b) Your external WAN) network is connected via eth1 with static ip address 10.10.10.1  ( public IP provided by ISP ) Please note that interface eth1 may have public IP address or IP assigned by ISP. eth1 may be connected to a dedicated DSL / ADSL / WAN / Cable router: Step # 1: Enable Packet Forwarding Login as the root user. Open /etc/sysctl.conf file # vi /etc/sysctl.conf Add the following line to enable packet forwarding for IPv4: net.ipv4.conf.default.forwarding=1 Save and close the file. Restart networking: # service network restart Step # 2: Enable IP masquerading In Linux networking, Network Address Translation (NAT) or Network Masquerading (IP Masquerading) is a technique of transce...

Create a SSL Certificate for Apache on CentOS 6

About Self-Signed Certificates A SSL certificate is a way to encrypt a site's information and create a more secure connection. Additionally, the certificate can show the virtual private server's identification information to site visitors. Certificate Authorities can issue SSL certificates that verify the virtual server's details while a self-signed certificate has no 3rd party corroboration. Step One—Install Mod SSL In order to set up the self signed certificate, we first have to be sure that Apache and Mod SSL are installed on our VPS. You can install both with one command: yum install mod_ssl Step Two—Create a New Directory Next, we need to create a new directory where we will store the server key and certificate mkdir /etc/httpd/ssl Step Three—Create a Self Signed Certificate When we request a new certificate, we can specify how long the certificate should remain valid by changing the 365 to the number of da...