Friday, March 31, 2017

Bash: Scripting - rewrite trimeline


#!/bin/bash
#
#
FILE=/homeb/d54712/sandbox/scripts/data.d/lorem.txt
#
exec 3< $FILE
function trimline ()
{
        #IFS='\n'
        MAXLEN=$(($LINELEN - 3))
        if [ "${#1}" -le $LINELEN ]
        then
                echo "$1"
        else
        echo " ${1:0:${MAXLEN}} \\"
        fi
}
exec <&0
LINELEN=${1:-80}
exec <&3
while read line
do
        #echo "$line"
        trimline "$line"
done

Bash: Scripting - Revised

#!/bin/bash
#
#
#
FILE=/homeb/d54712/sandbox/scripts/data.d/lorem.txt
exec 3< $FILE # Create a new File Descriptor for STDIN from file
function trimline()
{
        MAXLEN=$((LINELEN - 3))
        if [ "${#1}" -le $LINELEN  ]
        then
                echo "$1"
                echo "Width: ${#1}"
        else
                echo "${1:0:${MAXLEN}} \\ "
        fi
}

# STDIN from keyboard to get input for LINELEN
exec <&0
LINELEN=${1:-80} # if variable is undefined or null substitute with 80 (i.e ${var:-word})

# Redirect STDIN from FILE
exec <&3
while read myline
do
        trimline "$myline"
        #echo "$myline"
done




#debug
echo "LINELEN:$LINELEN"
echo "MAXLEN: $MAXLEN"




Results:
$ ./trimline.sh 30
Width: 0
rem ipsum dolor sit amet, c \
Morbi vestibulum sem in ege \
vehicula, eros ex dapibus a \
ante ipsum primis in faucib \
Maecenas porta pretium null \
tincidunt ut. Vivamus tempu \
lobortis. Phasellus quis mi \
ultricies nunc id imperdiet \
arcu tincidunt consectetur.
Width: 27
Width: 0
Phasellus urna ex, viverra  \
scelerisque elit id varius  \
Etiam at ligula at felis or \
Proin cursus, ipsum in vene \
vestibulum turpis sapien a  \
risus lectus, quis faucibus \
augue vitae, elementum nunc.
Width: 28
Width: 0
LINELEN:30
MAXLEN: 27

Bash: Scripting - trimline

#!/bin/bash
#
function trimline()
{
        MAXLEN=$((LINELEN -3)) # allow space for " \ " at end of line
        if [ "${#1}" -le "${LINELEN}"  ]
        then
                echo "$1"
        else
                echo "${1:0:${MAXLEN}} \\"
                trimline "${1:${MAXLEN}}"
        fi

}
LINELEN=${1:-80} # default to 80 columns (${var:-word} if var is undefined or null then substitute #its value as "word")


while read myline
do
        trimline "$myline"
done

        echo "LINELEN: $LINELEN"
        echo "MAXLEN: $MAXLEN"
~


# cat /home/kensipe/sandbox/scripts/data.d/data1.txt | ./trimline.sh 50

Thursday, March 30, 2017

Bash: Script - part 4: Archivieing in Dynamic Directory

#!/bin/bash
#
# used in arcive directory
YEAR=$(date +%Y)
MONTH=$(date +%m)
DAY=$(date +%d)
# used in arcive file name
TIME=$(date +%H%M%S)
# read files to be backed up
CONFIG_FILE=/homeb/d54712/sandbox/scripts/data.d/Files_To_Backup
# Base destination path (augmented with archive directory
BASE_DEST=/homeb/d54712/sandbox/scripts/data.d
 # Archive File name
AR_FILE=$TIME.tar.gz
# make archive directory
mkdir -p $BASE_DEST/$YEAR/$MONTH/$DAY
# Backup Destination
DESTINATION=$BASE_DEST/$YEAR/$MONTH/$DAY/$AR_FILE
# Redirect STDIN to $CONFIG_FILE
exec < $CONFIG_FILE
# Read each line in from STDIN ($CONFIG_FILE)
while read FILE_NAME
do
        if [ -f "$FILE_NAME" -o -d "$FILENAME" ]
        then
                # Store each line in variable
                FILE_LIST="$FILE_LIST $FILE_NAME"
        else
                echo
                echo "$(tput setaf 1)WARNING: $(tput setaf 3) <<$FILE_NAME>> $(tput setaf 4) does not exist $(tput setaf 7)"
                echo
        fi
