Sunday, October 4, 2009

Question mark (?) in linux and shell script

- The question mark (?) in linux and shell script can be used to indicate a test for a condition. Within double parenthesis construct it can serve as an element of a trinary operator.
Example:

$ cat >question_ex.sh
var1=10
(( var2 = var1<20?0:100 ))
echo $var2

$ sh question_ex.sh
0


The command performing test operation by (( var2 = var1<20?0:100 ))
is same as,

# if [ "$var1" -lt 20 ]
# then
# var2=0
# else
# var2=100
# fi


- In a parameter substitution expression, the question mark (?) tests whether a variable has been set.
If we use following syntax,
${parameter?err_msg}, ${parameter:?err_msg}
then if parameter is set, then it's value is used, else it prints err_msg.


$ echo ${var?The variable is not set}
bash: var: The variable is not set

$ echo ${USER?The variable is not set}
Arju

In the above example in first case variable var is not set, hence error message appeared while in the second case variable USER is set and hence displays it's value.

- In case of pattern matching question mark (?) matches the preceding pattern element zero or one time. For example if we write xy?z then it matches only xz and xyz which is described in http://arjudba.blogspot.com/2009/07/list-of-metacharacters-in-regular.html. Note that with "?" filename expansion in globbing does not behave exactly like regular expression while in case of utility like sed/awk it behaves like regular expression.

10 Bangladesh Chhatra League (BCL) members rape a school girl

Before the http://arjudba.blogspot.com/2009/10/shame-on-bangladesh-girl-raped-by-bal.html here is the another pathetic history in Bangladesh performed by ten activitists of the current ruling party members.

Ten activists of Bangladesh Chhatra League (BCL) allegedly gang-raped a teenage girl Friday night and were later handed down mere punishment at village arbitration arranged to ensure their escape by local Awami League leaders.

Family sources say the AL leaders compelled the victim's father not to go for legal action and also took their signatures on three blank sheets to stop any future move to that end.

Police also released two of the accused held on the spot while handing the victim over to her father early Saturday morning.

A band of 16 youths kidnapped the girl, a class VII student of Pakhimara in Kalapara upazila, Friday night when she was returning home on a rickshaw from a Puja mandap with her cousin Nasir Uddin.
The BCL men beat up Nasir, drove him away and took the girl to a nearby garden. Ten of them raped the girl until her relatives with police rushed to the scene to rescue her around 2:00am.

Sub-inspector Kabir Hossain of Kalapara police station held two of the culprits -- Rajib and Sohag -- from the spot but freed them later after handing over the girl to her family.

Later on Sunday, local AL leaders including Kalapara upazila unit Secretary Rakibul Ahsan and Upazila Parishad Vice-Chairman Sultan Mahmud arranged the arbitration at Kalapara Girls' School in the evening.
The village arbitration in a farcical judgement fined all the 16 Tk 10,000 each and that they all be whipped 100 times each. Shame!

But four days into the grisly incident the victim's father, a poor farmer, still does not dare to file a case.
He said, "Rakibul (AL leader) and his men took signatures of me and my daughter on three blank sheets asking us not to seek justice anywhere else."

Rakibul Ahsan denied the allegation. He claims, "The youths did not rape the girl, they just made an attempt. We punished them so that they don't indulge into such activities in future."
The Kalapara AL leader also denied their political identity as BCL activists dubbing them as "spoilt brats".
Contacted, Kalapara police Officer-in-Charge (OC) Ishaq Ali said they released Rajib and Sohag as the victim's father did not file any complaint.

He says he does not know anything about the village arbitration, adding the police would not file any case of their own and "take legal steps if any complaint is filed".

The gang members were identified as Rajib, Rubel, Ratul, Pobitra, Mojibur, Omer, Rana, Sohel, Regan, Lalu, Jewel, Sohag, Bappi of Nayapara, Saiful and Munim of Madrasa Road in Kalapara upazila town and Al-Amin of Dhankhali.

Now a days this is becoming common scenarios by the ruling government party and their units.

Asterisk (*) in shell script and linux

- In the regular expression, asterisk (*) matches the preceding pattern element zero or more times. For example if we write xy*z then it matches xz, xyz, xyyz etc. However, bash itself cannot recognize Regular Expressions. Within linux and shell script different utilities like sed, awk recognize * as regular expression metacharacters.

