Mohammedz.com

For Linux and Shell scripting.


Leave a comment

Restore primary partition table without overwriting the MBR

You can find this useful information from the manpage for gpart.

The gpart may be of some help when the primary partition table was lost or destroyed but it can under no circumstances replace proper disk/partition  table  backups. To save the master boot record (MBR) including the primary partition table to a file type

dd if=/dev/hda of=mbr bs=512 count=1

Exchanging /dev/hda with the block device name of the disk in question. This should be done for all disks in the system. To restore the primary partition table without overwriting the MBR type

dd if=mbr of=/dev/hda bs=1 count=64 skip=446 seek=446

Warning: make sure that all parameters are typed as shown and that the disk device is correct. Failing to do so may result in severe filesystem  corruption. The saved file should be stored in a safe place like a floppy disk.

~mohammed


Leave a comment

WordPress: password protect your posts

Do you want to protect your posts/articles with passwords? It’s much simpler than Joomla method. In wordpress, you can do it in any of the following methods.

When you Edit a post, under the Publish module (next to Visibility), click on
the Edit link. Select the Password Protected box and enter the password.

When you use Quick Edit on a post, there’s a field for the password.

~mohammed


Leave a comment

3ware raid monitoring tool installation hangs up

Did your 3ware raidmonitoring software installation hangs up ever? I have had this situation a couple of times and I found a way to get it done (of course it’s not a fix, but worked for me multiple times).

I was trying to install combined 3DM2 and CLI available at http://3ware.com/support/download.asp. It hung up without finishing and I waited for hours without any luck. I have installed 3ware raidmon on so many machines before. So, I just connected to another session of that server and tried to install raidmonitoring tool again.

# /bin/bash setupLinux_x64.bin -console

As I guessed it ended up in an error, but surprisingly the installation process resumed from the older session.

what the hell causes such an issue? Umm…I don’t know and I don’t want to spend my time on such ridiculous stuffs.

~mohammed


1 Comment

Joomla: how to reset the administrator password?

You can’t recover your Joomla password as it’s stored via MD5 method. But, you can reset it via mysql database.

Using phpMyAdmin
*********************
* Select the data base for your Joomla from left combo-box.
* Choose jos_users table and select browse.
* Edit the row where username is admin
* set the password type to MD5 under the function column
* Type in your password and save it.

MySQL commandline
***********************
* Login to your mysql
mysql -uusername -ppassword
* Select database for you joomla
use joomladb
* Change password for admin
UPDATE jos_users SET password=MD5(‘NewPassword’) WHERE username=’admin’;
* quit from mysql

That’s all !!!
~mohammed


Leave a comment

SHELL GRAMMAR: simple commands and compound commands

Simple Commands

A simple command is a sequence of optional variable assignments followed by blank-separated words and redirections, and terminated by a control operator. The first word specifies the command to be executed, and is passed as argument zero. The remaining words are passed as arguments to the invoked command.

The return value of a simple command is its exit status, or 128+n if the command is terminated by signal n.

Pipelines
A pipeline is a sequence of one or more commands separated by the character |. The format for a pipeline is:

[time [-p]] [ ! ] command [ | command2 … ]

The standard output of command is connected via a pipe to the standard input of command2. This connection is performed before any redirections specified by the command.

The return status of a pipeline is the exit status of the last command, unless the pipefail option is enabled. If pipefail is enabled, the pipeline’s return status is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands exit successfully. If the reserved word ! precedes a pipeline, the exit status of that pipeline is the logical negation of the exit status as described above. The shell waits for all commands in the pipeline to terminate before returning a value.

If the time reserved word precedes a pipeline, the elapsed as well as user and system time consumed by its execution are reported when the pipeline terminates. The -p option changes the output format to that specified by POSIX. The TIMEFORMAT variable may be set to a format string that specifies how the timing information should be displayed.

Each command in a pipeline is executed as a separate process (i.e., in a subshell).

Lists
A list is a sequence of one or more pipelines separated by one of the operators ;, &, &&, or ||, and optionally terminated by one of ;, &, or <newline>. Of these list operators, && and || have equal precedence, followed by ; and &, which have equal precedence. A sequence of one or more newlines may appear in a list instead of a semicolon to delimit commands.

If a command is terminated by the control operator &, the shell executes the command in the background in a subshell. The shell does not wait for the command to finish, and the return status is 0. Commands separated by a ; are executed sequentially; the shell waits for each command to terminate in turn. The return status is the exit status of the last command executed.

The control operators && and || denote AND lists and OR lists, respectively. An AND list has the form
command1 && command2

command2 is executed if, and only if, command1 returns an exit status of zero.

An OR list has the form
command1 || command2

command2 is executed if and only if command1 returns a non-zero exit status. The return status of AND and OR lists is the exit status of the last command executed in the list.

