BASH
Scripting
Tests
We can
perform testing on Strings, Files an integers
#which
test ---> To know where it has been installed.
Integer
testing:
In
Unix 0 Good
1 Bad
In
Boolean 0 False
1 True
# echo
$? ---> To see previous command's excution status.
-eq == ---> Equal
to
-ne != ---> Not
equal to
-le <= ---> Less
than or equal to
-ge >= ---> Greater
than or equal to
-lt < ---> Less
than
-gt > ---> Greater
than
-nt ---> Newer
than
-ot ---> Older
than
-e ---> Exists
Commands:
#
tac ---> Inverse command of cat
Script 1. Bash script for translating Upper case filenames to lowercase.
GitHub Link
Script 1. Bash script for translating Upper case filenames to lowercase.
GitHub Link
Script 2. Bash script for translating Upper case filenames to lowercase and rename a file.
GitHub Link
GitHub Link
Command
Chaining:
We
can give number of commands at a time in a single line which are
separated by";".
In
this way we can execute commands one after one serially.
Ex:
#
clear ; pwd ; echo hello world
But in
this way every command works independently. i.e. irrespective of the
previous commands status.
If we
want to use a command depending on the previous command's exit status
we can use in the below way.
Ex:
# ls
-l && pwd
If the
exit status of the first command is 0 the second command will run
otherwise it won't.
# ls
-z || pwd
If the
exit status of the first command is 1 the second command will run
because in unix ls command doesn't have anargument z so the first
command is false.
&& ---> AND
|| ---> OR
# echo
"Hello world"
Hello
world
# echo
"hello world $value"
hello
world
# echo
'hello world $value'
Functions:
Like "real" programming languages, Bash has functions, though in a somewhat limited implementation. A function is a subroutine, a code block that implements a set of operations, a "black box" that performs a specified task. Wherever there is repetitive code, when a task repeats with only slight variations in procedure, then consider using a function.
function function_name {
command...}
or
function_name () {
command...
}
command...
}
This second form will cheer the hearts of C programmers (and is more portable).
As in C, the function's opening bracket may optionally appear on the second line.
function_name ()
{
command...
}
{
command...
}
Script 4: Functions
GitHub Link
Loops
A loop is a block of code that iterates a list of commands as long as the loop control condition is true.
For Loops :
For Loops :
for
argument in [ list ]
do
action item
done
While
Loops:
while condition
while condition
do action item
done
Until
Loops:
until condition
until condition
do action item
Control
Structure:
Script 10: Control Structure 1
GitHub Link
As may be expected, the shell language contains many special features for working with filenames. It is thus very easy to create control structures based on the simple examples above that perform complex operations on files.
For example, it is trivial to write a for loop that will rename files to fit a certain pattern. Since the shell itself is evaluating the loop, the LIST can be a regular expressions just like those given to any other command.
This script adds the prefix "text-" to all files in the current directory whose names end with ".txt", and moves them to the "unix/" subdirectory:
#!/bin/bash
for file in *.txt; do
mv $file text/unix-$file
done
But suppose we want to make sure no files get overwritten. This can be done with an if statement that tests for the existence of a file. The next script will do the same as the above, but also preserves any files that would have been lost by moving them to the "backup/" subdirectory:
#!/bin/bash
for file in *.txt; do
if [ -e text/unix-$file ]; then
mv text/unix-$file backup/unix-$file
fi
mv $file text/unix-$file
done
Script 10: Control Structure 1
GitHub Link
Special built-in environmental variables are positional parameters which hold command-line arguments to positions with the names 1,2,3,4, etc. which are indicated by $1,$2,$3,$4, etc. Argument $0 is the name of the script.
The select construct allows easy menu generation. The syntax is quite similar to that of the for loop:
LIST is expanded, generating a list of items. The expansion is printed to standard error; each item is preceded by a number. If in LIST is not present, the positional parameters are printed, as if in $@ would have been specified. LIST is only printed once.
Upon printing all the items, the PS3 prompt is printed and one line from standard input is read. If this line consists of a number corresponding to one of the items, the value of WORD is set to the name of that item. If the line is empty, the items and the PS3 prompt are displayed again. If an EOF (End Of File) character is read, the loop exits. Since most users don't have a clue which key combination is used for the EOF sequence, it is more user-friendly to have a break command as one of the items. Any other value of the read line will set WORD to be a null string.
The read line is saved in the REPLY variable.
select var in "CHOICE1 CHOICE2"
do
command
Move
many files:
Create "ftpsynchronize.lftp" with the following text.
open -u <user>,<passwd> <remote hostname/IP>
cd temp2
lcd /root/temp2
mirror -Rn
Script 18: Move many files 1
Script 20: Monitor Service
Script 21: Script for FTP Synchronization
open -u <user>,<passwd> <remote hostname/IP>
cd temp2
lcd /root/temp2
mirror -Rn
No comments :
Post a Comment