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) Remving 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:xxxxx@xxxx-www.xxxxx.com/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 Commands Android Java Applications Application Libraries Bash / Shell Scripts Bluetooth driver Build Frameworks Commands and Packages Core Kernel C Programs Development Environment Setup Documents / Books Errors & Failures File Systems Framebuffer / Display Driver git Go Language Programs Hardware Platforms Home Kernel & Device Drivers Kernel Booting and Porting Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL NDK / Middleware / HAL Network Driver OS Concepts PHP Procfs Filesystem 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