done
# Back up portion
echo "Starting Archive..."
echo

sleep 1
tar -cvzf $DESTINATION $FILE_LIST 2> /dev/null
sleep 3
echo
echo "Archive File: $DESTINATION"
echo



# debug
#echo "FILE_LIST"
#echo "$FILE_LIST"
#echo "Year: $YEAR"
#echo "Month: $MONTH"
#echo "Day: $DAY"
#echo "Time: $TIME"
#echo "Configuration File: $CONFIG_FILE"
#echo "Base Destination: $BASE_DEST"
#echo "AR_FILE: $AR_FILE"
#echo "Archive File: $DESTINATION"






Result:







Wednesday, March 29, 2017

Bash: Script - part 3: Archiving in Dynamic Directory

#!/bin/bash
#
# used in arcive directory
YEAR=$(date +%Y)
MONTH=$(date +%m)
DAY=$(date +%d)
# used in arcive file name
TIME=$(date +%H%M%S)
# read files to be backed up
CONFIG_FILE=/homeb/d54712/sandbox/scripts/data.d/Files_To_Backup
# Base destination path (augmented with archive directory
BASE_DEST=/homeb/d54712/sandbox/scripts/data.d
 # Archive File name
AR_FILE=$TIME.tar.gz
# make archive directory
mkdir -p $BASE_DEST/$YEAR/$MONTH/$DAY
# Backup Destination
DESTINATION=$BASE_DEST/$YEAR/$MONTH/$DAY/$AR_FILE
# Redirect STDIN to $CONFIG_FILE
exec < $CONFIG_FILE
# Read each line in from STDIN ($CONFIG_FILE)
while read FILE_NAME
do
        if [ -f "$FILE_NAME" -o -d "$FILENAME" ]
        then
                # Store each line in variable
                FILE_LIST="$FILE_LIST $FILE_NAME"
        else
                echo
                echo "$(tput setaf 1)WARNING: $(tput setaf 3) <<$FILE_NAME>> $(tput setaf 4) does not exist $(tput setaf 7)"
                echo
        fi
done
echo "FILE_LIST"
echo "$FILE_LIST"
echo "Year: $YEAR"
echo "Month: $MONTH"
echo "Day: $DAY"
echo "Time: $TIME"
echo "Configuration File: $CONFIG_FILE"
echo "Base Destination: $BASE_DEST"
echo "AR_FILE: $AR_FILE"
echo "Archive File: $DESTINATION"

Result:
#debug








Bash: Script - part 2: Archiving in Dynamic Directory

#!/bin/bash
#
# used in arcive directory
YEAR=$(date +%Y)
MONTH=$(date +%m)
DAY=$(date +%d)
# used in arcive file name
TIME=$(date +%H%M%S)
# read files to be backed up
CONFIG_FILE=/homeb/d54712/sandbox/scripts/data.d/Files_To_Backup
# Base destination path (augmented with archive directory
BASE_DEST=/homeb/d54712/sandbox/scripts/data.d
 # Archive File name
AR_FILE=$TIME.tar.gz
# make archive directory
mkdir -p $BASE_DEST/$YEAR/$MONTH/$DAY
# Backup Destination
DESTINATION=$BASE_DEST/$YEAR/$MONTH/$DAY/$AR_FILE
# Redirect STDIN to $CONFIG_FILE
exec < $CONFIG_FILE
# Read each line in from STDIN ($CONFIG_FILE)
while read FILE_NAME
do
        # Store each line in variable
        FILE_LIST="$FILE_LIST $FILE_NAME"
done

echo "FILE_LIST"
echo "$FILE_LIST"

echo "Year: $YEAR"
echo "Month: $MONTH"
echo "Day: $DAY"
echo "Time: $TIME"
echo "Configuration File: $CONFIG_FILE"
echo "Base Destination: $BASE_DEST"
echo "AR_FILE: $AR_FILE"
echo "Archive File: $DESTINATION"



