Thursday, May 7, 2009

Date Command - Display Yesterday Date

Option 1:

#! /usr/bin/ksh
#
# Description:
# Get yesterday's date in YYYY-MM-DD format.
# With argument N in range 1..28 gets date N days before.
#
#

OFFSET=${1:-1}

case $OFFSET in
*[!0-9]* ???* 3? 29) print -u2 "Invalid input" ; exit 1;;
esac

eval `date "+day=%d; month=%m; year=%Y`
typeset -Z2 day month
typeset -Z4 year

# Subtract offset from day, if it goes below one use 'cal'
# to determine the number of days in the previous month.
day=$((day - OFFSET))
if (( day <= 0 )) ;then
month=$((month - 1))
if (( month == 0 )) ;then
year=$((year - 1))
month=12
fi
set -A days `cal $month $year`
xday=${days[$(( ${#days[*]}-1 ))]}
day=$((xday + day))
fi
print $year-$month-$day
print $month/$day/${year#??}
--------------------------------------------------------------------------------------------

Option 2:

datestamp=`date '+%Y%m%d'`
yest=$((datestamp -1))
print $yest

--------------------------------------------------------------------------------------------
To show current date:

$ echo `/usr/bin/date '+%Y%m%d'`
20090508

$ echo `date +"%d-%m-%Y %R"`
08-05-2009 11:08

$ echo `/usr/bin/date '+%Y%m%d%H%M'`
200905081148

Tuesday, March 24, 2009

Solaris如何change网卡IP地址?

1. Temporarily change 网卡IP地址

ifconfig用于配置网卡地址,改动后立即生效,但并不保存配置,下次启动后失效。

# ifconfig -a
lo0: flags=849 mtu 8232
inet 127.0.0.1 netmask ff000000
hme0: flags=863 mtu 1500
inet 172.16.255.2 netmask ffff0000 broadcast 172.16.255.255
ether 8:0:20:ee:11:51

ifconfig hme0 172.16.3.3 netmask 255.255.255.0

ifconfig –a
lo0: flags=849 mtu 8232
inet 127.0.0.1 netmask ff000000
hme0: flags=863 mtu 1500
inet 172.16.3.3 netmask ffffff00 broadcast 172.16.3.255
ether 8:0:20:ee:11:51

#vi /etc/hosts

2. Change 网卡IP地址 permanence

想要永久更改配置,则要更改配置文件,以网卡hme0为例:

#more /etc/hostname.hme0
Sunland

#more /etc/hosts
127.0.0.1 localhost
172.16.255.2 Sunland loghost

#vi /etc/hosts

#more /etc/hosts
127.0.0.1 localhost
172.16.3.3 Sunland loghost

#more /etc/netmasks
#
# The netmasks file associates Internet Protocol (IP) address # masks with IP network numbers.
#
# network-number netmask
#
# The term network-number refers to a number obtained from the Internet Network
# Information Center. Currently this number is restricted to being a class # A, B, or C network number. In the future we should be able to support # arbitrary network numbers per the Classless Internet Domain Routing # guidelines.
#
# Both the network-number and the netmasks are specified in
# "decimal dot" notation, e.g:
#
# 128.32.0.0 255.255.255.0

#vi /etc/netmasks

#more /etc/netmasks
#
# The netmasks file associates Internet Protocol (IP) address # masks with IP network numbers.
#
# network-number netmask
#
# The term network-number refers to a number obtained from the Internet Network # Information Center. Currently this number is restricted to being a class # A, B, or C network number. In the future we should be able to support # arbitrary network numbers per the Classless Internet Domain Routing # guidelines.
#
# Both the network-number and the netmasks are specified in
# "decimal dot" notation, e.g:
#
# 128.32.0.0 255.255.255.0
172.16.3.0 255.255.255.0

reboot

If/Else

Remember that the spacing is very important in the if statement.
Notice that the termination of the if statement is fi.
You can also replace the "==" with "!=" to test if the variables are NOT equal.

Comparisons:
-eq : equal to
-ne : not equal to
-lt : less than
-le : less than or equal to
-gt : greater than
-ge : greater than or equal to

File Operations:
-s : file exists and is not empty
-f : file exists and is not a directory
-d : directory exists
-x : file is executable
-w : file is writable
-r : file is readable

Script 1:

First, to write age to a file, named "username_DAT".

======================================
#!/bin/sh

# Prompt for a user name...
echo "Please enter your name:"
read USERNAME

# Check for the file.
if [ -s ${USERNAME}_DAT ]; then
# Read the age from the file.
AGE=`cat ${USERNAME}_DAT`
echo "You are $AGE years old!"
else
# Ask the user for his/her age
echo "How old are you?"
read AGE

if [ "$AGE" -le 2 ]; then
echo "You are too young!"
else
if [ "$AGE" -ge 100 ]; then
echo "You are too old!"
else
# Write the age to a new file.
echo $AGE > ${USERNAME}_DAT
fi
fi
fi
======================================

You can test multiple expressions at once by using the (or) operator or the && (and) operator.
The structure of elif is the same as the structure of if

Script 2:

======================================
#!/bin/sh

# Prompt for a user name...
echo "Please enter your age:"
read AGE

if [ "$AGE" -lt 20 ] [ "$AGE" -ge 50 ]; then
echo "Sorry, you are out of the age range."
elif [ "$AGE" -ge 20 ] && [ "$AGE" -lt 30 ]; then
echo "You are in your 20s"
elif [ "$AGE" -ge 30 ] && [ "$AGE" -lt 40 ]; then
echo "You are in your 30s"
elif [ "$AGE" -ge 40 ] && [ "$AGE" -lt 50 ]; then
echo "You are in your 40s"
fi
======================================