- For general unix command like for "ls" command asterisk (*) is not used as standard regular expression. A process named globbing recognize and expand * which does not behave like regular expression. For example, in case of listing file by ls command asterisk does not match hidden file name or in another word does not match with filenames that start with a dot.

Example:

$ touch .test

$ ls *
b.txt google.doc hi

$ ls -a
. .. .test b.txt google.doc hi

Note that in the above example with "ls *" command the .test file is not appeared.

- In the context of arithmetic operations, the * denotes multiplication.
Example:

$ expr 18 \* 4
72

As * is special character so to make multiplication we need to escape it by backslash.

- A double asterisk (**) can represent the exponentiation operator in arithmetic operation.
Example:

$ let "a=6**3"

$ echo $a
216


- Another use of double asterisk (**) is to matches filenames and directories recursively.

Example:

echo "Using **"
for filename in **
do
echo "$filename"
done # Lists complete file tree, recursively.

Saturday, October 3, 2009

Shame on Bangladesh! Girl Raped by a BAL leader video hits the market

In Bangladesh, a leader of the current government party Awami League's (BAL) unit Bangladesh Chhatra League (BCL), in Pirojpur district has lured a class X student to a love trap, raped her and recorded it in cellphone.

The video footage has reached local youths through cellphone, flash drives and CDs and is also on sale in video stores.

Ahsan Kabir Mamun alias Mamun Hawlader, information and research secretary of district unit BCL, student front of ruling Awami League, admitted the rape but pinned the blame on political and business rivals for the video.

The culprit Mamun said in an emotionless voice the rape was "recorded secretly" to tarnish his political and business image.

This is the second such incident after a gang in Faridpur in July raped a schoolgirl, recorded the scene and released the footage. Faridpur police could not arrest the culprits two months into that incident.

Mamun said he was "in a relation" with the class X student of Karimunnessa Girls' High School for the last one year.

He added he met the girl on her way to school a week before the Ramadan and lured her to the house of a lawyer, an acquaintance, on Shikarpur Road to "discuss something important".

At one stage of their conversation Mamun raped the girl. He claims Monir Hossain alias 'Ganja' Monir, local locksmith Altaf Sheikh's son and a BNP activist, "secretly" recorded the rape in his cellphone.

He claims he 'deleted' the footage recorded by Monir but at the same time pins the blame on Monir for its release with the help of a cellphone repairer on Pourashava Road.

He also points finger at Alamkathi ward Jubo League leader Ramiz Sarder and district BCL joint general secretaries Sajib Khan Rana and Badsha Howlader for "spreading" the footage to sideline him in business.

"The girl is my close relative and my family would soon arrange our wedding after consultation with her family," he claims.

Asked about the matter, 'Ganja' Monir said though he is member of a rival political group, he has been a very close friend of Mamun since childhood.

Monir admits the rape and recording it but says he had done it as "instructed" by Mamun. He says he has no idea how the footage hit the market.

Many ones correspondent acted as a buyer of pornography Saturday morning and bought a CD from a video shop in the town.

Speaking anonymously, the video shop owner said the CD was available in the market soon after the Eid but he does not know who are behind it.

The family of the victim demanded exemplary punishment of everyone involved.

They said they would go for legal action, adding Mamun, an addict, is their close relative who used to tease the girl on her way to school.

Asked over phone, Officer-in-Charge of Sadar police Mir Fashiar Rahman expressed his ignorance but said he would look into the matter.

Superintendent of Police Md Shahidullah Chowdhury said that they would seize the video footage from the market and take necessary action against the persons responsible.

But it is very unfortunate that this tiger Mamun in Pirojpur still visible in front of people. Due to ruling party leader police do not arrest him. The same was true with Awami League leader Manik who celebrated his 100 times rape celebration with his caders.

What I can say here? Just shame on Bangladesh!








Exclamation mark in shell script and linux

The exclamation mark (!) in shell script just reverse the effect of test and exit status.
Example of ! about how it inverts the exit status of a command

$ cat >exit_status_check.sh
ls nofile_exist.txt
echo $?
! ls nofile_exist.txt
echo $?

$ sh exit_status_check.sh
ls: cannot access nofile_exist.txt: No such file or directory
2
ls: cannot access nofile_exist.txt: No such file or directory
0

In the example exclamation mark just reverse the exit status of ls command.

- With test operator "!" inverts the meaning of the test.

