Let's say you have a fixed format and you need only the first character or first 4 characters:
Here I have Jan 31, 2003 10:30:43 formatted as 20030131103043:
DATECODE=20030131103043
YEAR=`echo $DATECODE | sed --silent 's/\(....\).*/\1/p'`
MONTH=`echo $DATECODE | sed --silent 's/....\(..\).*/\1/p'`
DAY=`echo $DATECODE | sed --silent 's/......\(..\).*/\1/p'`
HOUR=`echo $DATECODE | sed --silent 's/........\(..\).*/\1/p'`
MINUTE=`echo $DATECODE | sed --silent 's/..........\(..\).*/\1/p'`
SECOND=`echo $DATECODE | sed --silent 's/............\(..\)/\1/p'`
echo $YEAR
echo $MONTH
echo $DAY
echo $HOUR
echo $MINUTE
echo $SECOND
A faster way is to format it in one command:
echo $DATECODE | sed --silent 's/\(....\)\(..\)\(..\)\(..\)\(..\)\(..\).*/\2\/\3\/\1 - \4:\5:\6/p'
Another method when you have multiple separator symbols and you can't grab the one you want and the s///2 won't help you, then use the keep/throw method:
Let's say you have the following variable:
MYVARIABLE=errata-2.4.20-19.8smp
and you want to keep the version number, but throw away errata-. s///2 or s///1 doesn't work here and you're not guaranteed to get a number.
So you throw away what you want to keep (because of the left/right order of sed). Here we create a variable with the contents that we do not want. Then we can keep what we want and throw out what we don't want.
THROW=`echo $MYVARIABLE | sed -e 's/[0-9].*$//'`
KEEP=`echo $MYVARIABLE | sed -e "s/$THROW//"`
echo $KEEP
making a function and calling it:
myfunction(){
echo hello
}
myfunction
Dot sourcing (including) another file (which often contains functions):
. ./myfunctionsfile
echo Now I have loaded any variables and functions from ./myfunctions
echo so I can use any of them now.
Same for $include myfunctionsfile
Calling a 2nd script:
There isn't anything special for calling another script. When that
2nd script completes, control will be passed to the originating program
at the point immediately after the call to the 2nd script.
2nd script contents:
echo this is the 2nd script
1st script contents:
echo this is the first script
./2ndscript
echo and now back to the first script
Setting a variable in a for loop:
The following doesn't work:
for X in VAR; do $X=n; done
bash: VAR=n: command not found
Better is:
for X in VAR; do export $X=n; done
echo $VAR
Setting variables systemwide for each boot
in /etc/profile you will find a line stating something like
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC
Just above this add your info you need and add those variables to the export line:
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC http_proxy https_proxy CVSROOT
Cool stuff I wanted to find a long time ago:
Notification of job completion does not show up immediately:
(echo hi; sleep 3) &
Immediate notification of job completion:
set -b
(echo hi; sleep 3) &
Also here is debugging information
set -x
echo do debug stuff
set +x
echo debug turned off
Also at the top of the script file you can put debug there and
it will be in effect for the entire file:
#!/bin/sh -x
Let's say you want to create your own command to execute (sometimes it's faster this way or easier for for loops, etc.).
Here are several ways to get this done:
Method 1. dirty and slow because it touches the filesystem
echo COMMANDS TO RUN >> myscriptfile.sh
chmod +x myscriptfile.sh
./myscriptfile.sh
rm ./myscriptfile.sh
Method 2. this method still touches the filesystem (basically the same as method #1, but automates creating the script, checking for uniqueness, marking it executable, running it, then deleting the file), but a little better than method #1
sh <<-EOF
$COMMAND
EOF
Method 3. Best method, this should not touch the filesystem.
eval "sh $COMMAND"
(Not yet tested, but I saw this and it should be the best method available).