Jan 11 2008

filenames …

Tag: Bashberrisch @ 7:39 pm

The whole article …..

Here's where a few examples would have helped.
To understand the man page I simply experimented
with the echo command and several shell variables.
This is what it all means:
      Given:
            foo=/tmp/my.dir/filename.tar.gz 

      We can use these expressions:

      path = ${foo%/*}
          To get: /tmp/my.dir (like dirname)
      file = ${foo##*/}
          To get: filename.tar.gz (like basename)
      base = ${file%%.*}
          To get: filename
      ext = ${file#*.}
          To get: tar.gz 

      Note that the last two depend on the assignment made in the second one

Here we notice two different "operators" being used inside the parameters (curly braces).
Those are the # and the % operators.
We also see them used as single characters and in pairs.
This gives us four combinations for trimming patterns off the beginning or end of a string:

${variable%pattern}
    Trim the shortest match from the end
${variable##pattern}
    Trim the longest match from the beginning
${variable%%pattern}
    Trim the longest match from the end
${variable#pattern}
    Trim the shortest match from the beginning

Jan 10 2008

basic arithmetics

Tag: Bashberrisch @ 7:57 pm



So I think it is very simple to do some shell scripting, but when you have to calculate some simple variables it is not very handy. So I asked my friend Google and here are some of the result, simply to add, subtract, multiply vars in the bash language!

VAR=55             # Assign integer 55 to variable VAR.
((VAR = VAR + 1))  # Add one to variable VAR.  Note the absence of the '$' character.
((++VAR))          # Another way to add one to VAR.  Performs C-style pre-increment.
((VAR++))          # Another way to add one to VAR.  Performs C-style post-increment.
echo $[VAR * 22]   # Multiply VAR by 22 and substitute the result into the command.
echo $((VAR * 22)) # Another way to do the above.
 
 
# some date arithmetics (yestarday and one month ago)
date -d “-1 day” +%d%m     #will give me the yesterdays date.
date -d “-1 month” +%d%m  #will give me date one month previous.
 
Example: 
YESTERDAY=`date -d"-1 day" "+%Y.%m.%d"`;
echo $YESTERDAY;

Dec 07 2007

Open multiple konsoles

Tag: Bashberrisch @ 6:46 pm
#!/bin/sh
SESSIONS="
SESSION_1
SESSION_2
"
 
KONSOLE=`dcopclient $KONSOLE_DCOP`
CURSESSION=$KONSOLE_DCOP_SESSION
for A in $SESSIONS ; do
NEWSESSION=`dcop $KONSOLE konsole newSession $A`
#dcop $KONSOLE $NEWSESSION renameSession $A
done
#dcop $CURSESSION closeSession

Next Page »