Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Tuesday, March 16, 2010

How to get the latest file from the directory

#
way 1:
$ ls -lrt | awk '{ f=$NF }; END{ print f }'
#
#The above awk is to print the last argument of the last line

#
way 2:
$ ls -t1 | head -n

REFERENCE: bash - parameter expansion, i.e. http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02

file=data.txt
  echo ${file%.*}
  echo ${file#*.}

outputs the file base and extension respectively ("data" and "txt").

${variable#pattern} is similar to "echo $variable |sed 's/^pattern//'"
but the pattern matching isn't "greedy" unless you double the #.
Likewise ${variable%pattern} is similar to
"echo $variable | sed 's/pattern$//'"

Thursday, March 4, 2010

Read file writting strings into array

declare -a ARRAY
count=0

while read line; do
echo $line
ARRAY[$count]=$line
let count=count+1
echo $count
done < ips.tmp

echo ${ARRAY[1]}
echo ${#ARRAY[@]}

Wednesday, February 10, 2010

How to find out the symlink source (without readlink)

$ cat basher
#!/usr/local/bin/bash
string="lrwxr-xr-x 1 mmmm users 66 Sep 4 09:58 LINK_SEND -> /apps/aaa/home/mmmm/FFFFF/Apps/RRRRRR/unit_tests/TARGET_SEND"
echo $string
string=${string##* }
echo $string
string=${string%/*}
echo $string
exit 0
$
$
$ ./basher
lrwxr-xr-x 1 mmmm users 66 Sep 4 09:58 LINK_SEND -> /apps/aaa/home/mmmm/FFFFF/Apps/RRRRRR/unit_tests/TARGET_SEND
/apps/aaa/home/mmmm/FFFFF/Apps/RRRRRR/unit_tests/TARGET_SEND
/apps/aaa/home/mmmm/FFFFF/Apps/RRRRRR/unit_tests
$

source