bash programming intro
http://www.faqs.org/docs/Linux-HOWTO/Bash-Prog-Intro-HOWTO.html
ssh
to ssh into another box with a non-standard port
Zip
to zip all of a directory in linux
zip -r <file_name>.zip <dir_to_zip>
Tar
to tar the contents of a directory (creating the tar):
tar -cvf <file_name>.tar <dir_to_zip>
to add to an existing tar
tar -rvf <file_name>.tar <dir_to_zip>
to list the contents of a tar
to extract the file
to zip the file
To set the date time (to Jan 13 2006, 9:51am)
Note AMERICAN DATE FORMAT
date -s "01/13/2006 09:51:00"
Secure Copy: scp
To copy a file from one box to another
scp -P 7272 <local file> user@host:<remote location>
Who's using the system, from where etc
w - show who is logged on and what they are doing
users - print the user names of users currently logged in to the current host
last - show listing of last logged in users
Links
To delete a symbolic link:
ie. Delete it as a regular file (make sure there is not / after it (if the link is a directory).
Finding text
The following command will find all css files containing "background.gif". Customise as required.
find . -name "*.css" -exec grep -H background.gif {} ";"
Deleting files
first to verify we're not deleting sensitive files.
The following command will use find to delete all found files (.svn files)
find . -name *.svn -exec rm -rf {} ;Usually
will do on most *nix, but not on the Mac ;(
chkconfig
For checking and setting the run levels on a box
Global profiles
__Application Environment Setup Using /etc/profile.d/*_
When a user logs in, environment variables are set from various places. That includes /etc/profile (for all users).
Then all the files in the /etc/profile.d directory.
Then ~/.bash_profile, then ~/.bashrc.
/etc/profile.d/ is a good place to put your application specific setups. For example, I always use SSH for CVS (cf. RSH). So I use:
echo "export CVS_RSH=ssh" >> /etc/profile.d/cvs.sh
chmod +x /etc/profile.d/cvs.sh
Port forwarding (eg. access mysql through ssh)
Use the following command to allow connection from a local mysql client to connect to a remote mysql server on 3306 through ssh. (i.e. port 3306 is not open to outside world)
ssh -p 7272 -L 9906:localhost:3306 mpecher@marandcustomsolutions.com
then local client points to: port 9906, and ip 127.0.0.1
lstail
This bit of bash will do an ls 100 times
for i in $(seq 1 100); do ls -alp; sleep 0.25; done
ps with extended command line
/usr/ucb/ps axww//more w for wider
Get PID of a running process using command line
(/usr/ucb/ps axww | grep "COMMAND LINE FOR PROCESS TO FIND" | awk '{print $1}')//more greps to narrow
Is a var set?
if [ -z $THRESHOLD ]
then
THRESHOLD=6500
fi
The Importance of Quotation Marks
The use of the different types of quotation marks is very important in shell programming. Both kinds of quotation marks and the backslash character are used by the shell to perform different functions. The double quotation marks (""), the single quotation marks (''), and the backslash () are all used to hide special characters from the shell. Each of these methods hides varying degrees of special characters from the shell. Remember that everything in this section also applies to the bash command line, so for example you could use a backslash to use a space in the name of a file.
Double Quotes
The double quotation marks are the least powerful of the three methods. When you surround characters with double quotes, all the whitespace characters are hidden from the shell, but all other special characters are still interpreted by the shell. This type of quoting is most useful when you are assigning strings that contain more than one word to a variable. For example, if you wanted to assign the string hello there to the variable greeting, you would type the following command: greeting="Hello there" This command would store the string "Hello there" in the variable "greeting" as one word. If you typed this command without using the quotes, you would not get the results you wanted. bash would not understand the command and would return an error message.
Single Quotes
Single quotes are the most powerful form of quoting. They hide all special characters from the shell. This is useful if the command that you enter is intended for a program other than the shell. Because the single quotes are the most powerful, you could have written the previous example using single quotes. You might not always want to do this. If the string being assigned to the greeting variable contained another variable, you would have to use the double quotes. For example, if you wanted to include the name of the user in your greeting, you would type the following command:
greeting="Hello there $LOGNAME"
This would store the string "Hello there " and the value of $LOGNAME into the variable greeting. The LOGNAME variable is a shell variable that contains the username of the person who is logged in to the system. If you tried to write this command using single quotes it wouldn't work, because the single quotes would hide the dollar sign from the shell and the shell wouldn't know that it was supposed to perform a variable substitution. Backslash
Using the backslash is the third way of hiding special characters from the shell. Like the single quotation mark method, the backslash hides all special characters from the shell, but it can hide only one character at a time, as opposed to groups of characters. You could rewrite the greeting example using the backslash instead of double quotation marks by using the following command:
greeting=Hello There
In this command, the backslash hides the space character from the shell, and the string "Hello there" is assigned to the variable "greeting".
Backslash quoting is used most often when you want to hide only a single character from the shell. This is usually done when you want to include a special character in a string. For example, if you wanted to store the price of a box of computer disks into a variable named disk_price, you would use the following command:
disk_price=$5.00
The backslash in this example would hide the dollar sign from the shell. If the backslash were not there, the shell would try to find a variable named 5 and perform a variable substitution on that variable. Assuming that no variable named 5 were defined, the shell would assign a value of .00 to the disk_price variable. This is because the shell would substitute a value of null for the $5 variable. The disk_price example could also have used single quotes to hide the dollar sign from the shell.
Back Quotes `
The back quote marks (`) perform a different function. They are used when you want to use the results of a command in another command. For example, if you wanted to set the value of the variable contents equal to the list of files in the current directory, you would type the following command:
contents=`ls`
This command would execute the ls command and store the results of the command into the contents variable. As you will see in later, this feature can be very useful when you want to write a shell program that performs some action on the results of another command.