Difference between revisions of "User talk:Bossa nesta"
Bossa nesta (talk | contribs) (→File Permission) |
Bossa nesta (talk | contribs) (→BASH SCRIPTING) |
||
Line 25: | Line 25: | ||
======================== BASH SCRIPTING ======================== | ======================== BASH SCRIPTING ======================== | ||
− | $ cal 16 2008 2>&1 >all-output.txt | + | <pre>$ cal 16 2008 2>&1 >all-output.txt |
− | run 'cal' with parameter "16 2008", err out to display/terminal, output to "all-output.txt" | + | run 'cal' with parameter "16 2008", err out to display/terminal, |
+ | output to "all-output.txt" | ||
$ cal 16 2008 2>err.txt >all-output.txt | $ cal 16 2008 2>err.txt >all-output.txt | ||
err out to "err.txt", output to file "all-output.txt" | err out to "err.txt", output to file "all-output.txt" | ||
− | grep "/bin/bash$" /etc/passwd | cut -d: -f1 | sort | mail -s "Bash users" joe@example.com | + | grep "/bin/bash$" /etc/passwd | cut -d: -f1 | |
+ | sort | mail -s "Bash users" joe@example.com | ||
1. selects all BASH users from the system account file /etc/passwd | 1. selects all BASH users from the system account file /etc/passwd | ||
Line 44: | Line 46: | ||
$ vi Nes$(date +%Y%m%d)-notes.txt | $ vi Nes$(date +%Y%m%d)-notes.txt | ||
− | creat a note with the name started with "Nes" follow by current year, month, date, e.g: "Nes20081012-note.txt' | + | creat a note with the name started with "Nes" follow by current year, |
+ | month, date, e.g: "Nes20081012-note.txt' | ||
Line 65: | Line 68: | ||
$ rm "$NAME" | $ rm "$NAME" | ||
− | = 'export' to turn variables into environment variables, so, all sub process can use the variable(s) | + | = 'export' to turn variables into environment variables, |
+ | so, all sub process can use the variable(s) | ||
$ TEST="Yes" | $ TEST="Yes" | ||
$ bash -c 'echo $TEST' | $ bash -c 'echo $TEST' | ||
Line 120: | Line 124: | ||
The test Command | The test Command | ||
− | = BASH has a built-in test command (similar to /bin/test) which can perform basic string and integer comparisons using these operators (results are returned as an exit code): | + | = BASH has a built-in test command (similar to /bin/test) |
+ | which can perform basic string and integer comparisons | ||
+ | using these operators (results are returned as an exit code): | ||
= return 0 or 1, where 0 is true, 1 is false | = return 0 or 1, where 0 is true, 1 is false | ||
Line 162: | Line 168: | ||
$ [ ! "a" != "b" ]; echo $? | $ [ ! "a" != "b" ]; echo $? | ||
1 | 1 | ||
− | |||
======================== BASH FLOW CONTROL ======================== | ======================== BASH FLOW CONTROL ======================== |
Revision as of 06:10, 14 October 2008
Mid-term Note - SPR720
Contents
================== File Permission ==================
$ chmod 7777 rpm.txt ; ll rpm.txt -rwsrwsrwt 1 BossaNesta BossaNesta 41310 2008-10-12 16:39 rpm.txt $ chmod 7000 rpm.txt ; ll rpm.txt ---S--S--T 1 BossaNesta BossaNesta 41310 2008-10-12 16:39 rpm.txt $ chmod 4234 rpm.txt ; ll rpm.txt --wS-wxr-- 1 BossaNesta BossaNesta 41310 2008-10-12 16:39 rpm.txt > set-user-id (suid) = use owner ID instead of current user ID > set-group-id (sgid) = inherit group ID from directory, = sub-dir will automatic has same sgid = even over sudo command and root ID > sticky bit = on old systems, file was not swapped out and stuck in memory = a file in that directory can be renamed or deleted only by the owner of the file/directory or the superuser.
================== BASH SCRIPTING ==================
$ cal 16 2008 2>&1 >all-output.txt run 'cal' with parameter "16 2008", err out to display/terminal, output to "all-output.txt" $ cal 16 2008 2>err.txt >all-output.txt err out to "err.txt", output to file "all-output.txt" grep "/bin/bash$" /etc/passwd | cut -d: -f1 | sort | mail -s "Bash users" joe@example.com 1. selects all BASH users from the system account file /etc/passwd 2. cuts out the user name ('cut -d: -f1') 3. sorts them into order ('sort') 4. e-mails them to joe@example.com with the subject line "Bash users". $ vi $(date +%Y)-notes.txt creat a note that start with year, forexample, "2007-note.txt", "2008-note.txt" $ vi $(date +%Y%m%d)-notes.txt creat a note with current year, month, date, e.g: "20081012-note.txt' $ vi Nes$(date +%Y%m%d)-notes.txt creat a note with the name started with "Nes" follow by current year, month, date, e.g: "Nes20081012-note.txt' = single quote is actual value/string, double or no quote is variable $ X="Test" $ echo "$X" Test $ echo '$X' $X $ echo $X Test = ALWAYS use double quote for value $ touch "test file" $ NAME="test file" $ rm $NAME rm: cannot remove `test': No such file or directory rm: cannot remove `file': No such file or directory $ rm "$NAME" = 'export' to turn variables into environment variables, so, all sub process can use the variable(s) $ TEST="Yes" $ bash -c 'echo $TEST' $ export TEST $ bash -c 'echo $TEST' Yes $ = destory/erase variables unset Var_NAME Common Environment Variables Variable ↓ Description ↓ $PATH command search paths $HOME Current user's home directory. $MAIL Current user's mailbox. $DISPLAY X window display specification. $TERM Current terminal type (used to analyze keypresses and send special codes such as colours and effects to the terminal). $SHELL Absolute pathname of the default shell for the current user. $HOSTNAME Name of the host (computer) on which the shell is executing. $PS1 Primary prompt, used by the shell to request a command from the user. $PS2 Secondary prompt, used to request additional info from the user. $PS3 3rd prompt (rarely used). $PS4 4th prompt (rarely used). = BASH automatically updates the value of certain special variables: Variable ↓ Description ↓ $? Exit status of last pipeline $$ Process ID of the current shell $! Process ID of the last background pipeline $RANDOM Random integer (usually in the range 0-327687). Retrieving Exist Status = ONLY '0' IS successfull, the rest are error $ ls /tmp >/dev/null $ echo $? 0 $ ls /temp >/dev/null ls: cannot access /temp: No such file or directory $ echo $? 2 $ exit 2 Set exit variable to '2" $ exit 2143 Set exit variable to '2143" The test Command = BASH has a built-in test command (similar to /bin/test) which can perform basic string and integer comparisons using these operators (results are returned as an exit code): = return 0 or 1, where 0 is true, 1 is false Operator Comparision type Comparison Example -eq Integer Equal $x -eq 4 -ne Integer Not equal $x -ne 4 -gt Integer Greater than $x -gt 0 -lt Integer Less than $x -lt 1000 -ge Integer Greater/equal $x -ge $y = String Equal "$x" = "Y" != String Not equal "$x" != "NEVER" Unary File Tests Operator Test Example -e File exists [ -e /etc/passwd ] -r File is readable [ -r /etc/hosts ] -w File is writable [ -w /tmp ] -x File is executable [ -x /usr/bin/ls ] -f File is a regular file [ -f /dev/tty ] -d File is a directory [ -d /dev/tty ] = For example.... $ test 10 -gt 5 $ echo $? 0 $ test 10 -lt 5 $ echo $? 1 $ [ -w /etc/passwd ] $ echo $? 1 $ a=10; [ "$a" -ge 100 -a "$a" -le 1000 ]; echo $? 1 $ [ ! "a" = "b" ]; echo $? 0 $ [ ! "a" != "b" ]; echo $? 1================== BASH FLOW CONTROL ==================
Format of 'if'
============
if pipeline then
success-commands[elif pipeline2
else-if-commands] [else
alt-commands] fi
============
CASE
echo -n "Are you sure you wish to remove '$file'?" read YN if [ "$YN" = "y" -o "$YN" = "Y" ] then
echo "Deleting '$file'..." rm "$file"else
echo "Aborted. '$file' not deleted."fi
if [ "$(date +%Y)" -lt 2010 ] thenecho "Still waiting for the Whistler Olympics."fi
Format of "while"============
while pipeline do
commandsdone
============
CASE
num=1 while [ $num -le 5 ] do
echo $num num=$[ $num + 1 ]done
CASE
- In this case it will just print number 1 to 5
============
while (( 1 )) do
eject -Tdone
============
Format of "for"
============
for COLOUR in red blue green do
print "$COLOUR"done
============
for ((x=0; x<=10; x++)) do
echo $xdone
============
for FILE in /etc/* do
if [ -x $FILE ] then echo "$FILE is executable" fidone
============
================== RPM ==================
RPM file names normally have the following format: <name>-<version>-<release>.<arch>.rpm
== Query / Verify commands
1. Getting detailed information: $ rpm -qi wget 2. Determining which package installed /usr/bin/wget: $ rpm -qf /usr/bin/wget 3. Showing all the files installed by the package wget: $ rpm -ql wget 4. Viewing the documentation files for the command wget: $ rpm -qd wget 5. Listing all files included in the not yet installed package wget by entering the following: $ rpm -qpl /mnt/iso/suse/i586/wget-1.10.2-78.i586.rpm 6. Listing all files included in the installed package wget: $ rpm -ql wget 7. Verifying that a package is no longer installed by entering: $ rpm -qa | grep wget 8. Seeing what has changed in the files on your hard drive since the wget RPM was originally installed by entering the following: $ rpm -V wget 9. Checking package to ensure its integrity and origin: (NOTE: gpg or pgp software must be installed on your system before you use this command) $ rpm -K /mnt/iso/suse/i586/wget-1.10.2-78.i586.rpm== Install / Uninstall / Upgrade commands
1. Installing the package wget: $ rpm -ivh /mnt/iso/suse/i586/wget-1.10.2-78.i586.rpm 2. Uninstalling the package wget: $ rpm -e wget 3. Upgrading the package wget: (NOTE: if the package is not installed it will install it for You, like option "-ivh") $ rpm -Uvh /mnt/iso/suse/i586/wget-1.10.2-78.i586.rpm 4. Extracting RPM file using rpm2cpio and cpio command: (NOTE: RPM content will be extracted the current directory)$ rpm2cpio wget-1.10.2-78.i586.rpm | cpio -idmv