- Generate all the possible combinations of 3-letter strings containing the letters A-Z and put a newline char at the end of each of them:
echo {a..z}{a..z}{a..z}{a..z}$'\n' - Grep multiple strings:
cat /proc/meminfo | grep -i "write\|dirty" - Execute a command for each file that find finds: "find . -name *.mp3 -exec normalize '{}' \;"
- Parameters given to a script are contained in "$0", "$1", etc... variables.
- String thingys:
sed 's/string1/string2/g' Replace string1 with string2
sed 's/\(.*\)1/\12/g' Modify anystring1 to anystring2
sed '/ *#/d; /^ *$/d' Remove comments and blank lines
sed ':a; /\\$/N; s/\\\n//; ta' Concatenate lines with trailing \
sed 's/[ \t]*$//' Remove trailing spaces from lines
sed 's/\([\\`\\"$\\\\]\)/\\\1/g' Escape shell metacharacters active within double quotes
sed -n '1000p;1000q' Print 1000th line
sed -n '10,20p;20q' Print lines 10 to 20 - More examples about the usage of sed: http://student.northpark.edu/pemente/sed/sed1line.txt
- If else condition:
#!/bin/sh
BAO=`ps -a -o comm | grep fireflies | grep -v grep`
if [ -f $BAO]
then
#Do here something if BAO is empty
#Start fireflies
else
#If BAO contains something
#Stop fireflies
fi - Scripts to be executed in /etc/init.d/ have to start with "#!/sbin/runscript" instead of the usual "#!/bin/sh"
- For loop:
==================
#!/bin/bash
for ((a=1; a <= 64999 ; a++))
do
ps -o cpu -p 20345034 >> outputps
sleep 5
done
exit 0
================== - Example of substring etc...
================================
#!/bin/bash
rm outputps
echo "I am gonna watch this program:" >> outputps
echo $(ps -Af | grep $1 | grep -v grep | grep -v checkps) >> outputps
echo "" >> outputps
PROG2BWATCHED=$1
for ((a=1; a <= 64999 ; a++))
do
VAR=$(ps -Af | grep $PROG2BWATCHED | grep -v grep | grep -v checkps)
#echo $VAR #FORDEBUG
CHECKIF=$(echo $VAR | sed 's/ //g')
CHECKIF=${CHECKIF:0:15}
#echo "CHECKIF is -"$CHECKIF"-" #FORDEBUG
if [ -f $CHECKIF ]
then
echo "You did not specify a program of program not found!"
else
VAR=${VAR:9:20}
POS=$(echo `expr index "$VAR" " "`)
FINAL=${VAR:0:POS}
echo "DO NOT SHUT ME DOWN!"
echo "watching program: "$PROG2BWATCHED
#echo "VAR is: "$VAR #FORDEBUG
#echo "POS is: "$POS #FORDEBUG
echo "Program s PID: "$FINAL
echo $(ps -o cpu,nice -p $FINAL | grep -v CP),$(date) >> outputps
ps -o cpu,nice -p $FINAL | grep -v CP
fi
sleep 5
done
exit 0
================================ - Creating insert statements
- Insert "insert into filedata_temp values(" " at the beginning of each line
cat "$1" | sed 's/^/insert into filedata_temp values("/' > "$1"_temp1.txt - Remove the ":"
cat "$1"_temp1.txt | sed 's/://g' > "$1"_temp2.txt - Replace the " " and the " " blanks between the fields with ","
cat "$1"_temp2.txt | sed 's/ /","/g' | sed 's/ /","/g' > "$1"_temp3.txt - Replace the EOL with ");
cat "$1"_temp3.txt | sed 's/.$/");/' > "$1"_temp4.txt - Eventually use...
cat "$1"_temp3.txt | sed 's/$/");/' > "$1"_temp4.txt
...if the file does not have a MSDOS EOL - Eliminate the first line which contains the header
tail -n +2 "$1"_temp4.txt > "$1"_ready.txt - rm *_temp*
- Insert "insert into filedata_temp values(" " at the beginning of each line
- Get a substring inside a Bash-script, specifying a fixed start and end character place:
substring=${YOURVAR:0:3} - Get a substring inside a Bash-script, specifying the column, where the columns are delimited by a "space"-character:
SUPERVAR="ciao bao miao"
SUBSTRING=$(echo $SUPERVAR | awk '{n=split($0,tmp," ");print tmp[2]}') - Loop through files:
for f in frame*
do
echo Processing $f and creating transparent-$f
convert -transparent "rgb(255,255,255)" -fuzz "25%" -format gif $f transparent-$f.gif
done
- Listing the CPU nice-level/priority of all processes:
ps -A -o pid,cmd,nice - Showing the CPU nice-level/priority of process 1234:
ps -o pid,cmd,nice -p 1234 - Raising the CPU priority of process 1234 by 10 (the higher the value the lower the priority - root processes have 0, kernel processes have -20):
renice 10 -p 1234 - Setting the I/O priority of process 1234 to 2 (the lower the value the higher the priority) and assigning it to the class 2 (3: idle / 2: best-effort / 1: realtime):
ionice -c2 -n2 -p 1234 - Showing the current I/O priority of proces 1234:
ionice -p 1234