- ! is a bash keyword. With #, ! is used to determine the interpreter of shell command which is discussed on http://arjudba.blogspot.com/2009/04/what-is-sharp-bang-appear-at-first.html.

- ! also can be used as indirect variable assignment.
Example:

$ cat >indirect_ref.txt
v=variable
variable=2
echo "v = $v" # Direct reference.

echo "Now v = ${!v}" # Indirect reference.
# The ${!variable} notation is more intuitive than the old
#+ eval var1=\$$var2

$ sh indirect_ref.txt
v = variable
Now v = 2


- From the command line, the ! invokes the Bash history mechanism. Note that within a script, the history mechanism is disabled.

Related Documents

Colon (:) character in shell script and Linux

- The colon character (:) in shell script is considered as NULL command. It means do no operation (Do nothing). The ":" command is itself a Bash builtin, and its exit status is true (0).
Demonstration here,

$ :

$ echo $?
0

- The colon is used in case of while loop to make endless iteration. Example of while endless loop is,

while :
do
...
done

which is same as,

while true
do
...
done

- Colon (:) can be used as placeholder in if/then test.
Example:

if condition
then : # Do nothing and branch ahead
else # Or else ...
take-some-action
fi

After "then" the colon (:) indicates do nothing if "if condition" is satisfied and ahead branch.

- In case of, "${parameter:-default}" colon (:) makes a difference only when the parameter has been declared, but is set to null. In this case it will echo default. Example.

$ cat >colon_test.sh
# Colon makes to trigger the default optuion
#+if the variable is declared and set to null/

username=
echo "username has been declared, but is set to null."
echo "username = ${username-`whoami`}"
# This will not echo as variable is set to null and is not used colon (:).


echo "username = ${username:-`whoami`}"
# Will echo the execution of `whoami` command as we used colon (:) here.

$ sh colon_test.sh
username has been declared, but is set to null.
username =
username = Arju

$ whoami
Arju


- In case of arithmetic operations, while assigning variable ":" is necessary because otherwise Bash attempts to interpret statement as a command.
Example:

$ cat >arithmetic_colon.sh

n=1; echo -n "$n "
: $((n = $n + 1))
# Here ":" is necessary. If we don't use colon then Bash attempts
#+ to interpret "$((n = $n + 1))" as a command.
echo -n "$n "

$ sh arithmetic_colon.sh
1 2

Note that we could also increment n by 1 using "(( n = n + 1 ))" or "n=$(($n + 1))".

- Colon (:) also can be used to evaluate environmental variable. Before doing any operation it can be checked whether certain environmental variable is set.

$ cat >check_environment.sh
# Check some of the system's environmental variables.
: ${HOSTNAME?} ${USER?}

$ sh check_environment.sh

$ cat >check_environment2.sh
# Check some of the system's environmental variables.
: ${HOSTNAME?} ${USER?} ${ORACLE_HOME?}

$ sh check_environment2.sh
check_environment2.sh: line 2: ${$ORACLRACLE_HOME?}: bad substitution

In the above example see if all environmental variables were set then no error returned but as ORACLE_HOME variable was not set and so it returned error.

- In combination with the redirection operator (>) colon, truncates a file to zero length, without changing its permissions. If the file did not previously exist, creates it.
For example

$ : >new_file.txt # File "new_file.txt" is now empty.

It have the similar effect as statement cat /dev/null >new_file.txt however, this does not fork a new process, since ":" is a builtin.

If : is used with >> redirection operator, then it has no effect on a pre-existing target file (: >> target_file). If the file did not previously exist, then it creates it.

- Colon (:) can be used to comment a line but not recommended. Because with : error checking is done in the line.

- The ":" also serves as a field separator, in /etc/passwd, and in the $PATH variable.
Example:

$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin


Related Documents

Forward slash and backslash in shell script and linux

Forward Slash in Linux and shell script
- The forward slash(/) is filename path separator in linux as well as in shell script. For example if we issue pwd then you will see the current directory location and location components might be separated by forward slash.

$ pwd
/home/Arju/t_dir

- Forward slash (/) is also arithmetic operator and works do division.
Example:

$ expr 18 / 4
4

Backslash in Linux and shell script
The backslash (\) is called the escape character. If we use \X then character X has the quoting effect. So \X is equivalent to 'X' But with echo and sed command backslash with certain character may have the special meaning.
For example,

\n means newline

\r means return

\t means tab

\v means vertical tab

\b means backspace

\a means alert (beep or flash)

Related Documents