≡ Menu

shell command

Bash: Assign Output of Shell Command To Variable

How do I assign the output of a shell command to a shell variable under Unix like operating system? For example, I want to store the date command output to a variable called $now. How do you do that?
[click to continue…]

Bash C Style For Loop Example and Syntax

How do I use the bash C style for loop under UNIX or Linux operating systems?
[click to continue…]

Bash Shell Read a Line Field By Field

How do I read a file field-by-field under UNIX / Linux / BSD Bash shell? My sample input data file is as follows:

device1,deviceType,major,minor,permissions
device2,deviceType,major,minor,permissions
...
....
.
deviceN,deviceTypeN,major,minor,permissions

For each line I need to construct and execute a shell command as follows:
/path/to/deviceMaker --context=$1 -m $permissions $device2 $deviceType $major $minor
[click to continue…]

How do I display shell command history with date and time under UNIX or Linux operating systems?
[click to continue…]

How do I check if file is empty or not using bash or ksh shell script under UNIX / Linux / OS X / BSD operating systems?
[click to continue…]

Q. I'd like to find out if xyz package is available or not using apt-get command. How do I look up package names and contents via shell command?

A. You need to use apt-cache command to performs a variety of operations on APT’s package cache. apt-cache does not manipulate the state of the system but does provide operations to search and generate interesting output from the package metadata.

apt-cache search {package} command

search option performs a full text search on all available package lists for the regex pattern given. It searches the package names and the descriptions for an occurrence of the regular expression and prints out the package name and the short description. For example search for lighttpd package, enter:
$ apt-cache search lighttpd
Output:

lighttpd - A fast webserver with minimal memory footprint
lighttpd-doc - Documentation for lighttpd
lighttpd-mod-cml - Cache meta language module for lighttpd
lighttpd-mod-magnet - Control the request handling module for lighttpd
lighttpd-mod-mysql-vhost - MySQL-based virtual host configuration for lighttpd
lighttpd-mod-trigger-b4-dl - Anti-deep-linking module for lighttpd
lighttpd-mod-webdav - WebDAV module for lighttpd
php5-xcache - Fast, stable PHP opcode cacher

Download and Install csh / tcsh shell on Linux

Q. cshell is a shell (command interpreter) with C-like syntax. How do I download and install csh shell on Linux?

A. csh is a command language interpreter incorporating a history mechanism , job control facilities , interactive file name and user name completion, and a C-like syntax. It is used both as an interactive login shell and a shell script command processor. tcsh is an enhanced but completely compatible version of the Berkeley UNIX C shell. In most cases csh / tcsh is installed by default. All you have to do is type shell name to start using it:
$ csh
OR
$ tcsh

Install csh / tcsh

In case csh is not installed, type the following command at shell prompt:

$ sudo apt-get install csh
If you are using Redhat / Fedora / CentOS Linux, enter the following command:
# yum install tcsh

To make csh as default login shell, type the following command:
$ which csh
Output:

/bin/csh

$ chsh