≡ Menu


Bash source Command: $0 Can Not Determine Its Own Location

The source command read and execute commands from a file. My parent.sh script source a script called child.sh. But, child.sh script can not determine it's own location using $0. By default, $0 gives parent script location. How do I find out a script that is sourced at the command line can determine it's own location? My sample script:

# /var/www/html/java/tasks/child.sh 
# I want /var/www/html/java/tasks in _currentdir 
echo "${_currentdir}"

When I run from parent.sh:

source child.sh


source /var/www/html/java/tasks/child.sh

The $_currentdir is set to be the directory where parent.sh lives which is in /root/scripts/java/helper or . (current) directory. How do I fix this problem?
[click to continue…]

Find Linux RAM Information Command

How do I find out ram information under Linux operating systems?
[click to continue…]

Bash Shell: Find Out If a Variable Is Set or Not

How do I check if a bash shell variable called $input is defined or not under BSD / Apple OS X / Unix / Linux like operating systems?
[click to continue…]

Bash Iterate Array Examples

How do I iterate through an array under Bash scripting?
[click to continue…]

Linux Software RAID – Failed to RUN_ARRAY /dev/md0 invalid argument Error and Solution

When I run the following command at shell prompt on Debian or Ubuntu Linux

mdadm -Ac partitions /dev/md0 -m dev

I'm getting the message/warning that read as follows:

Failed to RUN_ARRAY /dev/md0 invalid argument.

How do I fix this issue? How do I rebuild array again on Linux operating system using mdadm command?
[click to continue…]

Bash For Loop Array: Iterate Through Array Values

How do I use bash for loop to iterate thought array values under UNIX / Linux operating systems?
[click to continue…]

Bash Get All Command Line Arguments Before Last Parameter In $@

I'm writing a wrapper bash shell script that will get the last argument (a domain name) from the command line into a shell variable called $_domain. I need to find all other parameters before last parameter in $@ and stored in a shell variable called $allargs. So that I can pass them as follows:

/path/to/real/binary "$allargs" "$_domain"

How do I do this using bash shell under Unix like operating systems?
[click to continue…]

Perl Display And Pass Command Line Arguments With @argv

How do I read or display command-line arguments with Perl?
[click to continue…]