Result:
#debug
$ ./read3.sh
FILE_LIST
 /auto/home/homeb/d54712/sandbox/scripts/data.d/file1 /auto/home/homeb/d54712/sandbox/scripts/data.d/file2 /auto/home/homeb/d54712/sandbox/scripts/data.d/file3 /auto/home/homeb/d54712/sandbox/scripts/data.d/file4 /auto/home/homeb/d54712/sandbox/scripts/data.d/file5
Year: 2017
Month: 03
Day: 29
Time: 110444
Configuration File: /homeb/d54712/sandbox/scripts/data.d/Files_To_Backup
Base Destination: /homeb/d54712/sandbox/scripts/data.d
AR_FILE: 110444.tar.gz
Archive File: /homeb/d54712/sandbox/scripts/data.d/2017/03/29/110444.tar.gz

Bash: Script - part 1: Archiving in Dynamic Directory

#!/bin/bash
#
# used in archive directory
YEAR=$(date +%Y)
MONTH=$(date +%m)
DAY=$(date +%d)
# used in archive file name
TIME=$(date +%H%M%S)
# read files to be backed up
CONFIG_FILE=/homeb/d54712/sandbox/scripts/data.d/Files_To_Backup
# Base destination path (augmented with archive directory
BASE_DEST=/homeb/d54712/sandbox/scripts/data.d
 # Archive File name
AR_FILE=$TIME.tar.gz
# make archive directory
mkdir -p $BASE_DEST/$YEAR/$MONTH/$DAY
# Backup Destination
DESTINATION=$BASE_DEST/$YEAR/$MONTH/$DAY/$AR_FILE
echo "Year: $YEAR"
echo "Month: $MONTH"
echo "Day: $DAY"
echo "Time: $TIME"
echo "Configuration File: $CONFIG_FILE"
echo "Base Destination: $BASE_DEST"
echo "AR_FILE: $AR_FILE"
echo "Archive File: $DESTINATION"




Result:
#debug
$ ./read3.sh
Year: 2017
Month: 03
Day: 29
Time: 105105
Configuration File: /homeb/d54712/sandbox/scripts/data.d/Files_To_Backup
Base Destination: /homeb/d54712/sandbox/scripts/data.d
AR_FILE: 105105.tar.gz
Archive File: /homeb/d54712/sandbox/scripts/data.d/2017/03/29/105105.tar.gz



Monday, March 27, 2017

Bash: Scripting: Search and Replace Text


Search and Replace Text


# cat data6.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.


# rep=$(grep -i number data6.txt)

# echo ${rep/number/No.}
This is line No. 1. This is line number 2. This is line number 3. This is line number 4.


This only change the first instance of number.

To change all instances of number change the first / to a double //.


# echo ${rep//number/No.}
This is line No. 1. This is line No. 2. This is line No. 3. This is line No. 4.

Bash: Scripting: Strip way URL elements

Strip way URL elements






