Home‎ > ‎

Shell Scripting IFS Usaage Part-3

This post is new learning experiment on Internal Field Separator IFS. It is more efficient than tr command.By default IFS is available in the bash shell with ' ' space as delimiter which we could use directly in the scripts. This page contains two different experiments:
  1. How multiple IFS values can be used?
  2. Using IFS ftp the logs to remote path [sample snippet] 

1. How multiple IFS Values can be used in a Shell script?

The following example will illustrate the variations
  • The default IFS value uses space
  • Using single delimit char
  • Using multiple delimit char set
#!/bin/bash
# Description   :       Script to illustrate the IFS usage

clear
echo -e "\nInitially..."
echo
mystring="vybhava:Technologies Trainings Consulting Development"
for word in $mystring; do
  echo "Word: $word"
done

# Now lets apply the IFS
echo -e "\nApplying IFS=\":\" ..."
echo

IFS=":"
for word in $mystring; do
  echo "Word: $word"
done

echo -e "\nApplying IFS=\": \" ..."
echo

IFS=": "
for word in $mystring; do
  echo "Word: $word"
done
Execution of the above script gives us the follows:



2. FTP Logs to remote box using IFS Trick

Now in the cloud and virtualization era we need lots of automation strategies to save time and cost, where we need to develop such scripts which can be more effective and optimized scripts.
Here is sample remote domains.txt file

vybhava.biz:192.168.33.100:/home/oracle/logs:wladmin
mydev01.com:192.168.33.1:/home/oracle/logs:ftpuser
mydev02.com:192.168.33.2:/home/oracle/logs:ftpuser
mydev03.com:192.168.33.3:/home/oracle/logs:ftpuser

#!/bin/bash
#
# Date          :       31/01/2016
# Description   :       This sample IFS usage code help you to understand FTP Log files to remote box
#

file=domains.txt

# set the Internal Field Separator to :
IFS=':'
while read -r domain ip logpath ftpusername
do
        echo -e  "*** Connecting to $domain ..."
        echo -e  "Using $ip ip for process..."
        echo -e  "Log path is set to $logpath"
        echo -e  "Adding ftp access for $domain using $ftpusername ftp account...\n\n"

done < $file

Execution of the above script as follows:

Thanks for being with me, Like this page and Share this article with your social media friends...

Comments