Saturday, September 19, 2026
HomeNetworkingUtilizing features in bash to selectively run a bunch of Linux instructions

Utilizing features in bash to selectively run a bunch of Linux instructions


Utilizing a operate in bash permits you to create one thing in Linux that works as if it have been a script inside a script. Every time the information being processed matches a set of situations, your script can name a operate that does additional processing.

The format of a operate may be very simple. The syntax appears to be like like this:

<function_name> () {
    <instructions>
}

It’s also possible to use the next format that makes use of the phrase “operate” should you favor:

operate <function_name> {
    <instructions>
}

The truth is, it’s also possible to create a operate on a single line if the instructions to be run are restricted, however notice the required “;” that follows the command(s):

<function_name> () { <instructions>; }

Substitute the weather contained in the < and > indicators (together with the indicators as nicely) with the identify you need to use and the instructions you need to run. Capabilities can embrace any selection and any variety of instructions.

Within the easy instance beneath, one operate (replspaces) is outlined that may change the areas in textual content with underscores utilizing a sed command. A second (dropspaces) was added that removes the areas.

#!/bin/bash

replspaces ()  sed 's/ /_/g'

dropspaces ()  sed 's/ //g'; 

replspaces "Howdy World"
replspaces `date`
dropspaces "Howdy World"
dropspaces `date`

Once you run the script, it’s best to see one thing like this:

$ nospaces
Hello_World
Tue_Oct_12_12:55:26_AM_EDT_2022
HelloWorld
TueOct1212:55:26AMEDT2022

Within the considerably extra complicated instance beneath, the script creates a listing of UIDs from the /and so forth/passwd file after which sends any which might be within the UID vary for person accounts (1000 and better) to the operate to assemble the group memberships for every of those accounts. It makes use of the proceed command to keep away from processing the no one account. Discover that the operate must be outlined earlier than it’s referred to as.

#!/bin/bash

# receive extra group memberships from the /and so forth/group file
listgroups () {
    echo -n "$uid: "
    uname=`grep ":$uid:" /and so forth/passwd | awk -F: '{print $1}'`
    echo $uname
    for group in `grep $uname /and so forth/group | awk -F: '{print $1}'`
    do
      if [ $group != $uname ]; then
        echo "      $group"
      fi
    accomplished
}

# collect listing of UIDs from the /and so forth/passwd file
whereas learn -r line
do
  uids=`awk -F: '{print $3}'`
accomplished < /and so forth/passwd

for uid in `echo $uids`
do
  if [ $uid -eq 65534 ]		# no one
  then
    proceed
  fi
  if [ $uid -ge 1000 ]		# person account
  then
    listgroups $uid
  fi
accomplished

When the script is run, it generates output like this:

$ list_users_and_groups
1000: shs
      wheel
      techs
1002: bugfarm
1003: dbell
1004: dumbo
1005: eel
      wheel

As you possibly can see, customers corresponding to “dbell” have just one account. Group names from the /and so forth/group file will solely be added if they’re completely different than the account’s username.

Wrap-up

Capabilities can be utilized for selective processing any time the necessity arises. They typically make scripts simpler to learn by separating a bunch of instructions that target a single job, however also can comprise code that may in any other case should be included quite a few instances in a script.

Be a part of the Community World communities on Fb and LinkedIn to touch upon subjects which might be high of thoughts.

Copyright © 2022 IDG Communications, Inc.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments