• Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Search

execute

Bash input executes functions

December 1, 2021

How to execute functions in Bash based on argument.

There are 2 functions: build and terminate.

#!/bin/bash
build () {
 echo 'Building services'
}
terminate () {
 echo 'Terminating services'
}
if [ $1 == 'build' ]; then build
elif [ $1 == 'terminate' ]; then terminate
else echo 'Valid options are "build" or "terminate"'
fi

#!/bin/bash build () { echo 'Building services' } terminate () { echo 'Terminating services' } if [ $1 == 'build' ]; then build elif [ $1 == 'terminate' ]; then terminate else echo 'Valid options are "build" or "terminate"' fi

Execute a function based on argument.

$ bash test.sh build
Building services
 
$ bash test.sh terminate
Terminating services
 
$ bash test.sh anyother
Valid options are "build" or "terminate"

$ bash test.sh build Building services $ bash test.sh terminate Terminating services $ bash test.sh anyother Valid options are "build" or "terminate"

Filed Under: Linux Tagged With: argument, bash, execute, function

CloudFormation Userdata

January 15, 2021

Here’s another way to add startup scripts to your instance during creation.

      UserData:
        Fn::Base64: !Sub |
          #cloud-config
          repo_upgrade: none
 
          runcmd:
            # Cloud init startup script
            - "bash /root/setup.sh"
 
          write_files:
            # Cloud init startup script
            - owner: root:root
              permissions: '0644'
              path: /root/setup.sh
              content: |
                #! /bin/bash -x
          write_files:
            # Cloud init startup script
            - owner: root:root
              permissions: '0644'
              path: /root/setup.sh
              content: |
                #! /bin/bash -x
                # run your bash commands here
                date > log.txt
                uptime >>  log.txt

UserData: Fn::Base64: !Sub | #cloud-config repo_upgrade: none runcmd: # Cloud init startup script - "bash /root/setup.sh" write_files: # Cloud init startup script - owner: root:root permissions: '0644' path: /root/setup.sh content: | #! /bin/bash -x write_files: # Cloud init startup script - owner: root:root permissions: '0644' path: /root/setup.sh content: | #! /bin/bash -x # run your bash commands here date > log.txt uptime >> log.txt

The above script creates a file setup.sh and executes it during instance creation. The output is dumped to log.txt file.

Filed Under: Cloud Tagged With: cloudformation, execute, file, run, startup, userdata

  • Home
  • About
  • Search

Copyright © 2023