Compound Commands

A compound command is one of the following:

(list)
list is executed in a subshell environment. Variable assignments and builtin commands that affect the shell’s environment do not remain in effect after the command completes. The return status is the exit status of list.

{ list; }
list is simply executed in the current shell environment. list must be terminated with a newline or semicolon. This is known as a group command. The return status is the exit status of list. Note that unlike the metacharacters ( and ), { and } are reserved words and must occur where a reserved word is permitted to be recognized. Since they do not cause a word break, they must be separated from list by whitespace.

((expression))
The expression is evaluated according to the rules described below under ARITHMETIC EVALUATION. If the value of the expression is non-zero, the return status is 0; otherwise the return status is 1. This is exactly equivalent to let “expression”.

[[ expression ]]
Return a status of 0 or 1 depending on the evaluation of the conditional expression expression. Expressions are composed of the primaries described below under CONDITIONAL EXPRESSIONS. Word splitting and pathname expansion are not performed on the words between the [[ and ]]; tilde expansion, parameter and variable expansion, arithmetic expansion, command substitution, process substitution, and quote removal are performed. Conditional operators such as -f must be unquoted to be recognized as primaries.

When the == and != operators are used, the string to the right of the operator is considered a pattern and matched according to the rules described below under Pattern Matching. If the shell option nocasematch is enabled, the match is performed without regard to the case of alphabetic characters. The return value is 0 if the string matches (==) or does not match (!=) the pattern, and 1 otherwise. Any part of the pattern may be quoted to force it to be matched as a string.

An additional binary operator, =~, is available, with the same precedence as == and !=. When it is used, the string to the right of the operator is considered an extended regular expression and matched accordingly. The return value is 0 if the string matches the pattern, and 1 otherwise. If the regular expression is syntactically incorrect, the conditional expression’s return value is 2. If the shell option nocasematch is enabled, the match is performed without regard to the case of alphabetic characters. Substrings matched by parenthesized subexpressions within the regular expression are saved in the array variable BASH_REMATCH. The element of BASH_REMATCH with index 0 is the portion of the string matching the entire regular expression. The element of BASH_REMATCH with index n is the portion of the string matching the nth parenthesized subexpression.

Expressions may be combined using the following operators, listed in decreasing order of precedence:

( expression )
Returns the value of expression. This may be used to override the normal precedence of operators.

! expression
True if expression is false.

expression1 && expression2
True if both expression1 and expression2 are true.

expression1 || expression2
True if either expression1 or expression2 is true.

The && and || operators do not evaluate expression2 if the value of expression1 is sufficient to determine the return value of the entire conditional expression.

for name [ in word ] ; do list ; done
The list of words following in is expanded, generating a list of items. The variable name is set to each element of this list in turn, and list is executed each time. If the in word is omitted, the for command executes list once for each positional parameter that is set. The return status is the exit status of the last command that executes. If the expansion of the items following in results in an empty list, no commands are executed, and the return status is 0.

for (( expr1 ; expr2 ; expr3 )) ; do list ; done
First, the arithmetic expression expr1 is evaluated according to the rules described below under ARITHMETIC EVALUATION. The arithmetic expression expr2 is then evaluated repeatedly until it evaluates to zero. Each time expr2 evaluates to a non-zero value, list is executed and the arithmetic expression expr3 is evaluated. If any expression is omitted, it behaves as if it evaluates to 1. The return value is the exit status of the last command in list that is executed, or false if any of the expressions is invalid.

select name [ in word ] ; do list ; done
The list of words following in is expanded, generating a list of items. The set of expanded words is printed on the standard error, each preceded by a number. If the in word is omitted, the positional parameters are printed (see PARAMETERS below). The PS3 prompt is then displayed and a line read from the standard input. If the line consists of a number corresponding to one of the displayed words, then the value of name is set to that word. If the line is empty, the words and prompt are displayed again. If EOF is read, the command completes. Any other value read causes name to be set to null. The line read is saved in the variable REPLY. The list is executed after each selection until a break command is executed. The exit status of select is the exit status of the last command executed in list, or zero if no commands were executed.

case word in [ [(] pattern [ | pattern ] … ) list ;; ] … esac
A case command first expands word, and tries to match it against each pattern in turn, using the same matching rules as for pathname expansion. The word is expanded using tilde expansion, parameter and variable expansion, arithmetic substitution, command substitution, process substitution and quote removal. Each pattern examined is expanded using tilde expansion, parameter and variable expansion, arithmetic substitution, command substitution, and process substitution. If the shell option nocasematch is enabled, the match is performed without regard to the case of alphabetic characters. When a match is found, the corresponding list is executed. After the first match, no subsequent matches are attempted. The exit status is zero if no pattern matches. Otherwise, it is the exit status of the last command executed in list.

