1. Alias
1.1. Defining alias
For commands that are used a lot, it's better to define an alias:
alias yourAlias='theRealCommand'
For example, in the following case, the command 'l' will call 'ls -al':
alias l='ls -al'
Note: The definition of the alias can be placed in .bashrc so that it will be defined for each new shell.
1.2. Deleting alias
To delete an alias, just call
unalias yourAlias
For example, to delete the cp alias, call
unalias cp
1.3. Temporarily disabling alias
If you want disable just for this time un alias, call
\alias parameter1 parameter2 ...
For example, you have defined such an alias
alias cp='cp -v'
and you want to copy something without showing what it is doing. Call
\cp file1 file2
1.4. Defining "function" instead of alias
It's not a good idea to define an alias to starting an application, because if the '&' character is not placed after the call, the application will block the shell until the application self will be closed.
For such case it's better to define a function:
functionName() { yourApplication yourParameters $* }
For example, in the following case, the command 'n myFile' will call 'nedit -rows 50 myFile':
n() { nedit -rows 50 $* & }
The '$*' indicates the list of arguments that you can give at the command line.
Note: The definition of the function can be placed in .bashrc so that it will be defined for each new shell.
2. Basic operations
2.1. Input command
2.1.1. Input
echo "A="
read A
echo "You entered $A"
2.1.2. Keypress
read -s -n2 -p "Hit a key " keypress
echo
echo "pressed '$keypress'"
-s: don't echo input
-n: accept only N characters of input
-p: echo the following prompt before reading input
If you want to show what key as been pressed, just remove the -s option.
2.2. Check arguments
if [ ! $# = 2 ]
then
echo " usage: `basename $0` first_argument second_argument"
exit 1;
fi
2.3. Check return code
`your_script`
if [ $? ]
echo "ERROR"
exit 1;
else
echo "OK"
fi
2.4. Print to screen and simultaneously log in a file
grep myword * tee -a file.log
2.5. Arithmetic calculations
expr 5 + 4
2.6. Logical operators
if [ "$a" -eq 24 ] && [ "$b" -eq 47 ]
if [ "$a" -eq 24 -a "$b" -eq 47 ]
if [ "$a" = rhino ] && [ "$b" = crocodile ]
See http://www.tldp.org/LDP/abs/html/abs-guide.html#ORREF" target="">Advanced Bash-Scripting Guide.
2.7. If ... else if ... else
if [ $A -gt 0 ]
then
echo "$A is bigger than zero"
elif [ $A -lt 0 ]
then
echo "$A is smaller than zero"
else
echo "$A is equal to zero"
fi
2.8. Sequence
A sequence of number can be indicates as following:
seq 0 9
Example:
for i in $(seq 0 9)
do
echo $i
done
3. Case
3.1. Changing case
To change the case from lowercase to uppercase:
echo "word" tr [:lower:] [:upper:]
To change the case from uppercase to lowercase:
echo "word" tr [:upper:] [:lower:]
To rename all files in a directory from lowercase to uppercase:
for i in *; do (echo $itr [:lower:] [:upper:]); done
4. File
4.1. Reading file line by line
exec < fileName
while read line
do
echo $line
done
LINES=( `cat "$FILE"` )
for LINE in ${LINES[@]}
do
echo $line
done
4.2. Remove commented lines from a file
sed '/^#/d' fileName
5. Find
5.1. Doing something only for searched elements
find PATH -name FILENAME -exec COMMAND {} \;
The
{}
indicates the output of the find command.
Example. Search all text file in home directory and copy them to /tmp:
find ~ -name "*.txt" -exec cp {} /tmp \;
5.2. Search in more specified directories
echo "/tmp/ /usr/" > directories
cat directories xargs -i find {} -name "*.txt"
6. Loop
6.1. For
for file in `ls *`
do
echo $file
done
6.2. For with list
LIST="one two three"
for ELEMENT in $LIST
do
echo $ELEMENT
done
6.3. While
while [ 1 ]
do
echo "hello";
done
7. Process
7.1. Own process id
To known the process id of the script it self, use the special variable
$$
Example:
echo "My process id is $$"
7.2. Pidof
Returns all the process having the specified name:
pidof nedit
17277 17270 17267
8. Shortcuts
8.1. Case
ALT + U: uppercase until end of word
ALT + L: lowercase until end of word
ALT + C: uppercase current char, lowercase the rest
8.2. Cut, copy, paste
CTRL + Y: paste
CTRL + U: cut begin of the line until char on the left
ALT + BACKSPACE: cut from current char to begin of current word
CTRL + K: cut from current char to end of line
8.3. Enter
CTRL + J: enter
CTRL + M: enter
CTRL + O: enter
8.4. History
CTRL + P: same as cursor up (history backwards)
CTRL + N: same as cursor down (history forwards)
CTRL + R: reverse search in history (from last command to first)
ALT + P: search in history (from first command to last)
!cucu: repeat last command started with the word cucu
8.5. Jumping
CTRL + A: jump to begin of the line
CTRL + E: jump to end of line
CTRL + F: same as cursor right
CTRL + B: same as cursor left
ALT + B: jump one word backwards
ALT + F: jump one word forwards
8.6. Process
CTRL + Z: send SIGSTOP to current process
CTRL + C: send SIGTERM to current process
CTRL + D: exit
8.7. Screen
CTRL + L: clear screen
CTRL + S: stop video scrolling
CTRL + Q: restart video scrolling
8.8. Others
CTRL + T: swap current char with them on the left
CTRL + H: backspace
CTRL + I: tabulator
9. Tricks
9.1. Beeping until key pressing
To announce to the user a long job has been done, the command
alarm
will beep until the user press a key.
9.2. Counting files
To get the number of files/directory present in the current directory:
ls wc -l
9.3. Extracting IP number from ifconfig
ifconfig eth0 grep inet cut -d : -f 2 cut -d \ -f 1
or using awk:
ifconfig eth0 awk -F ':' '/inet/ {print $2}' cut -f 1 -d ' '
9.4. Removing last slash in path
A="/usr/lib/"
B=${A/%\//}
echo $A
echo $B
9.5. Renaming file with blanks
find $DIR -maxdepth 1 -type f -print0 xargs -0 -n1 bash -c 'newname=${0// /_}; if [[ ! -e $newname ]];then eval mv \"$0\" $newname;fi;'
9.6. Wildcards
To get a list of specified files:
FILE='lib*.so'
or
FILE=`ls lib*.so`
10. Variables
10.1. Asking for returned value
When a script or programm is called, it returns a value giving informations about the result of its call (without error, error 1, error 2, ...).
This value is stored in the variable $? and can be easily checked.
myScript
echo $?
This value will be overwritten by the next call of a script or programm.
10.2. Defining and clearing
Normally, variables doesn't need to be declared. But if a variable is needed outside the script which declares it, it must be exported:
export MY_VARIABLE=Hello
Here how to verify that the variable has been correctly exported:
export grep MY_VARIABLE
To undo the operation:
unset MY_VARIABLE
Popular Posts
-
Here are some useful links about Indian laws, courts, legal system and various departments of Government of India. Man...
-
Network-Attached Storage 1. Introduction 2. What is a NAS Device? 3. What is a Filer? 4. Network-Attached Storage Versus Sto...
-
The Basics. Network Storage - The Basics Are you new to network storage? If so then this series of articles is for you! Over the next few...
-
Save a lot of typing with these handy bash features you won't find in an old-fashioned UNIX shell. bash, or the Bourne again shell, ...
-
Linux admin interview questions How do you take a single line of input from the user in a shell script? Write a script to convert all DO...
-
* Determine the IPv4 addresses that you want to use for the additional interfaces. * Ensure that the physical interface to be configured has...
-
UNIX Questions and Answers Most answers refer to Solaris 2.x systems Hardware Issues How can I configure new devices without rebooting? What...
-
Tried Gusty Gibbon Ubuntu 6.10 on Compaq f733AU works fine without any major problems. some tweaking required in udev/rules/70-persistent-ne...
-
write 6 random bits of base64-encoded data. This will produce an eight character string making a good unix (crypt based) password: $ openssl...
-
Bryce Harrington and Kees Cook have come together to write this informative article titled ' Managing Disk Space with LVM ' which ...
0 comments:
Post a Comment