#!/bin/bash
#
#
getdomain()
{
url=$1
url_without_proto=${url#*://}
domain_and_port=${url_without_proto%%/*}
domain=${domain_and_port%:*}

echo "$url becomes $url_without_proto"
echo
echo "$url_without_proto becomes $domain_and_port"
echo
echo "$domain_and_port becomes $domain"
getent hosts $domain | head -1
}
for url in $*
do
        getdomain $url
done








Results:
$ ./url1.sh http://kenmsipe.blogspot.com:8080/2017/03/bash-sed-part-xi.html http://joesipe.blogspot.com:8080/2017/03/bash-sed-part-xi.html



http://kenmsipe.blogspot.com:8080/2017/03/bash-sed-part-xi.html becomes kenmsipe.blogspot.com:8080/2017/03/bash-sed-part-xi.html
kenmsipe.blogspot.com:8080/2017/03/bash-sed-part-xi.html becomes kenmsipe.blogspot.com:8080
kenmsipe.blogspot.com:8080 becomes kenmsipe.blogspot.com
2607:f8b0:4000:80b::2001 blogspot.l.googleusercontent.com kenmsipe.blogspot.com
http://joesipe.blogspot.com:8080/2017/03/bash-sed-part-xi.html becomes joesipe.blogspot.com:8080/2017/03/bash-sed-part-xi.html
joesipe.blogspot.com:8080/2017/03/bash-sed-part-xi.html becomes joesipe.blogspot.com:8080
joesipe.blogspot.com:8080 becomes joesipe.blogspot.com
2607:f8b0:4000:80b::2001 blogspot.l.googleusercontent.com joesipe.blogspot.com

Thursday, March 23, 2017

Bash: sed -part XI


read and delete command together


# cat detail.txt


Blum, R         Browncoat
McGuiness, A    Alliance
Bresnahan, C    Browncoat
Harken, C       Alliance




# cat notice.std

Would the following people:
LIST
please report to the ship's captain.






# sed '/LIST/{
> r detail.txt
> }' notice.std


Would the following people:
LIST
Blum, R         Browncoat
McGuiness, A    Alliance
Bresnahan, C    Browncoat
Harken, C       Alliance
please report to the ship's captain.

However, this still leaves the placeholder text(LIST) in the output.

------------------------------
To remove the placeholder text(LIST), just use the delete command(d):


# sed '/LIST/{
> r detail.txt
> d
> }' notice.std




Would the following people:
Blum, R         Browncoat
McGuiness, A    Alliance
Bresnahan, C    Browncoat
Harken, C       Alliance
please report to the ship's captain.

Bash: sed - part X

Reading data from a file




The read command (r) allows you to insert data contained in a separate file.

[address] r filename




# cat Browncoats.txt

Blum, R         Browncoat
Bresnahan, C    Browncoat






# sed '3r Browncoats.txt' data6.txt




This is line number 1.
This is line number 2.
This is line number 3.
Blum, R         Browncoat
Bresnahan, C    Browncoat
This is line number 4.




----------------------------------------




# sed '$r Browncoats.txt' data6.txt



This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
Blum, R         Browncoat
Bresnahan, C    Browncoat

Bash: sed - part IX


Listing lines.

The list command (l) allows you to print both the text and nonprintable characters in a data stream.

# od -c data6a.txt


0000000   T   h   i   s  \t       i   s  \t       l   i   n   e  \t
0000020   n   u   m   b   e   r  \t       1   .  \n   T   h   i   s  \t
0000040       i   s  \t       l   i   n   e  \t       n   u   m   b   e
0000060   r  \t       2   .  \n   T   h   i   s  \t       i   s  \t
0000100   l   i   n   e  \t       n   u   m   b   e   r  \t       3   .
0000120  \n   T   h   i   s  \t       i   s  \t       l   i   n   e  \t
0000140       n   u   m   b   e   r  \t       4   .  \n








# sed -n 'l' data6a.txt

This\t is\t line\t number\t 1.$
This\t is\t line\t number\t 2.$
This\t is\t line\t number\t 3.$
This\t is\t line\t number\t 4.$

Bash: sed - part VIII

Transforming characters

[address]y/inchars/outchars/


The transform command performs a one-to-one mapping of the inchars and the outchars values.


# cat data8.txt


This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
This is line number 1 again.
This is yet another line.
This is the last line in the file.





# sed 'y/123/789/' data8.txt


This is line number 7.
This is line number 8.
This is line number 9.
This is line number 4.
This is line number 7 again.
This is yet another line.
This is the last line in the file.




-----------------------------------------

# echo "This 1 is a test of 1 try." | sed 'y/123/456/'

This 4 is a test of 4 try.

Bash: sed - part VII


Changing lines




You must specify the new line separately from the rest of the sed command.




# sed '3c\
> This is a changed line of text.' data6.txt


This is line number 1.
This is line number 2.
This is a changed line of text.
This is line number 4.


---------------------------------------
# sed '/number 3/c\
> This is a changed line of text.' data6.txt


This is line number 1.
This is line number 2.
This is a changed line of text.
This is line number 4.


---------------------------------------
Instead of changing both lines with text, the sed editor uses the single line of text to replace both lines.


# sed '2,3c\
> This is a new line of test.' data6.txt


This is line number 1.
This is a new line of test.
This is line number 4.

Bash: sed - part VI

Insert and Append text:




Insert (before current line)


# echo "Test Line 2" | sed 'i\Test Line 1'
Test Line 1
Test Line 2


------------------

# sed '3i\
> This is an inserted line.' data6.txt


This is line number 1.
This is line number 2.
This is an inserted line.
This is line number 3.
This is line number 4.

------------------
# sed '1i\
> This is one line of new text.' data6.txt



This is one line of new text.
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.



=========================================================

Append (after current line)

# echo "Test Line 2" | sed 'a\Test Line 1'


Test Line 2
Test Line 1

------------------

# sed '3a\
> This an appended line.' data6.txt



This is line number 1.
This is line number 2.
This is line number 3.
This an appended line.
This is line number 4.


------------------

# sed '$a\
> This is a new line of test.' data6.txt



This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
This is a new line of test.

Bash: sed - part V

Delete a range of lines:




# cat data6.txt


This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.


# sed '/1/,/3/d' data6.txt


This is line number 4.

Bash: sed - part IV


Grouping commands

$ cat sed1

#!/bin/bash
#
sed '2{
s/fox/elephant/
s/dog/cat/
}'  data1a.txt







$ ./sed1The quick brown fox jumps over the lazy dog
The quick brown elephant jumps over the lazy cat
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog

Bash: sed - part III

Using text pattern filters


/pattern/command


For example:


/d54712/s

$ sed -n '/d54712/s/csh/bash/p' /etc/passwd

d54712:x:11385:4000:Sipe; Ken 12Sep13:/homeb/d54712:/bin/bash

bash: sed - part II

The w substitution flag produces the same output but stores the output in the specified file (test.txt).
The normal out of sed editor appears in STDOUT, but only the lines that include the matching pattern are stored in the specified output file.


$ sed 's/test/trial/w test.txt' data5.txt
This is a trial line.
This is a different line


$ cat test.txt
This is a trial line.




===============================================
The sed editor modified the text in line two per address specified:


$ sed '2s/dog/cat/' data1a.txt
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog

===============================================
Here's another example, this time using a range of line addresses:


$ sed '2,3s/dog/cat/' data1a.txt
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy dog

===============================================

If you want to apply a command to a group of lines starting at some point within the text, but continuing to the end of the test. Use the $ sign:


$ sed '2,$s/dog/cat/' data1a.txt
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy cat

Wednesday, March 22, 2017

Bash: sed -part I

Use the -n option to suppress output and the p substitution flag displays any line that has been modified. Use the two together produces output only for lines that have been modified by the substitue command.

# sed -n 's/whaling/tuna/p' data1.txt





a tuna voyage; this the invisible police officer of the Fates, who has the
going on this tuna voyage, formed part of the grand programme of Providence
put me down for this shabby part of a tuna voyage, when others were set
By reason of these things, then, the tuna voyage was welcome; the great

Bash: gawk - part II

# gawk 'BEGIN {print "The data3 File Contents:"}
{print $0}
> END {print "End of File"}' data3.txt

The data3 File Contents:
Line 1
Line 2
Line 3
End of File


===============================================

$ cat script4.gawk
BEGIN {
print "The latest list of users and shells"
print "UserID    \t Shell"
print "------     \t------"
FS=":"
}
{
print $1 "       \t " $7
}
END {
print "This concludes the listing"
}



$$ gawk -f script4.gawk /etc/passwd

The latest list of users and shells
UserID           Shell
------          ------
root             /bin/bash
bin              /sbin/nologin
daemon           /sbin/nologin
adm              /sbin/nologin
lp               /sbin/nologin
sync             /bin/sync
shutdown         /sbin/shutdown
halt             /sbin/halt
mail             /sbin/nologin
news
operator         /sbin/nologin

bash: gawk - part I

$ gawk '{print $1}' data1.txt

little
under
besides
own

===============================================

$ gawk -F : -f ./script2.gawk /etc/passwd


$ cat script2.gawk
{print $1 "'s home directory is " $6}

===============================================


$ gawk -F : -f ./script3.gawk /etc/passwd


$ cat script3.gawk
{text="'s home directory is "
print $1 text  $6
}

Tuesday, March 21, 2017

Bash : Make directory with one command

bash-3.2# mkdir -p {One,Two/{1..3},Three/{a..d},Four,Five/{e..g}}





bash-3.2# find . -ls
2997229    4 drwxr-xr-x   7 root     root         4096 Mar 21 14:42 .
2997230    4 drwxr-xr-x   2 root     root         4096 Mar 21 14:42 ./One
2997231    4 drwxr-xr-x   5 root     root         4096 Mar 21 14:42 ./Two
2997232    4 drwxr-xr-x   2 root     root         4096 Mar 21 14:42 ./Two/1
2997233    4 drwxr-xr-x   2 root     root         4096 Mar 21 14:42 ./Two/2
2997234    4 drwxr-xr-x   2 root     root         4096 Mar 21 14:42 ./Two/3
2997235    4 drwxr-xr-x   6 root     root         4096 Mar 21 14:42 ./Three
2997236    4 drwxr-xr-x   2 root     root         4096 Mar 21 14:42 ./Three/a
2997237    4 drwxr-xr-x   2 root     root         4096 Mar 21 14:42 ./Three/b
2997238    4 drwxr-xr-x   2 root     root         4096 Mar 21 14:42 ./Three/c
2997239    4 drwxr-xr-x   2 root     root         4096 Mar 21 14:42 ./Three/d
2997240    4 drwxr-xr-x   2 root     root         4096 Mar 21 14:42 ./Four
2997241    4 drwxr-xr-x   5 root     root         4096 Mar 21 14:42 ./Five
2997242    4 drwxr-xr-x   2 root     root         4096 Mar 21 14:42 ./Five/e
2997243    4 drwxr-xr-x   2 root     root         4096 Mar 21 14:42 ./Five/f
2997244    4 drwxr-xr-x   2 root     root         4096 Mar 21 14:42 ./Five/g

Saturday, March 18, 2017

Bash Scripting: Delete User Accounts

#!/bin/bash
#
######################################################
# Delete User Accounts
######################################################
#
# Variables
user_file=/home/kensipe/sbin/data.d/user_list
#
# Read User List File
#


exec < $user_file

while IFS=','  read user name
do
        userdel  -r $user
done
#
tail /etc/passwd

Bash Scripting: Add User Accounts

#!/bin/bash
#
######################################################
# Add User Accounts
######################################################
#
# Variables
user_file=/home/kensipe/sbin/data.d/user_list
temp_passwd="$( openssl passwd -crypt )"
#
# Read User List File
#

echo "Temp Password is: $temp_passwd"
echo

exec < $user_file

while IFS=','  read user name
do
        useradd -c "$name" -m -p "$temp_user" $user
        echo "$user $name"
done
#
tail /etc/passwd

#Note:
#####  user_file=/home/kensipe/sbin/data.d/user_list
#
#[kensipe@localhost data.d]$ cat user_list
#jpaul,John Paul
#bfranklin,Ben Franklin
#gwashington,George Washington
#prevere,Paul Revere

Thursday, March 16, 2017

Bash Scripting: Dynamically created Destination Directory with Archiving Script


  1 #!/bin/bash
  2 #
  3 # Back up certain files in t3psolarwebl2:/prd_webroot/docs/i-net_dev
  4 #
  5 # Gather Current Day, Month & Time
  6 #
  7 DAY=$( date +%d )
  8 MONTH=$( date +%m )
  9 TIME=$( date +%H%M )
 10 #
 11 # Set up Configuration File and Destination/Source
 12 #
 13 CONFIG_FILE=/auto/home/homeb/d54712/sandbox/scripts/data.d/backup_i-net_dev
 14 FILE=select_i-net_dev_archive.tar.
 15 BASEDEST=/auto/home/homeb/d54712/sandbox/scripts/data.d/archive/hourly
 16 #
 17 # User Variables
 18 FILE_NO=1 # Start reading file from line 1
 19 #
 20 ###################################################################
 21 # Read Configuration File and cache it
 22 #
 23 exec < $CONFIG_FILE # STDIN is Config File
 24
 25 while read FILE_NAME
 26 do
 27         if [ -f $FILE_NAME -o -d $FILE_NAME ]
 28         then
 29                 echo
 30                 FILE_LIST="$FILE_LIST $FILE_NAME"       # Cache input from Config File
 31         else
 32                 echo "File: $FILE_NAME, does not exist"
 33                 echo "$FILE_NAME is located in configuration file: $CONFIG_FILE on line:    $FILE_NO"
 34         fi
 35
 36         FILE_NO=$[ $FILE_NO + 1 ] # Advance number line count
 37 done
 38
 39 # echo $FILE_LIST
 40 #
 41 # Create Archive Destination Directory
42 #
 43  mkdir -p $BASEDEST/$MONTH/$DAY
 44 #
 45 # Build Archive Destination File Name
 46 #
 47 DESTINATION=$BASEDEST/$MONTH/$DAY/archive.$TIME.tar.gz
 48 #
 49 ##################################################################
 50 # Backup  Directories/Files in Configuration File
 51 #
 52 #
 53
 54 tar -cvzf $DESTINATION $FILE_LIST 2> /dev/null
 55
 56 echo
 57 echo
 58
 59 for (( x=1; x<=10; x++ ))
 60 do
 61         echo -n "$x..."
 62         sleep .25
 63 done
 64 echo
 65 echo
 66 echo "Backup complete: $DESTINATION"
 67 echo
 68 exit




Result:
ls -lR archive
archive:
total 4
drwxr-xr-x 3 d54712 kmuser 4096 Mar 16 14:18 hourly
archive/hourly:
total 4
drwxr-xr-x 3 d54712 kmuser 4096 Mar 16 14:18 03
archive/hourly/03:
total 4
drwxr-xr-x 2 d54712 kmuser 4096 Mar 16 14:30 16
archive/hourly/03/16:
total 11264
-rw-r--r-- 1 d54712 kmuser 5735953 Mar 16 14:23 archive.1423.tar.gz
-rw-r--r-- 1 d54712 kmuser 5735953 Mar 16 14:30 archive.1430.tar.gz



Wednesday, March 15, 2017

Bash Scripting: Archiving Script < config_file

#!/bin/bash
#
# Set up Configuration File and Destination File
#
CONFIG_FILE=/home/kensipe/sbin/data.d/Files_To_Backup
FILE=archive.tar.gz
DESTINATION=/home/kensipe/sbin/data.d/$FILE
#
# Main Script Starts
############################################################
# Read Config File
#
#
FILE_NO=1
exec < $CONFIG_FILE

while read FILE_NAME    #Start reading config file
do
        if [ -f $FILE_NAME -o -d $FILE_NAME ]
        then

                echo
                FILE_LIST="$FILE_LIST $FILE_NAME"
        else
                echo "File does not exist"
                echo "File: $FILE_NAME  missing in  on Line: $FILE_NO"
        fi

        FILE_NO=$[ $FILE_NO + 1 ]
done
#############################################################
# Back up Files
#

echo
echo "Archiving..."

tar -czf $DESTINATION $FILE_LIST 2> /dev/null

echo
echo "Files Archived: $DESTINATION"

Bash Scripting: Daily Archive - Archive designated files & directories

#!/bin/bash
#
# Daily Archive - Archive designated files & directories
########################################################
# Gather Current Date
#
DATE=$( date +%y%m%d )
#
# Set Archive File Name
#
FILE=archive$DATE.tar.gz
#
# Set Configuration and Destination File
#
CONFIG_FILE=/auto/home/homeb/d54712/sandbox/scripts/data.d/Files_To_Backup
DESTINATION=/auto/home/homeb/d54712/sandbox/scripts/data.d/$FILE
#
######## Main Script ###################################
#
if [ -f $CONFIG_FILE ] # Make sure the config file still exits
then    #If it exists, do nothing but continue on
        echo
else    #If it doesn't exist, issue error & exist script
        echo
        echo "$CONFIG_FILE does not exist"
       

 echo "Backup not completed due to missing Configuration File"
        echo
        exit
fi
#
# Build the names of all the files to backup
#
FILE_NO=1 #Start on Line 1 of Config File
exec <$CONFIG_FILE      #Redirect STDIN to name of Config File
#
read FILE_NAME          #Read 1st record
#
while [ $? -eq 0 ]
do
        #Make sure the file or directory exists
        if [ -f $FILE_NAME -o -d $FILE_NAME ]
        then
                #If file exists, add its name to the list
                FILE_LIST="$FILE_LIST $FILE_NAME"
        else
                #If file doesn't exist, issue warning
                echo
                echo "$FILE_NAME, does not exist"
                echo "Obviously, I will not include it in this archive"
                echo "It is listed on $FILE_NO of the config file"
                echo "COntinuing to build archive list..."
        fi
#
        FILE_NO=$[ $FILE_NO + 1] #Increase Line/File number by one
        read FILE_NAME          #Read next record
done
#
#######################################################
#
# Backup the files and Compress Archive
#
echo "Starting archive.."
echo
#
tar -czf $DESTINATION $FILE_LIST 2>/dev/null
#
echo "Archive completed"
echo "Resulting archive file is: $DESTINATION"
echo
#
exit

Tuesday, March 14, 2017

Bash: mkdir to create more complicated trees

From the man page of mkdir
-p, --parents
no error if existing, make parent directories as needed


You can also use this option to create more complicated trees. Like this one.







Do that with this:
mkdir -p /tmp/A/{1,2,B/{1,2}}

Monday, March 13, 2017

Bash Scripting: redirecting input file descriptors

#!/bin/bash
#
#
#
exec 6<&0
exec 0<~/sandbox/scripts/data.d/states1
count=1
while read line
do
        echo "Line #$count: $line"
        count=$[ $count + 1 ]
done
exec 0<&6
read -p "Are you done now?" answer
case "$answer" in
        Y | y)echo "Goodbye" ;;
        N | n)echo "Sorry, this is the end" ;;
esac

Bash Scripting:storing STDOUT, then coming back to it

#!/bin/bash
#
# storing STDOUT, then coming back to it
#
exec 3>&1
exec 1>test14out
echo "This should store in the output file"
echo "along with this line"
exec 1>&3
echo "Now things should be back to normal"

Bash Scripting:using an alternative file descriptor

#!/bin/bash
#
#  using an alternative file descriptor
#
exec 3>test13out
echo "This should display on the monitor"
echo "and this should be stored in the file" >&3
echo "Then this should be back on the monitor"

Bash Scripting: redirecting file input

#!/bin/bash
#
# redirecting file input
#
exec 0<~/sandbox/scripts/data.d/states1
count=1
while read line
do
        echo "Line #$count: $line"
        count=$[ $count + 1 ]
done
echo "File processed"

Bash Scripting: reading data from a file

#!/bin/bash
#
# reading data from a file
#
file=~/sandbox/scripts/data.d/states1
count=1
cat $file | while read line
do
        echo "Line $count: $line"
        count=$[ $count + 1 ]
done
echo "Finished processing file"




[d54712@t3psolarwebl2 bash.d]$ ./test28.sh
Line 1: Alabama
Line 2: Alaska
Line 3: Arizona
Line 4: Arkansas
Line 5: Colorado
Line 6: Connecticut
Line 7: Delaware
Line 8: Florida
Line 9: Georgia
Line 10: New Hampsire
Line 11: New Mexico
Line 12: New York
Finished processing file

Bash Scripting: simple demonstration of the getopts command

#!/bin/bash
#
#    simple demonstration of the getopts command
#
echo
while getopts :ab:c opt
do
        case "$opt" in
                a) echo "Found the -a option" ;;
                b) echo "Found the -b option, with value $OPTARG" ;;
                c) echo "Found the -c option" ;;
                *) echo "Unknown option: $opt" ;;
        esac
done
#




User enters:


./test19.sh -acb testb




Results:

Found the -a option
Found the -c option
Found the -b option, with value testb

Wednesday, March 1, 2017

Bash Scripting: # changing the IFS value

#!/bin/bash
#
# changing the IFS value
#
IFS=$'\n'
#
for entry in $( cat /etc/passwd )
do
        echo "Values in $entry -"
        IFS=:
        for value in $entry
        do
                echo "  $value"
        done
done
#
#



Results:



Values in root:x:0:0:root:/root:/bin/bash -
        root
        x
        0
        0
        root
        /root
        /bin/bash


[...]