if list; then list; [ elif list; then list; ] … [ else list; ] fi
The if list is executed. If its exit status is zero, the then list is executed. Otherwise, each elif list is executed in turn, and if its exit status is zero, the corresponding then list is executed and the command completes. Otherwise, the else list is executed, if present. The exit status is the exit status of the last command executed, or zero if no condition tested true.

while list; do list; done
until list; do list; done

The while command continuously executes the do list as long as the last command in list returns an exit status of zero. The until command is identical to the while command, except that the test is negated; the do list is executed as long as the last command in list returns a non-zero exit status. The exit status of the while and until commands is the exit status of the last do list command executed, or zero if none was executed.

Shell Function Definitions

A shell function is an object that is called like a simple command and executes a compound command with a new set of positional parameters. Shell functions are declared as follows:

[ function ] name () compound-command [redirection]
This defines a function named name. The reserved word function is optional. If the function reserved word is supplied, the parentheses are optional. The body of the function is the compound command compound-command. That command is usually a list of commands between { and }, but may be any command listed under Compound Commands above. compound-command is executed whenever name is specified as the name of a simple command. Any redirections specified when a function is defined are performed when the function is executed. The exit status of a function definition is zero unless a syntax error occurs or a readonly function with the same name already exists. When executed, the exit status of a function is the exit status of the last command executed in the body.


Leave a comment

PDF to HTML converter in Linux

Are searching for a tool in Linux to convert PDF to HTML? The “pdftohtml” tool can do the work for you. This comes with poppler-utils package. Actually, there are two packages to be installed, poppler and poppler-utils and you can install them using yum (apt-get or whatever you are using).

# yum install poppler*

How to use?
—————————————————————–
Usage: pdftohtml [options] <PDF-file> [<html-file> <xml-file>]
-f <int> : first page to convert
-l <int> : last page to convert
-q : don’t print any messages or errors
-h : print usage information
-help : print usage information
-p : exchange .pdf links by .html
-c : generate complex document
-i : ignore images
-noframes : generate no frames
-stdout : use standard output
-zoom <fp> : zoom the pdf document (default 1.5)
-xml : output for XML post-processing
-hidden : output hidden text
-nomerge : do not merge paragraphs
-enc <string> : output text encoding name
-dev <string> : output device name for Ghostscript (png16m, jpeg etc)
-v : print copyright and version info
-opw <string> : owner password (for encrypted files)
-upw <string> : user password (for encrypted files)
-nodrm : override document DRM settings
—————————————————————–

That’s all !!!

~mohammed


Leave a comment

Execution permission to scripts…why and why not???

I had some friends asking on execution permission to scripts. As we all know, there are two ways to run a script.

1) /bin/bash program.sh
2) ./program.sh

Suppose, you have a new script program.sh WITHOUT execution permission. You can run that script using the first method </bin/bash program.sh>, but you will get permission denied error if you try with second method <./program.sh>. Why???

When you run the script like “/bin/bash program.sh”, the shell is
executing “program.sh” as an argument to “bash” program. Since “bash” is executable, the program can run even without an execution permission to program.sh.

When you run the script like “./program.sh”, the shell loads “program.sh” to the program area for execution. Since program.sh doesn’t have execution permission, it gives “Permission denied” error.

You can get the details by tracing the commands with strace (I’m pasting only the first line of strace output):

t.sh without execution permission
—————
~]$ strace ./t.sh
execve(“./t.sh”, [“./t.sh”], [/* 50 vars */]) = -1 EACCES (Permission denied)
—————

t.sh without execution permission
—————
~]$ strace bash t.sh
execve(“/bin/bash”, [“bash”, “t.sh”], [/* 50 vars */]) = 0
—————

t.sh with execution permission
—————
~]$ strace ./t.sh
execve(“./t.sh”, [“./t.sh”], [/* 50 vars */]) = 0
—————

See this part from execve manpage:
—————

NAME
execve – execute program

SYNOPSIS
#include <unistd.h>

int execve(const char *filename, char *const argv[], char *const
envp[]);

DESCRIPTION

execve() executes the program pointed to by filename. filename must be either a binary executable, or a script starting with a line of the form “#! interpreter [arg]”. In the latter case, the interpreter must be a valid pathname for an executable which is not itself a script, which will be invoked as interpreter [arg] filename.
—————

See “man execve” for more details.

~mohammed


Leave a comment

Why is my xterm scrollbar not working with screen?

The xterm won’t allow scrolling if the alternate text buffer is selected with screen. The standard definitions of termcap initialize capabilities ti and te switch to and from the alternate text buffer. You can tell screen not to use these initializations by adding the following line to your ~/.screenrc

termcapinfo xterm ti@:te@

That’s it !!!

~mohammed