Find a particular class file in a directory of jar files
#!/bin/sh
findclass='ClassFileName'
for f in `ls`
do
jar tvf $f | grep ${findclass}
if [ $? = '0' ]
then
echo "Found ${findclass} in $f file."
fi
done
Find a particular class file in all jar files
#!/bin/sh
findclass='ClassFileName'
for f in `find . -name "*.jar"`
do
jar tvf $f | grep ${findclass}
if [ $? = '0' ]
then
echo "Found ${findclass} in $f file."
fi
done
Use getopts
#--------------------------------------------
# Get options
#--------------------------------------------
while getopts ":a:b:r:" opt
do
case $opt in
a)
alpha=$OPTARG
;;
b)
beta=$OPTARG
;;
r)
gamma=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG"
;;
:)
echo "Option -$OPTARG requires an argument."
;;
esac
done
grep Examples
# grep one of multiple strings
grep "string1\|string2\|string3"
# Inverse grep
grep -v "notThisString"
Remove Old Log Files
# List log files older than 30 days
find . -type f -mtime +30 -type f -name "*.log" -exec ls -altr {} \;
# Remove log files older than 30 days
find . -type f -mtime +30 -type f -name "*.log" -exec rm {} \;
# Remove log files older than 3 days
find . -type f -mtime +3 -type f -name "*.log" -exec rm {} \;
Filed under: bash | |Comments off
Assign Array Elements
# Assign array elements
echo
myArr[0]='one'
myArr[1]='two'
myArr[10]='ten'
# Access array elements. *Curly brackets* are important.
echo "\${myArr[0]}: ${myArr[0]}" # ${myArr[0]}: one.
# Echo all elements
echo "\${myArr[@]}: ${myArr[@]}" # ${myArr[@]}: one two ten
# Initialize array
echo
myArr3=( one two three four )
# Echo all elements
echo "\$myArr3[@]: ${myArr3[@]}" # $myArr3[@]: one two three four
# Number of array elements
echo "\$#myArr3[@]: ${#myArr3[@]}" # $#myArr3[@]: 4
# Initialize array
echo
myStr2='one two three four'
myArr2=( $myStr2 )
# Echo all elements
echo "\${myArr2[@]}: ${myArr2[@]}" # ${myArr2[@]}: one two three four
Tokenize String Into Array
myString2=one:two::four
oldIFS=$IFS
IFS=:
myArr2=( $myString2 )
IFS=$oldIFS
echo ${myArr2[0]} # Output: 'one'
echo ${myArr2[1]} # Output: 'two'
echo ${myArr2[2]} # Output: ''
echo ${myArr2[3]} # Output: 'four'
Loop Through Array Elements
myStrings='string1 string2 string3'
myStringArray=( ${myStrings} )
for myString in "${myStringArray[@]}"
do
echo "${myString}"
done
References
http://tldp.org/LDP/abs/html/arrays.html
Filed under: bash | |Comments off
sed -n l file
tab character displayed as ">" character.
Filed under: bash, unix | |Comments off