Bash Scripting : Tricks and examples

Get Input from user : Reference : http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_08_02.html

#!/bin/bash
# This script will test if you have given a leap year or not.

echo "Type the year that you want to check (4 digits), followed by [ENTER]:"

read year

if (( ("$year" % 400) == "0" )) || (( ("$year" % 4 == "0") && ("$year" % 100 !=
"0") )); then
  echo "$year is a leap year."
else
  echo "This is not a leap year."
fi

2) Removing Carriage Return & Line Feed from string :

String=$(echo $String | tr -d 'n' | tr -d 'r')

3) How to set a BASH variable equal to the output from a command?

var=$(ls -l)
OR
var=`ls -l`

Reference :  How to set a BASH variable equal to the output from a command?

4) Return Values from Bash Functions : Reference

http://www.linuxjournal.com/content/return-values-bash-functions 4) Problem accessing a global variable from within a while loop - http://stackoverflow.com/questions/4667509/problem-accessing-a-global-variable-from-within-a-while-loop

5) Proper ways to use CURL POST in json string :

curl -i 
-H "Accept: application/json" 
-H "Content-Type:application/json" 
-X POST --data '{"account":{"email":"'"$email"'","screenName":"'"$screenName"'","type":"'"$theType"'","passwordSettings":{"password":"'"$password"'","passwordConfirm":"'"$password"'"}},"firstName":"'"$firstName"'","lastName":"'"$lastName"'","middleName":"'"$middleName"'","locale":"'"$locale"'","registrationSiteId":"'"$registrationSiteId"'","receiveEmail":"'"$receiveEmail"'","dateOfBirth":"'"$dob"'","mobileNumber":"'"$mobileNumber"'","gender":"'"$gender"'","fuelActivationDate":"'"$fuelActivationDate"'","postalCode":"'"$postalCode"'","country":"'"$country"'","city":"'"$city"'","state":"'"$state"'","bio":"'"$bio"'","jpFirstNameKana":"'"$jpFirstNameKana"'","jpLastNameKana":"'"$jpLastNameKana"'","height":"'"$height"'","weight":"'"$weight"'","distanceUnit":"MILES","weightUnit":"POUNDS","heightUnit":"FT/INCHES"}' "https://xxx:[email protected]/xxxxx/xxxx/xxxx"

Reference : http://stackoverflow.com/questions/17029902/using-curl-post-with-variables-defined-in-bash-script-functions

6) Run curl from .sh script with defined Content-Type

Reference : http://stackoverflow.com/questions/12051329/run-curl-from-sh-script-with-defined-content-type

Android Android Build system Android Commands Android Java Applications Application Libraries Application Stack / User Interface Bash / Shell Scripts Commands and Packages Compilation Content Management System - CMS Core Kernel C Programs Development & Build Development Environment Setup Errors & Failures Flutter git Go Language Programs Hardware Platforms HTML JAVA Programs Kernel & Device Drivers Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL Monetization / Google AdSense NDK / Middleware / HAL OS Concepts PHP Programming Languages RaspberryPi Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Socurce Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Userspace Utilities Web design and development Wordpress Yocto / Bitbake / Openembedded

Leave a Reply / Ask Question