#!/bin/bash
HOST='192.168.31.75'
USER='indra'
PASSWD='indra'
FILE='file_transaksi_*'
# please don't use "\" for \END_SCRIPT this used because error at write the blog
ftp -n $HOST <<\END_SCRIPT
quote USER $USER
quote PASS $PASSWD
# bin mode
bin
# answer yes for any question
prompt
lcd /home/indra/trans
cd /home/data/trans/backup
mget $FILE
quit
END_SCRIPT
exit 0
Tuesday, December 11, 2007
About Shell
this letter from other site, but i am forgot exactly where is it?
i hope this letter can help anyone who needed to learn shell script included me.
1. Tests
Tests (for ifs and loops) are done with [ ] or with the test command.
1.1. Checking files
-r file Check if file is readable.
-w file Check if file is writable.
-x file Check if we have execute access to file.
-f file Check if file is an ordinary file (as opposed to a directory, a device special file, etc.)
-s file Check if file has size greater than 0.
-d file Check if file is a directory.
-e file Check if file exists. Is true even if file is a directory.
Example:
if [ -s file ]
then
such and such
fi
1.2. Checking strings
s1 = s2 Check if s1 equals s2. s1 != s2 Check if s1 is not equal to s2. -z s1 Check if s1 has size 0. -n s1 Check if s2 has nonzero size. s1 Check if s1 is not the empty string.
Example:
if [ $myvar = "hello" ]
then
echo "We have a match"
fi
1.3. Checking numbers
Note that a shell variable could contain a string that represents a number. If you want to check the numerical value use one of the following:
n1 -eq n2 Check to see if n1 equals n2.
n1 -ne n2 Check to see if n1 is not equal to n2.
n1 -lt n2 Check to see if n1 <>
n1 -le n2 Check to see if n1 <= n2.
n1 -gt n2 Check to see if n1 > n2.
n1 -ge n2 Check to see if n1 >= n2.
Example:
if [ $# -gt 1 ]
then
echo "ERROR: should have 0 or 1 command-line parameters"
fi
1.4. Boolean operators
! not
-a and
-o or
Example:
if [ $num -lt 10 -o $num -gt 100 ]
then
echo "Number $num is out of range"
elif [ ! -w $filename ]
then
echo "Cannot write to $filename"
fi
Note that ifs can be nested. For example:
if [ $myvar = "y" ]
then
echo "Enter count of number of items"
read num
if [ $num -le 0 ]
then
echo "Invalid count of $num was given"
else
... do whatever ...
fi
fi
The above example also illustrates the use of read to read a string from the keyboard and place it into a shell variable. Also note that most UNIX commands return a true (nonzero) or false (0) in the shell variable status to indicate whether they succeeded or not. This return value can be checked. At the command line echo $status. In a shell script use something like this:
if grep -q shell bshellref
then
echo "true"
else
echo "false"
fi
Note that -q is the quiet version of grep. It just checks whether it is true that the string shell occurs in the file bshellref. It does not print the matching lines like grep would otherwise do.
2. I/O Redirection
pgm > file Output of pgm is redirected to file.
pgm <>
pgm >> file Output of pgm is appended to file.
pgm1 | pgm2 Output of pgm1 is piped into pgm2 as the input to pgm2.
n > file Output from stream with descriptor n redirected to file.
n >> file Output from stream with descriptor n appended to file.
n >& m Merge output from stream n with stream m.
n <& m Merge input from stream n with stream m.
<<>
Note that file descriptor 0 is normally standard input, 1 is standard output, and 2 is standard error output.
3. Shell Built-in Variables
$0 Name of this shell script itself.
$1 Value of first command line parameter (similarly $2, $3, etc)
$# In a shell script, the number of command line parameters.
$* All of the command line parameters.
$- Options given to the shell.
$? Return the exit status of the last command.
$$ Process id of script (really id of the shell running the script)
4. Pattern Matching
* Matches 0 or more characters.
? Matches 1 character.
[AaBbCc] Example: matches any 1 char from the list.
[^RGB] Example: matches any 1 char not in the list.
[a-g] Example: matches any 1 char from this range.
5. Quoting
\c Take character c literally.
`cmd` Run cmd and replace it in the line of code with its output.
"whatever" Take whatever literally, after first interpreting $, `...`, \
'whatever' Take whatever absolutely literally.
Example:
match=`ls *.bak` #Puts names of .bak files into shell variable match.
echo \* #Echos * to screen, not all filename as in: echo *
echo '$1$2hello' #Writes literally $1$2hello on screen.
echo "$1$2hello" #Writes value of parameters 1 and 2 and string hello.
6. Grouping
Parentheses may be used for grouping, but must be preceded by backslashes since parentheses normally have a different meaning to the shell (namely to run a command or commands in a subshell). For example, you might use:
if test \( -r $file1 -a -r $file2 \) -o \( -r $1 -a -r $2 \)
then
do whatever
fi
7. Case statement
Here is an example that looks for a match with one of the characters a, b, c. If $1 fails to match these, it always matches the * case. A case statement can also use more advanced pattern matching.
case "$1" in
a) cmd1 ;;
b) cmd2 ;;
c) cmd3 ;;
*) cmd4 ;;
esac
8. Shell Arithmetic
In the original Bourne shell arithmetic is done using the expr command as in: result=`expr $1 + 2` result2=`expr $2 + $1 / 2` result=`expr $2 \* 5` (note the \ on the * symbol)
With bash, an expression is normally enclosed using [ ] and can use the following operators, in order of precedence: * / % (times, divide, remainder)
1. - (add, subtract) < > <= >= (the obvious comparison operators) == != (equal to, not equal to) && (logical and)
(logical or)
= (assignment) Arithmetic is done using long integers.
Example:
result=$[$1 + 3]
In this example we take the value of the first parameter, add 3, and place the sum into result.
9. Order of Interpretation
The bash shell carries out its various types of interpretation for each line in the following order:
brace expansion (see a reference book)
~ expansion (for login ids)
parameters (such as $1)
variables (such as $var)
command substitution (Example: match=`grep DNS *` )
arithmetic (from left to right)
word splitting
pathname expansion (using *, ?, and [abc] )
10. Looping
# to see by row in the file
filename="/export/home/indra/listfile.lst"
cat $filename | while read x; do
echo $x
done
11. Other Shell Features
$var Value of shell variable var.
${var}abc Example: value of shell variable var with string abc appended.
# At start of line, indicates a comment.
var=value Assign the string value to shell variable var.
cmd1 && cmd2 Run cmd1, then if cmd1 successful run cmd2, otherwise skip.
cmd1 || cmd2 Run cmd1, then if cmd1 not successful run cmd2, otherwise skip.
cmd1; cmd2 Do cmd1 and then cmd2.
cmd1 & cmd2 Do cmd1, start cmd2 without waiting for cmd1 to finish.
(cmds) Run cmds (commands) in a subshell.
12. sed
bash-2.05$ more BandarLampung.txt
510014030735812|89620130000281606063|85840839128|2166|3107
510014030735813|89620130000281606071|85840839129|2166|3107
bash-2.05$ cat BandarLampung.txt | sed 's/|3107/|3000/1' | head
510014030735812|89620130000281606063|85840839128|2166|3000
510014030735813|89620130000281606071|85840839129|2166|3000
13. for loop file
#!/bin/sh
for i in `ls -l /app/prdtrt12/batch/SIMBOX/archive/*.out | awk '{FS=" "}{if ($5==0) print $9}'`
do
echo "deleteing file $i ..."
rm $i
done
i hope this letter can help anyone who needed to learn shell script included me.
1. Tests
Tests (for ifs and loops) are done with [ ] or with the test command.
1.1. Checking files
-r file Check if file is readable.
-w file Check if file is writable.
-x file Check if we have execute access to file.
-f file Check if file is an ordinary file (as opposed to a directory, a device special file, etc.)
-s file Check if file has size greater than 0.
-d file Check if file is a directory.
-e file Check if file exists. Is true even if file is a directory.
Example:
if [ -s file ]
then
such and such
fi
1.2. Checking strings
s1 = s2 Check if s1 equals s2. s1 != s2 Check if s1 is not equal to s2. -z s1 Check if s1 has size 0. -n s1 Check if s2 has nonzero size. s1 Check if s1 is not the empty string.
Example:
if [ $myvar = "hello" ]
then
echo "We have a match"
fi
1.3. Checking numbers
Note that a shell variable could contain a string that represents a number. If you want to check the numerical value use one of the following:
n1 -eq n2 Check to see if n1 equals n2.
n1 -ne n2 Check to see if n1 is not equal to n2.
n1 -lt n2 Check to see if n1 <>
n1 -le n2 Check to see if n1 <= n2.
n1 -gt n2 Check to see if n1 > n2.
n1 -ge n2 Check to see if n1 >= n2.
Example:
if [ $# -gt 1 ]
then
echo "ERROR: should have 0 or 1 command-line parameters"
fi
1.4. Boolean operators
! not
-a and
-o or
Example:
if [ $num -lt 10 -o $num -gt 100 ]
then
echo "Number $num is out of range"
elif [ ! -w $filename ]
then
echo "Cannot write to $filename"
fi
Note that ifs can be nested. For example:
if [ $myvar = "y" ]
then
echo "Enter count of number of items"
read num
if [ $num -le 0 ]
then
echo "Invalid count of $num was given"
else
... do whatever ...
fi
fi
The above example also illustrates the use of read to read a string from the keyboard and place it into a shell variable. Also note that most UNIX commands return a true (nonzero) or false (0) in the shell variable status to indicate whether they succeeded or not. This return value can be checked. At the command line echo $status. In a shell script use something like this:
if grep -q shell bshellref
then
echo "true"
else
echo "false"
fi
Note that -q is the quiet version of grep. It just checks whether it is true that the string shell occurs in the file bshellref. It does not print the matching lines like grep would otherwise do.
2. I/O Redirection
pgm > file Output of pgm is redirected to file.
pgm <>
pgm >> file Output of pgm is appended to file.
pgm1 | pgm2 Output of pgm1 is piped into pgm2 as the input to pgm2.
n > file Output from stream with descriptor n redirected to file.
n >> file Output from stream with descriptor n appended to file.
n >& m Merge output from stream n with stream m.
n <& m Merge input from stream n with stream m.
<<>
Note that file descriptor 0 is normally standard input, 1 is standard output, and 2 is standard error output.
3. Shell Built-in Variables
$0 Name of this shell script itself.
$1 Value of first command line parameter (similarly $2, $3, etc)
$# In a shell script, the number of command line parameters.
$* All of the command line parameters.
$- Options given to the shell.
$? Return the exit status of the last command.
$$ Process id of script (really id of the shell running the script)
4. Pattern Matching
* Matches 0 or more characters.
? Matches 1 character.
[AaBbCc] Example: matches any 1 char from the list.
[^RGB] Example: matches any 1 char not in the list.
[a-g] Example: matches any 1 char from this range.
5. Quoting
\c Take character c literally.
`cmd` Run cmd and replace it in the line of code with its output.
"whatever" Take whatever literally, after first interpreting $, `...`, \
'whatever' Take whatever absolutely literally.
Example:
match=`ls *.bak` #Puts names of .bak files into shell variable match.
echo \* #Echos * to screen, not all filename as in: echo *
echo '$1$2hello' #Writes literally $1$2hello on screen.
echo "$1$2hello" #Writes value of parameters 1 and 2 and string hello.
6. Grouping
Parentheses may be used for grouping, but must be preceded by backslashes since parentheses normally have a different meaning to the shell (namely to run a command or commands in a subshell). For example, you might use:
if test \( -r $file1 -a -r $file2 \) -o \( -r $1 -a -r $2 \)
then
do whatever
fi
7. Case statement
Here is an example that looks for a match with one of the characters a, b, c. If $1 fails to match these, it always matches the * case. A case statement can also use more advanced pattern matching.
case "$1" in
a) cmd1 ;;
b) cmd2 ;;
c) cmd3 ;;
*) cmd4 ;;
esac
8. Shell Arithmetic
In the original Bourne shell arithmetic is done using the expr command as in: result=`expr $1 + 2` result2=`expr $2 + $1 / 2` result=`expr $2 \* 5` (note the \ on the * symbol)
With bash, an expression is normally enclosed using [ ] and can use the following operators, in order of precedence: * / % (times, divide, remainder)
1. - (add, subtract) < > <= >= (the obvious comparison operators) == != (equal to, not equal to) && (logical and)
(logical or)
= (assignment) Arithmetic is done using long integers.
Example:
result=$[$1 + 3]
In this example we take the value of the first parameter, add 3, and place the sum into result.
9. Order of Interpretation
The bash shell carries out its various types of interpretation for each line in the following order:
brace expansion (see a reference book)
~ expansion (for login ids)
parameters (such as $1)
variables (such as $var)
command substitution (Example: match=`grep DNS *` )
arithmetic (from left to right)
word splitting
pathname expansion (using *, ?, and [abc] )
10. Looping
# to see by row in the file
filename="/export/home/indra/listfile.lst"
cat $filename | while read x; do
echo $x
done
11. Other Shell Features
$var Value of shell variable var.
${var}abc Example: value of shell variable var with string abc appended.
# At start of line, indicates a comment.
var=value Assign the string value to shell variable var.
cmd1 && cmd2 Run cmd1, then if cmd1 successful run cmd2, otherwise skip.
cmd1 || cmd2 Run cmd1, then if cmd1 not successful run cmd2, otherwise skip.
cmd1; cmd2 Do cmd1 and then cmd2.
cmd1 & cmd2 Do cmd1, start cmd2 without waiting for cmd1 to finish.
(cmds) Run cmds (commands) in a subshell.
12. sed
bash-2.05$ more BandarLampung.txt
510014030735812|89620130000281606063|85840839128|2166|3107
510014030735813|89620130000281606071|85840839129|2166|3107
bash-2.05$ cat BandarLampung.txt | sed 's/|3107/|3000/1' | head
510014030735812|89620130000281606063|85840839128|2166|3000
510014030735813|89620130000281606071|85840839129|2166|3000
13. for loop file
#!/bin/sh
for i in `ls -l /app/prdtrt12/batch/SIMBOX/archive/*.out | awk '{FS=" "}{if ($5==0) print $9}'`
do
echo "deleteing file $i ..."
rm $i
done
How I can get Yesterday Date?
#!/bin/sh
# File-name: yesterday.sh
#-----------------------------------------------
# Returns date 1 day ago from the specified date
# Current date is taken if no date is specified
#-----------------------------------------------
# Input: Default:
# $1 - dd Current day
# $2 - mm Current month
# $3 - yyyy Current year
#-----------------------------------------------
#This is how a function is defined in a
#UNIX shell scripts
get_one_day_before_specified_date()
{
#get the command line input(date month & year)
day=$1
month=$2
year=$3
# if it is the first day of the month
if [ $day -eq 01 ]
then
# if it is the first month of the year
if [ $month -eq 01 ]
then
# make the month as 12
month=12
# deduct the year by one
year=`expr $year - 1`
else
# deduct the month by one
month=`expr $month - 1`
fi
# use cal command, discard blank lines,
# take last field of last line,
# first awk command is used to get the
# last useful line of the calendar cmd,
# second awk command is used to get the
# last field of this last useful line,
# NF is no. of fields,
# $NF is value of last field
day=`cal $month $year | awk 'NF != 0{ last = $0 }; END{ print last }' | awk '{ print $NF }'`
else
# deduct the day by one
day=`expr $day - 1`
fi
lengthd=`echo "$day" | wc -c | cut -c1-8`
lengthd=`expr $lengthd - 1`
lengthm=`echo "$month" | wc -c | cut -c1-8`
lengthm=`expr $lengthm - 1`
if [ $lengthd -eq 1 ]
then
day="0"$day
fi
if [ $lengthm -eq 1 ]
then
month="0"$month
fi
echo $year-$month-$day
}
#!/bin/ksh
if [ $# -ne 3 ]
then
d=`date +%d`
m=`date +%m`
y=`date +%Y`
else
d=$1
m=$2
y=$3
fi
#Cmd line arguments are captured in a shell script
#through $1 $2 $3, ......., $9,${10} (not $10)
# This is how we call unix user-defined functions,
# notice it is not junk123( $1, $2, $3 ) format
get_one_day_before_specified_date $d $m $y
# end of script
or you can try this command in other script
bash-2.05$ TZ=y17 date +%c %Y
result : Fri Jul 18 07:24:35 2008
VCURDATE=`TZ=y17 date +%c %Y`
tgl=`echo $VCURDATE | cut -c9-10`
bln=`echo $VCURDATE | cut -c5-7`
thn=`echo $VCURDATE | cut -c21-24`
# File-name: yesterday.sh
#-----------------------------------------------
# Returns date 1 day ago from the specified date
# Current date is taken if no date is specified
#-----------------------------------------------
# Input: Default:
# $1 - dd Current day
# $2 - mm Current month
# $3 - yyyy Current year
#-----------------------------------------------
#This is how a function is defined in a
#UNIX shell scripts
get_one_day_before_specified_date()
{
#get the command line input(date month & year)
day=$1
month=$2
year=$3
# if it is the first day of the month
if [ $day -eq 01 ]
then
# if it is the first month of the year
if [ $month -eq 01 ]
then
# make the month as 12
month=12
# deduct the year by one
year=`expr $year - 1`
else
# deduct the month by one
month=`expr $month - 1`
fi
# use cal command, discard blank lines,
# take last field of last line,
# first awk command is used to get the
# last useful line of the calendar cmd,
# second awk command is used to get the
# last field of this last useful line,
# NF is no. of fields,
# $NF is value of last field
day=`cal $month $year | awk 'NF != 0{ last = $0 }; END{ print last }' | awk '{ print $NF }'`
else
# deduct the day by one
day=`expr $day - 1`
fi
lengthd=`echo "$day" | wc -c | cut -c1-8`
lengthd=`expr $lengthd - 1`
lengthm=`echo "$month" | wc -c | cut -c1-8`
lengthm=`expr $lengthm - 1`
if [ $lengthd -eq 1 ]
then
day="0"$day
fi
if [ $lengthm -eq 1 ]
then
month="0"$month
fi
echo $year-$month-$day
}
#!/bin/ksh
if [ $# -ne 3 ]
then
d=`date +%d`
m=`date +%m`
y=`date +%Y`
else
d=$1
m=$2
y=$3
fi
#Cmd line arguments are captured in a shell script
#through $1 $2 $3, ......., $9,${10} (not $10)
# This is how we call unix user-defined functions,
# notice it is not junk123( $1, $2, $3 ) format
get_one_day_before_specified_date $d $m $y
# end of script
or you can try this command in other script
bash-2.05$ TZ=y17 date +%c %Y
result : Fri Jul 18 07:24:35 2008
VCURDATE=`TZ=y17 date +%c %Y`
tgl=`echo $VCURDATE | cut -c9-10`
bln=`echo $VCURDATE | cut -c5-7`
thn=`echo $VCURDATE | cut -c21-24`
Subscribe to:
Posts (Atom)