#!/bin/bash
#
DIR=/home/kensipe/sbin/data.d/tmp
for file in $DIR/*.A
do
mv "$file" "${file/.A/.B}"
done
Results:
[kensipe@localhost tmp]$ ls -l
total 0
-rw-------. 1 kensipe kensipe 0 Apr 10 16:38 file.5o5P.A
-rw-------. 1 kensipe kensipe 0 Apr 10 16:38 file.aM48.A
-rw-------. 1 kensipe kensipe 0 Apr 10 16:38 file.JouO.A
-rw-------. 1 kensipe kensipe 0 Apr 10 16:38 file.Kt4W.A
-rw-------. 1 kensipe kensipe 0 Apr 10 16:38 file.msAl.A
-rw-------. 1 kensipe kensipe 0 Apr 10 16:38 file.NIAg.A
-rw-------. 1 kensipe kensipe 0 Apr 10 16:38 file.r82a.A
-rw-------. 1 kensipe kensipe 0 Apr 10 16:38 file.rhhO.A
-rw-------. 1 kensipe kensipe 0 Apr 10 16:38 file.roN3.A
-rw-------. 1 kensipe kensipe 0 Apr 10 16:38 file.sGjz.A
-rw-------. 1 kensipe kensipe 0 Apr 10 16:38 file.Wmmv.A
-rw-------. 1 kensipe kensipe 0 Apr 10 16:38 file.ZAlc.A
-rw-------. 1 kensipe kensipe 0 Apr 10 16:38 file.zwBy.A
[kensipe@localhost bash.d]$ ./rename1.sh
[kensipe@localhost bash.d]$
[kensipe@localhost tmp]$ ls -l
total 0
-rw-------. 1 kensipe kensipe 0 Apr 10 16:38 file.5o5P.B
-rw-------. 1 kensipe kensipe 0 Apr 10 16:38 file.aM48.B
-rw-------. 1 kensipe kensipe 0 Apr 10 16:38 file.JouO.B
-rw-------. 1 kensipe kensipe 0 Apr 10 16:38 file.Kt4W.B
-rw-------. 1 kensipe kensipe 0 Apr 10 16:38 file.msAl.B
-rw-------. 1 kensipe kensipe 0 Apr 10 16:38 file.NIAg.B
-rw-------. 1 kensipe kensipe 0 Apr 10 16:38 file.r82a.B
-rw-------. 1 kensipe kensipe 0 Apr 10 16:38 file.rhhO.B
-rw-------. 1 kensipe kensipe 0 Apr 10 16:38 file.roN3.B
-rw-------. 1 kensipe kensipe 0 Apr 10 16:38 file.sGjz.B
-rw-------. 1 kensipe kensipe 0 Apr 10 16:38 file.Wmmv.B
-rw-------. 1 kensipe kensipe 0 Apr 10 16:38 file.ZAlc.B
-rw-------. 1 kensipe kensipe 0 Apr 10 16:38 file.zwBy.B
Monday, April 10, 2017
Bash Scripting - for loop: find files in directory
#!/bin/bash
#
DIR=/home/kensipe/sbin/data.d/tmp
for file in "$DIR/*.A"
do
printf "%s \n" $file
done
Results:
[kensipe@localhost bash.d]$ ./rename.sh
/home/kensipe/sbin/data.d/tmp/file.5o5P.A
/home/kensipe/sbin/data.d/tmp/file.aM48.A
/home/kensipe/sbin/data.d/tmp/file.JouO.A
/home/kensipe/sbin/data.d/tmp/file.Kt4W.A
/home/kensipe/sbin/data.d/tmp/file.msAl.A
/home/kensipe/sbin/data.d/tmp/file.NIAg.A
/home/kensipe/sbin/data.d/tmp/file.r82a.A
/home/kensipe/sbin/data.d/tmp/file.rhhO.A
/home/kensipe/sbin/data.d/tmp/file.roN3.A
/home/kensipe/sbin/data.d/tmp/file.sGjz.A
/home/kensipe/sbin/data.d/tmp/file.Wmmv.A
/home/kensipe/sbin/data.d/tmp/file.ZAlc.A
/home/kensipe/sbin/data.d/tmp/file.zwBy.A
#
DIR=/home/kensipe/sbin/data.d/tmp
for file in "$DIR/*.A"
do
printf "%s \n" $file
done
Results:
[kensipe@localhost bash.d]$ ./rename.sh
/home/kensipe/sbin/data.d/tmp/file.5o5P.A
/home/kensipe/sbin/data.d/tmp/file.aM48.A
/home/kensipe/sbin/data.d/tmp/file.JouO.A
/home/kensipe/sbin/data.d/tmp/file.Kt4W.A
/home/kensipe/sbin/data.d/tmp/file.msAl.A
/home/kensipe/sbin/data.d/tmp/file.NIAg.A
/home/kensipe/sbin/data.d/tmp/file.r82a.A
/home/kensipe/sbin/data.d/tmp/file.rhhO.A
/home/kensipe/sbin/data.d/tmp/file.roN3.A
/home/kensipe/sbin/data.d/tmp/file.sGjz.A
/home/kensipe/sbin/data.d/tmp/file.Wmmv.A
/home/kensipe/sbin/data.d/tmp/file.ZAlc.A
/home/kensipe/sbin/data.d/tmp/file.zwBy.A
Bash - Scripting - part 2- replace part of string
1 #!/bin/bash
2 #
3 # Change file extensions
4 #
5 FILES=/homeb/d54712/sandbox/scripts/data.d/tmp/*
6
7 # ${file/%.pcx/.jpg}
8
9 CONVERT="$(ls $FILES)"
10 TEMP=$(mktemp)
11 echo "$CONVERT" > $TEMP
12
13
14 while read file
15 do
16 echo ${file/%.pcx/.jpg}
17 done < $TEMP
18
19
Results:
# ls -l
total 0
-rw-r--r-- 1 root root 0 Apr 10 13:57 file.135724.pcx
-rw-r--r-- 1 root root 0 Apr 10 13:57 file.135731.pcx
-rw-r--r-- 1 root root 0 Apr 10 13:57 file.135732.pcx
-rw-r--r-- 1 root root 0 Apr 10 13:57 file.135733.pcx
-rw-r--r-- 1 root root 0 Apr 10 13:57 file.135734.pcx
-rw-r--r-- 1 root root 0 Apr 10 13:57 file.135735.pcx
-rw-r--r-- 1 root root 0 Apr 10 13:57 file.135736.pcx
-rw-r--r-- 1 root root 0 Apr 10 13:57 file.135737.pcx
-rw-r--r-- 1 root root 0 Apr 10 13:57 file.135739.pcx
-rw-r--r-- 1 root root 0 Apr 10 13:57 file.135752.pcx
-rw-r--r-- 1 root root 0 Apr 10 13:57 file.135753.pcx
-rw-r--r-- 1 root root 0 Apr 10 13:57 file.135754.pcx
-rw-r--r-- 1 root root 0 Apr 10 13:57 file.135755.pcx
-rw-r--r-- 1 root root 0 Apr 10 13:57 file.135756.pcx
# Convert extensions in temp file ($TEMP)
$ ./file_convert1.sh
/homeb/d54712/sandbox/scripts/data.d/tmp/file.135724.jpg
/homeb/d54712/sandbox/scripts/data.d/tmp/file.135731.jpg
/homeb/d54712/sandbox/scripts/data.d/tmp/file.135732.jpg
/homeb/d54712/sandbox/scripts/data.d/tmp/file.135733.jpg
/homeb/d54712/sandbox/scripts/data.d/tmp/file.135734.jpg
/homeb/d54712/sandbox/scripts/data.d/tmp/file.135735.jpg
/homeb/d54712/sandbox/scripts/data.d/tmp/file.135736.jpg
/homeb/d54712/sandbox/scripts/data.d/tmp/file.135737.jpg
/homeb/d54712/sandbox/scripts/data.d/tmp/file.135739.jpg
/homeb/d54712/sandbox/scripts/data.d/tmp/file.135752.jpg
/homeb/d54712/sandbox/scripts/data.d/tmp/file.135753.jpg
/homeb/d54712/sandbox/scripts/data.d/tmp/file.135754.jpg
/homeb/d54712/sandbox/scripts/data.d/tmp/file.135755.jpg
/homeb/d54712/sandbox/scripts/data.d/tmp/file.135756.jpg
Bash - Scripting - part 1- replace part of string
#!/bin/bash
#
# Change file extensions
#
FILES=/homeb/d54712/sandbox/scripts/data.d/files.txt
exec 3< $FILES
exec <&3
while read file
do
echo "${file/%.pcx/.jpg}"
done
Results:
# cat files.txt
132014.pcx
132023.pcx
132024.pcx
132025.xml
132026.pcx
132026.pcx
132027.xpm
132027.pcx
132027.xml
132028.pdf
132028.pcx
132029.pcx
132029.pcx
132029.pdf
132030.pcx
132030.pcx
132030.pcx
132030.pcx
132031.gif
132031.pcx
132031.pcx
# Convert .pcx to .jpg
./file_convert.sh
132014.jpg
132023.jpg
132024.jpg
132025.xml
132026.jpg
132026.jpg
132027.xpm
132027.jpg
132027.xml
132028.pdf
132028.jpg
132029.jpg
132029.jpg
132029.pdf
132030.jpg
132030.jpg
132030.jpg
132030.jpg
132031.gif
132031.jpg
132031.jpg
#
# Change file extensions
#
FILES=/homeb/d54712/sandbox/scripts/data.d/files.txt
exec 3< $FILES
exec <&3
while read file
do
echo "${file/%.pcx/.jpg}"
done
Results:
# cat files.txt
132014.pcx
132023.pcx
132024.pcx
132025.xml
132026.pcx
132026.pcx
132027.xpm
132027.pcx
132027.xml
132028.pdf
132028.pcx
132029.pcx
132029.pcx
132029.pdf
132030.pcx
132030.pcx
132030.pcx
132030.pcx
132031.gif
132031.pcx
132031.pcx
# Convert .pcx to .jpg
./file_convert.sh
132014.jpg
132023.jpg
132024.jpg
132025.xml
132026.jpg
132026.jpg
132027.xpm
132027.jpg
132027.xml
132028.pdf
132028.jpg
132029.jpg
132029.jpg
132029.pdf
132030.jpg
132030.jpg
132030.jpg
132030.jpg
132031.gif
132031.jpg
132031.jpg
Bash - Scripting - part 1- sort highest with defaults
#!/bin/bash
#
# highest filename [howmany]
# Print howmany highest-numbered lines in file filename
# The input file is assumed to have lines that sart with numbers
#+Default for howmany is 10.
filename=$1
howmany=${2:-10}
sort -nr $filename | head -$howmany
Results:
# howmany defaults to 10
$ ./highest.sh albumns.txt
22 U2
12 Coldplay
11 Aerosmith
9 Kodaline
9 Jackson Browne
8 21 Pilots
7 Michael Jackson
6 Pitbull
6 Elton John
5 Thirty Seconds to Mars
# howmany ($2) is 5
$ ./highest.sh /auto/home/homeb/d54712/sandbox/scripts/data.d/albumns.txt 5
22 U2
12 Coldplay
11 Aerosmith
9 Kodaline
9 Jackson Browne
#
# highest filename [howmany]
# Print howmany highest-numbered lines in file filename
# The input file is assumed to have lines that sart with numbers
#+Default for howmany is 10.
filename=$1
howmany=${2:-10}
sort -nr $filename | head -$howmany
Results:
# howmany defaults to 10
$ ./highest.sh albumns.txt
22 U2
12 Coldplay
11 Aerosmith
9 Kodaline
9 Jackson Browne
8 21 Pilots
7 Michael Jackson
6 Pitbull
6 Elton John
5 Thirty Seconds to Mars
# howmany ($2) is 5
$ ./highest.sh /auto/home/homeb/d54712/sandbox/scripts/data.d/albumns.txt 5
22 U2
12 Coldplay
11 Aerosmith
9 Kodaline
9 Jackson Browne
Friday, April 7, 2017
Bash: Scripting - part 5- while getopts abcd opt
#!/bin/bash
#
while getopts abcdh opt
do
case "$opt" in
a)
DIR=/etc/
echo "Option: a"
echo "Break out of case structure: option a"
sleep 1
echo "..." ;;
b)
DIR=/var/
echo "Option: b"
echo "Break out of case structure: option b"
sleep 1
echo "..." ;;
c)
DIR=/proc
echo "Option: c"
echo "Break out of case structure: option c"
sleep 1
echo "..." ;;
d)
DIR=~/
echo "Option: d"
echo "Break out of case structure: option d"
sleep 1
echo "..." ;;
h)
echo "-a: List /etc/"
echo "-b: List /var/"
echo "-c: List /proc/"
echo "-d: List Current User's Home Directory"
exit 0 ;;
*)
echo "Select valid option"
echo
echo "For menu, select option "h""
exit 0 ;;
esac
done
echo "Script continues to end"
echo
echo "Option: ${@:-"No option"} selected"
if [ ! -e "$@" ]
then
echo
cd $DIR
echo "Directory: $PWD"
sleep 3
ls -l | less
else
echo
echo "Please include an option"
fi
Results:
$ ./getopts11c.sh
Script continues to end
Option: No option selected
Please include an option
#
while getopts abcdh opt
do
case "$opt" in
a)
DIR=/etc/
echo "Option: a"
echo "Break out of case structure: option a"
sleep 1
echo "..." ;;
b)
DIR=/var/
echo "Option: b"
echo "Break out of case structure: option b"
sleep 1
echo "..." ;;
c)
DIR=/proc
echo "Option: c"
echo "Break out of case structure: option c"
sleep 1
echo "..." ;;
d)
DIR=~/
echo "Option: d"
echo "Break out of case structure: option d"
sleep 1
echo "..." ;;
h)
echo "-a: List /etc/"
echo "-b: List /var/"
echo "-c: List /proc/"
echo "-d: List Current User's Home Directory"
exit 0 ;;
*)
echo "Select valid option"
echo
echo "For menu, select option "h""
exit 0 ;;
esac
done
echo "Script continues to end"
echo
echo "Option: ${@:-"No option"} selected"
if [ ! -e "$@" ]
then
echo
cd $DIR
echo "Directory: $PWD"
sleep 3
ls -l | less
else
echo
echo "Please include an option"
fi
Results:
$ ./getopts11c.sh
Script continues to end
Option: No option selected
Please include an option
Bash: Scripting - part 4- while getopts abcd opt
#!/bin/bash
#
while getopts abcdh opt
do
case "$opt" in
a)
DIR=/etc/
echo "Option: a"
echo "Break out of case structure: option a"
sleep 1
echo "..." ;;
b)
DIR=/var/
echo "Option: b"
echo "Break out of case structure: option b"
sleep 1
echo "..." ;;
c)
DIR=/proc
echo "Option: c"
echo "Break out of case structure: option c"
sleep 1
echo "..." ;;
d)
DIR=~/
echo "Option: d"
echo "Break out of case structure: option d"
sleep 1
echo "..." ;;
h)
echo "-a: List /etc/"
echo "-b: List /var/"
echo "-c: List /proc/"
echo "-d: List Current User's Home Directory"
exit 0 ;;
*)
echo "Select valid option"
echo
echo "For menu, select option "h""
exit 0 ;;
esac
done
echo "Script continues to end"
echo
echo "Option $@ selected"
echo
cd $DIR
echo "Directory: $PWD"
sleep 3
ls -l | less
Results:
$ ./getopts11b.sh -h
-a: List /etc/
-b: List /var/
-c: List /proc/
-d: List Current User's Home Directory
$ ./getopts11b.sh -w
./getopts11b.sh: illegal option -- w
Select valid option
For menu, select option h
#
while getopts abcdh opt
do
case "$opt" in
a)
DIR=/etc/
echo "Option: a"
echo "Break out of case structure: option a"
sleep 1
echo "..." ;;
b)
DIR=/var/
echo "Option: b"
echo "Break out of case structure: option b"
sleep 1
echo "..." ;;
c)
DIR=/proc
echo "Option: c"
echo "Break out of case structure: option c"
sleep 1
echo "..." ;;
d)
DIR=~/
echo "Option: d"
echo "Break out of case structure: option d"
sleep 1
echo "..." ;;
h)
echo "-a: List /etc/"
echo "-b: List /var/"
echo "-c: List /proc/"
echo "-d: List Current User's Home Directory"
exit 0 ;;
*)
echo "Select valid option"
echo
echo "For menu, select option "h""
exit 0 ;;
esac
done
echo "Script continues to end"
echo
echo "Option $@ selected"
echo
cd $DIR
echo "Directory: $PWD"
sleep 3
ls -l | less
Results:
$ ./getopts11b.sh -h
-a: List /etc/
-b: List /var/
-c: List /proc/
-d: List Current User's Home Directory
$ ./getopts11b.sh -w
./getopts11b.sh: illegal option -- w
Select valid option
For menu, select option h
Bash: Scripting - part 3- while getopts abcd opt
#!/bin/bash
#
while getopts abcd opt
do
case "$opt" in
a)
DIR=/etc/
echo "Option: a"
echo "Break out of case structure: option a"
sleep 1
echo "..." ;;
b)
DIR=/var/
echo "Option: b"
echo "Break out of case structure: option b"
sleep 1
echo "..." ;;
c)
DIR=/proc
echo "Option: c"
echo "Break out of case structure: option c"
sleep 1
echo "..." ;;
d)
DIR=~/
echo "Option: d"
echo "Break out of case structure: option d"
sleep 1
echo "..." ;;
esac
done
echo "Script continues to end"
echo
echo "Option $@ selected"
echo
cd $DIR
echo "Directory: $PWD"
sleep 3
ls -l | less
Results:
$ ./getopts11b.sh -d
Option: d
Break out of case structure: option d
...
Script continues to end
Option -d selected
Directory: /homeb/d54712
total 2220
drwxr-xr-x 7 itadmin kmuser 8192 Oct 10 10:17 archive
-rwxrwxr-x 1 d54712 kmuser 1379 Jun 24 2016 assignees.txt
-rw-r--r-- 1 root root 476397 Sep 15 2016 back_ks.tar.gz
drwxrwxr-x 2 itadmin t3pelog_admins 4096 Feb 10 2016 bin
-rwxr-x--- 1 d54712 kmuser 5377 Oct 31 08:53 category.cgi
-rwxrwxr-x 1 solarweb solarweb 817521 Oct 29 2015 coa.txt
-rw------- 1 d54712 kmuser 347 Nov 18 11:29 dead.letter
-rwxr-xr-x 1 itadmin kmuser 8481 Sep 30 2016 eSolarFTP.pl
drwxr-xr-x 2 root root 4096 Dec 14 08:25 INC000011135541
drwxr-x--- 2 d54712 kmuser 4096 Nov 22 08:39 inet-form
drwxr-xr-x 2 root root 4096 Aug 24 2016 ks_test
-rwxr-x--- 1 solarweb solarweb 1040 Dec 1 15:59 layout.css
drwxrwxr-x 2 itadmin t3pelog_admins 4096 Mar 13 22:00 log
-rw-r--r-- 1 itadmin kmuser 458413 Feb 9 03:12 manuals.txt
-rw-r--r-- 1 d54712 kmuser 0 Jan 31 09:53 manuals.txt_on_lewis_u r5.txt
-rw------- 1 root root 2198 Nov 18 16:09 mbox
....
#
while getopts abcd opt
do
case "$opt" in
a)
DIR=/etc/
echo "Option: a"
echo "Break out of case structure: option a"
sleep 1
echo "..." ;;
b)
DIR=/var/
echo "Option: b"
echo "Break out of case structure: option b"
sleep 1
echo "..." ;;
c)
DIR=/proc
echo "Option: c"
echo "Break out of case structure: option c"
sleep 1
echo "..." ;;
d)
DIR=~/
echo "Option: d"
echo "Break out of case structure: option d"
sleep 1
echo "..." ;;
esac
done
echo "Script continues to end"
echo
echo "Option $@ selected"
echo
cd $DIR
echo "Directory: $PWD"
sleep 3
ls -l | less
Results:
$ ./getopts11b.sh -d
Option: d
Break out of case structure: option d
...
Script continues to end
Option -d selected
Directory: /homeb/d54712
total 2220
drwxr-xr-x 7 itadmin kmuser 8192 Oct 10 10:17 archive
-rwxrwxr-x 1 d54712 kmuser 1379 Jun 24 2016 assignees.txt
-rw-r--r-- 1 root root 476397 Sep 15 2016 back_ks.tar.gz
drwxrwxr-x 2 itadmin t3pelog_admins 4096 Feb 10 2016 bin
-rwxr-x--- 1 d54712 kmuser 5377 Oct 31 08:53 category.cgi
-rwxrwxr-x 1 solarweb solarweb 817521 Oct 29 2015 coa.txt
-rw------- 1 d54712 kmuser 347 Nov 18 11:29 dead.letter
-rwxr-xr-x 1 itadmin kmuser 8481 Sep 30 2016 eSolarFTP.pl
drwxr-xr-x 2 root root 4096 Dec 14 08:25 INC000011135541
drwxr-x--- 2 d54712 kmuser 4096 Nov 22 08:39 inet-form
drwxr-xr-x 2 root root 4096 Aug 24 2016 ks_test
-rwxr-x--- 1 solarweb solarweb 1040 Dec 1 15:59 layout.css
drwxrwxr-x 2 itadmin t3pelog_admins 4096 Mar 13 22:00 log
-rw-r--r-- 1 itadmin kmuser 458413 Feb 9 03:12 manuals.txt
-rw-r--r-- 1 d54712 kmuser 0 Jan 31 09:53 manuals.txt_on_lewis_u r5.txt
-rw------- 1 root root 2198 Nov 18 16:09 mbox
....
Bash: Scripting - part 2- while getopts abcd opt
#!/bin/bash
#
while getopts abcd opt
do
case "$opt" in
a)
echo "Option: a"
echo "Break out of case structure: option a"
sleep 1
echo "..." ;;
b)
echo "Option: b"
echo "Break out of case structure: option b"
sleep 1
echo "..." ;;
c)
echo "Option: c"
echo "Break out of case structure: option c"
sleep 1
echo "..." ;;
d)
echo "Option: d"
echo "Break out of case structure: option d"
sleep 1
echo "..." ;;
esac
done
echo "Script continues to end"
echo "Option $@ selected"
Results:
$ ./getopts11a.sh -c
Option: c
Break out of case structure: option c
...
Script continues to end
Option -c selected
$ ./getopts11a.sh -ad
Option: a
Break out of case structure: option a
...
Option: d
Break out of case structure: option d
...
Script continues to end
Option -ad selected
#
while getopts abcd opt
do
case "$opt" in
a)
echo "Option: a"
echo "Break out of case structure: option a"
sleep 1
echo "..." ;;
b)
echo "Option: b"
echo "Break out of case structure: option b"
sleep 1
echo "..." ;;
c)
echo "Option: c"
echo "Break out of case structure: option c"
sleep 1
echo "..." ;;
d)
echo "Option: d"
echo "Break out of case structure: option d"
sleep 1
echo "..." ;;
esac
done
echo "Script continues to end"
echo "Option $@ selected"
Results:
$ ./getopts11a.sh -c
Option: c
Break out of case structure: option c
...
Script continues to end
Option -c selected
$ ./getopts11a.sh -ad
Option: a
Break out of case structure: option a
...
Option: d
Break out of case structure: option d
...
Script continues to end
Option -ad selected
Bash: Scripting - part 1- while getopts abcd opt
#!/bin/bash
#
while getopts abcd opt
do
case "$opt" in
a)
echo "Option: a" ;;
b)
echo "Option: b" ;;
c)
echo "Option: c" ;;
d)
echo "Option: d" ;;
esac
done
Results:
$ ./getopts11.sh -b
Option: b
#
while getopts abcd opt
do
case "$opt" in
a)
echo "Option: a" ;;
b)
echo "Option: b" ;;
c)
echo "Option: c" ;;
d)
echo "Option: d" ;;
esac
done
Results:
$ ./getopts11.sh -b
Option: b
Wednesday, April 5, 2017
Bash: Scripting - Lists symbolic links in directory
#!/bin/bash
# symlinks.sh: Lists symbolic links in directory.
directory=${1-$(pwd)}
# Defaults to current directory,
#+ if not otherwise specified.
# Equivalent to code block below
#---------------------------------------------------------
# ARGS=1 # Expect one command-line argument
#
# if [ $# -ne "$ARGS" ] # If not 1 arg...
# then
# directory=$(pwd) # current working directory
# else
# directory=$1
#fi
#---------------------------------------------------------
echo "Symbolic links in directory \"$directory\""
for file in "$(find $directory -maxdepth 1 -mindepth 1 -type l)" # -type l= symbolic links
do
echo "$file"
done | sort
# Strictly speaking, a loop isn't really neccessary here,
#+ since the output of the "find" command is expanded into a single word.
# Howerver, it's easy to understand and illustrative this way.
# Failing to quote $(find $directory -type l)
#+ will choke on filenames with embedded whitespace.
exit 0
Results:
$ ./symlinks.sh /prd_webroot/docs
Symbolic links in directory "/prd_webroot/docs"
/prd_webroot/docs/catalog_server.pl
/prd_webroot/docs/Corp
/prd_webroot/docs/Dept
/prd_webroot/docs/Images
# symlinks.sh: Lists symbolic links in directory.
directory=${1-$(pwd)}
# Defaults to current directory,
#+ if not otherwise specified.
# Equivalent to code block below
#---------------------------------------------------------
# ARGS=1 # Expect one command-line argument
#
# if [ $# -ne "$ARGS" ] # If not 1 arg...
# then
# directory=$(pwd) # current working directory
# else
# directory=$1
#fi
#---------------------------------------------------------
echo "Symbolic links in directory \"$directory\""
for file in "$(find $directory -maxdepth 1 -mindepth 1 -type l)" # -type l= symbolic links
do
echo "$file"
done | sort
# Strictly speaking, a loop isn't really neccessary here,
#+ since the output of the "find" command is expanded into a single word.
# Howerver, it's easy to understand and illustrative this way.
# Failing to quote $(find $directory -type l)
#+ will choke on filenames with embedded whitespace.
exit 0
Results:
$ ./symlinks.sh /prd_webroot/docs
Symbolic links in directory "/prd_webroot/docs"
/prd_webroot/docs/catalog_server.pl
/prd_webroot/docs/Corp
/prd_webroot/docs/Dept
/prd_webroot/docs/Images
Bash: Scripting
#!/bin/bash
#
#
generate_list ()
{
echo "one two three"
}
#
#
for word in $(generate_list)
do
echo $word
done
exit $?
Result:
$ ./gen_list.sh
one
two
three
#
#
generate_list ()
{
echo "one two three"
}
#
#
for word in $(generate_list)
do
echo $word
done
exit $?
Result:
$ ./gen_list.sh
one
two
three
Bash: Scripting - Userlist - awk
1 #!/bin/bash
2 #
3 # userlist.sh
4 #
5 PASSWORD_FILE=/etc/passwd
6 n=1 #User number
7
8 for name in $(awk 'BEGIN{FS=":"} {print $1}' < "$PASSWORD_FILE")
9 # Field Separator = :
10 # Print first name
11 # Get input from password file /etc/passwd
12 do
13 echo "USER #$n = $name"
14 n=$[ $n +1 ]
15 done
16
17 exit $?
Results:
$ ./userlist.sh|less
USER #1 = root
USER #2 = bin
USER #3 = daemon
USER #4 = adm
USER #5 = lp
USER #6 = sync
USER #7 = shutdown
...
USER #20 = pcap
...
2 #
3 # userlist.sh
4 #
5 PASSWORD_FILE=/etc/passwd
6 n=1 #User number
7
8 for name in $(awk 'BEGIN{FS=":"} {print $1}' < "$PASSWORD_FILE")
9 # Field Separator = :
10 # Print first name
11 # Get input from password file /etc/passwd
12 do
13 echo "USER #$n = $name"
14 n=$[ $n +1 ]
15 done
16
17 exit $?
Results:
$ ./userlist.sh|less
USER #1 = root
USER #2 = bin
USER #3 = daemon
USER #4 = adm
USER #5 = lp
USER #6 = sync
USER #7 = shutdown
...
USER #20 = pcap
...
Tuesday, April 4, 2017
Bash: Scripting - mktemp and tail
#!/bin/bash
#
# temp_tail.sh
# View states file
# Write results to temp file ($temp)
# $(mktemp) Creates temp file in /tmp
# tr "" "\n" translate spaces into newlines
# tail -3: view the bottom 3 lines
#
STATES=/auto/home/homeb/d54712/sandbox/scripts/data.d/states
temp=$(mktemp)
$((cat $STATES)> $temp |tr " " "\n")
tail -3 $temp
Results:
$ cat states
Alabama
Alaska
Arizona
Arkansas
Colorado
Connecticut
Delaware
Florida
Georgia
Results:
$ ./temp_tail.sh
Delaware
Florida
Georgia
#
# temp_tail.sh
# View states file
# Write results to temp file ($temp)
# $(mktemp) Creates temp file in /tmp
# tr "" "\n" translate spaces into newlines
# tail -3: view the bottom 3 lines
#
STATES=/auto/home/homeb/d54712/sandbox/scripts/data.d/states
temp=$(mktemp)
$((cat $STATES)> $temp |tr " " "\n")
tail -3 $temp
Results:
$ cat states
Alabama
Alaska
Arizona
Arkansas
Colorado
Connecticut
Delaware
Florida
Georgia
Results:
$ ./temp_tail.sh
Delaware
Florida
Georgia
Monday, April 3, 2017
Bash: Scripting - printf - advanced
1 #!/bin/bash
2 #
3 #
4 divider======================================
5 divider=$divider$divider
6
7 header="\n %10s %8s %10s %11s \n"
8 format=" %-10s %08d %10s %11.2f \n"
9 width=43
10
11
12 printf "$header" "ITEM NAME" "ITEM ID" "COLOR" "PRICE"
13 printf "%$width.${width}s\n" "$divider"
14 printf "$format" \
15 Triangle 13 red 20 \
16 Oval 204449 "dark blue" 65.656 \
17 Square 3145 orange .7
Results:
# ./printf.sh
ITEM NAME ITEM ID COLOR PRICE
===========================================
Triangle 00000013 red 20.00
Oval 00204449 dark blue 65.66
Square 00003145 orange 0.70
2 #
3 #
4 divider======================================
5 divider=$divider$divider
6
7 header="\n %10s %8s %10s %11s \n"
8 format=" %-10s %08d %10s %11.2f \n"
9 width=43
10
11
12 printf "$header" "ITEM NAME" "ITEM ID" "COLOR" "PRICE"
13 printf "%$width.${width}s\n" "$divider"
14 printf "$format" \
15 Triangle 13 red 20 \
16 Oval 204449 "dark blue" 65.656 \
17 Square 3145 orange .7
Results:
# ./printf.sh
ITEM NAME ITEM ID COLOR PRICE
===========================================
Triangle 00000013 red 20.00
Oval 00204449 dark blue 65.66
Square 00003145 orange 0.70
Bash: Scripting - part 4 - File Info ( Find files with the word "backup" in them (grep))
1 #!/bin/bash
2 #
3 # fileinfo.sh
4 # Find files with the word "backup" in them
5 #
6 MYFILES="/auto/home/homeb/d54712/sandbox/scripts/bash.d/*"
7 #
8 echo
9
10 for file in $MYFILES
11 do
12 if [ -f "$file" ] && [ "$(grep -i "backup" $file)" ]
13 then
14
15 ls -l $file |awk '{ print "Filename: " $9 "\n" "Timestamp: " $8"\n" "File size: " $5"\n" }' # print 3 field s
16
17 echo
18 fi
19
20
21
22 done
23
24 exit 0
Results:
$ ./fileinfo2.sh
Filename: /auto/home/homeb/d54712/sandbox/scripts/bash.d/backup_t3psolarwebl2.inet_dev.sh
Timestamp: 10:23
File size: 1197
Filename: /auto/home/homeb/d54712/sandbox/scripts/bash.d/Daily_Archive.sh
Timestamp: 14:16
File size: 1756
Filename: /auto/home/homeb/d54712/sandbox/scripts/bash.d/fileinfo2.sh
Timestamp: 12:47
File size: 328
Filename: /auto/home/homeb/d54712/sandbox/scripts/bash.d/hourly_backup_t3psolarwebl2.inet_dev.sh
Timestamp: 14:23
File size: 1473
Filename: /auto/home/homeb/d54712/sandbox/scripts/bash.d/read2.sh
Timestamp: 10:11
File size: 628
Filename: /auto/home/homeb/d54712/sandbox/scripts/bash.d/read3.sh
Timestamp: 12:20
File size: 1152
Filename: /auto/home/homeb/d54712/sandbox/scripts/bash.d/read4.sh
Timestamp: 11:34
File size: 1334
Filename: /auto/home/homeb/d54712/sandbox/scripts/bash.d/read_file_create_variable1.sh
Timestamp: 14:46
File size: 1214
2 #
3 # fileinfo.sh
4 # Find files with the word "backup" in them
5 #
6 MYFILES="/auto/home/homeb/d54712/sandbox/scripts/bash.d/*"
7 #
8 echo
9
10 for file in $MYFILES
11 do
12 if [ -f "$file" ] && [ "$(grep -i "backup" $file)" ]
13 then
14
15 ls -l $file |awk '{ print "Filename: " $9 "\n" "Timestamp: " $8"\n" "File size: " $5"\n" }' # print 3 field s
16
17 echo
18 fi
19
20
21
22 done
23
24 exit 0
Results:
$ ./fileinfo2.sh
Filename: /auto/home/homeb/d54712/sandbox/scripts/bash.d/backup_t3psolarwebl2.inet_dev.sh
Timestamp: 10:23
File size: 1197
Filename: /auto/home/homeb/d54712/sandbox/scripts/bash.d/Daily_Archive.sh
Timestamp: 14:16
File size: 1756
Filename: /auto/home/homeb/d54712/sandbox/scripts/bash.d/fileinfo2.sh
Timestamp: 12:47
File size: 328
Filename: /auto/home/homeb/d54712/sandbox/scripts/bash.d/hourly_backup_t3psolarwebl2.inet_dev.sh
Timestamp: 14:23
File size: 1473
Filename: /auto/home/homeb/d54712/sandbox/scripts/bash.d/read2.sh
Timestamp: 10:11
File size: 628
Filename: /auto/home/homeb/d54712/sandbox/scripts/bash.d/read3.sh
Timestamp: 12:20
File size: 1152
Filename: /auto/home/homeb/d54712/sandbox/scripts/bash.d/read4.sh
Timestamp: 11:34
File size: 1334
Filename: /auto/home/homeb/d54712/sandbox/scripts/bash.d/read_file_create_variable1.sh
Timestamp: 14:46
File size: 1214
Bash: Scripting - part 3 - File Info (All files in directory - * wildcard)
#!/bin/bash
#
# fileinfo.sh
MYFILES="/auto/home/homeb/d54712/sandbox/scripts/bash.d/*" #(All files in directory - * wildcard)
#
echo
for file in $MYFILES
do
if [ -f "$file" ]
then
ls -l $file |awk '{ print "Filename: " $9 "\n" "Timestamp: " $8"\n" "File size: " $5"\n" }' # print 3 fields
echo
fi
done
exit 0
Results:
$ ./fileinfo1.sh |less
Filename: /auto/home/homeb/d54712/sandbox/scripts/bash.d/addem
Timestamp: 14:43
File size: 227
Filename: /auto/home/homeb/d54712/sandbox/scripts/bash.d/advM1.sh
Timestamp: 14:44
File size: 119
Filename: /auto/home/homeb/d54712/sandbox/scripts/bash.d/advS1.sh
Timestamp: 14:50
File size: 107
Filename: /auto/home/homeb/d54712/sandbox/scripts/bash.d/backup_t3psolarwebl2.inet_dev.sh
Timestamp: 10:23
File size: 1197
Filename: /auto/home/homeb/d54712/sandbox/scripts/bash.d/badtest1.sh
Timestamp: 13:18
File size: 221
Filename: /auto/home/homeb/d54712/sandbox/scripts/bash.d/badtest2.sh
Timestamp: 13:44
File size: 260
Filename: /auto/home/homeb/d54712/sandbox/scripts/bash.d/badtest3.sh
Timestamp: 13:59
File size: 244
Filename: /auto/home/homeb/d54712/sandbox/scripts/bash.d/break1.sh
Timestamp: 11:38
File size: 196
Bash: Scripting - part 2 - File Info
#!/bin/bash
#
# fileinfo.sh
MYFILES="/auto/home/homeb/d54712/sandbox/scripts/bash.d/read1.sh
/auto/home/homeb/d54712/sandbox/scripts/data.d/states
/auto/home/homeb/d54712/sandbox/scripts/data.d/states12345" # List of files you are curious about
#Threw in in a dummy file, /auto/home/homeb/d54712/sandbox/scripts/bash.d/states12345
#
echo
for file in $MYFILES
do
if [ ! -e "$file" ]
then
echo "$file does not exist."
continue
fi
ls -l $file |awk '{ print "Filename: " $9 "\n" "Timestamp: " $8"\n" "File size: " $5"\n" }' # print 3 fields
echo
done
exit 0
Results:
$ ./fileinfo.sh
Filename: /auto/home/homeb/d54712/sandbox/scripts/bash.d/read1.sh
Timestamp: 14:23
File size: 126
Filename: /auto/home/homeb/d54712/sandbox/scripts/data.d/states
Timestamp: 15:30
File size: 78
/auto/home/homeb/d54712/sandbox/scripts/data.d/states12345 does not exist.
#
# fileinfo.sh
MYFILES="/auto/home/homeb/d54712/sandbox/scripts/bash.d/read1.sh
/auto/home/homeb/d54712/sandbox/scripts/data.d/states
/auto/home/homeb/d54712/sandbox/scripts/data.d/states12345" # List of files you are curious about
#Threw in in a dummy file, /auto/home/homeb/d54712/sandbox/scripts/bash.d/states12345
#
echo
for file in $MYFILES
do
if [ ! -e "$file" ]
then
echo "$file does not exist."
continue
fi
ls -l $file |awk '{ print "Filename: " $9 "\n" "Timestamp: " $8"\n" "File size: " $5"\n" }' # print 3 fields
echo
done
exit 0
Results:
$ ./fileinfo.sh
Filename: /auto/home/homeb/d54712/sandbox/scripts/bash.d/read1.sh
Timestamp: 14:23
File size: 126
Filename: /auto/home/homeb/d54712/sandbox/scripts/data.d/states
Timestamp: 15:30
File size: 78
/auto/home/homeb/d54712/sandbox/scripts/data.d/states12345 does not exist.
Bash: Scripting - part 1 - File Info
#!/bin/bash
#
# fileinfo.sh
MYFILES="/auto/home/homeb/d54712/sandbox/scripts/bash.d/read1.sh
/auto/home/homeb/d54712/sandbox/scripts/data.d/states
/auto/home/homeb/d54712/sandbox/scripts/data.d/states12345" # List of files you are curious about
#Threw in in a dummy file, /auto/home/homeb/d54712/sandbox/scripts/bash.d/states12345
#
echo
for file in $MYFILES
do
if [ ! -e "$file" ]
then
echo "$file does not exist."
continue
fi
ls -l $file |awk '{ print $8 " file size: " $5 }' # print 3 fields
echo
done
exit 0
Results:
$ ./fileinfo.sh
14:23 file size: 126
15:30 file size: 78
/auto/home/homeb/d54712/sandbox/scripts/data.d/states12345 does not exist.
#
# fileinfo.sh
MYFILES="/auto/home/homeb/d54712/sandbox/scripts/bash.d/read1.sh
/auto/home/homeb/d54712/sandbox/scripts/data.d/states
/auto/home/homeb/d54712/sandbox/scripts/data.d/states12345" # List of files you are curious about
#Threw in in a dummy file, /auto/home/homeb/d54712/sandbox/scripts/bash.d/states12345
#
echo
for file in $MYFILES
do
if [ ! -e "$file" ]
then
echo "$file does not exist."
continue
fi
ls -l $file |awk '{ print $8 " file size: " $5 }' # print 3 fields
echo
done
exit 0
Results:
$ ./fileinfo.sh
14:23 file size: 126
15:30 file size: 78
/auto/home/homeb/d54712/sandbox/scripts/data.d/states12345 does not exist.
Subscribe to:
Comments (Atom)