Page not found – ShopingServer Wiki https://wiki.shopingserver.com Tutorials and Articles About Technology and Gadgets Wed, 02 Sep 2020 02:35:58 +0000 en-US hourly 1 https://wordpress.org/?v=5.5.14 https://wiki.shopingserver.com/wp-content/uploads/2018/07/cropped-favicon-150x150.png Page not found – ShopingServer Wiki https://wiki.shopingserver.com 32 32 Unix and Linux: Redirect Error Output To null Command https://wiki.shopingserver.com/unix-linux-redirect-error-output-null-command/ https://wiki.shopingserver.com/unix-linux-redirect-error-output-null-command/#respond Sat, 06 Jan 2018 10:23:51 +0000 http://wiki.shopingserver.com/?p=18602 I

‘m a new Linux system user. How can I redirect command error output /dev/null on a Linux or Unix-like system using Bash shell?

 

Your shell comes with three file descriptors as follows:

stdin – 0 – Standard Input (usually keyboard or file)

stdout – 1 – Standard Output (usually screen)

stderr – 2 – Standard Error (usually screen)

What is a null (/dev/null) file in a Linux or Unix-like systems?

/dev/null is nothing but a special file that discards all data written to it. The length of the null device is always zero. In this example, first, send output of date command to the screen and later to the /dev/null i.e. discards date command output:

Show on screen ###

date

 

Discards date command output ###

date > /dev/null

Syntax: Standard Error (stderr -2 no) to a file or /dev/null

The syntax is as follows:

command 2>/dev/null

command arg1 arg2 2>/dev/null

date bar 2>/dev/null

ls -foo 2>/dev/null

In this example, send output of find command to /dev/null:

$ find /etc -type f -name  *  2>/dev/null

 

The following example will cause the stderr ouput of a program to be written to a file called errors.txt:

$ find /etc/ -type f -name  *  2> errors.txt

Linux and Unix redirect all output and error to file

The syntax is:

send command output to output.txt and error message to error.txt ##

command > output.txt 2> error.txt

command  -arg1 -arg2 > output.txt 2> error.txt

If you want both stderr and stdout in same file, try:

command > log.txt 2>&1

Use cat command to display log.txt on screen:

cat log.txt

See man pages for more information – bash(1),ksh(1).

 

 

]]>
https://wiki.shopingserver.com/unix-linux-redirect-error-output-null-command/feed/ 0
CentOS / RHEL: Install KornShell (KSH) https://wiki.shopingserver.com/centos-rhel-install-kornshell-ksh/ https://wiki.shopingserver.com/centos-rhel-install-kornshell-ksh/#respond Sat, 06 Jan 2018 09:56:33 +0000 http://wiki.shopingserver.com/?p=18565 I

am porting ksh script from Sun/Oracle Unix to Linux. How do I install ksh (KornShell) in CentOS / Fedora / Red Hat Enterprise Linux? How do I run and test ksh script on RHEL/CentOS Linux?

 

KSH was developed by David Korn at Bell Labs in 1980s. KSH is is quite popular is quite loved by sysadmins to automate everyday tasks on Unix like operating systems. You can install ksh on CentOS / RHEL. KSH-93 is the most recent version of the KornShell. It is a shell programming language, which is upward compatible with “sh” (the Bourne Shell).

Steps to install ksh in Linux

Open the Terminal app.

Type the ‘yum install ksh‘ command on CentOS/RHEL.

Type the ‘dnf install ksh‘ command on Fedora Linux.

Update your shell in /etc/passwd

Start using your ksh shell.

Let us see steps in detailed to install KornShell (KSH) on a RHEL/CentOS Linux based system.

How to install the Korn Shell (KSH)

Open a terminal and then type the following yum command to install the ksh shell:

$ sudo yum install ksh

 

OR

# yum install ksh

 

Sample outputs:

Loaded plugins: auto-update-debuginfo, protectbase, rhnplugin, security

This system is receiving updates from RHN Classic or RHN Satellite.

0 packages excluded due to repository protections

Setting up Install Process

Resolving Dependencies

–> Running transaction check

—> Package ksh.x86_64 0:20100621-19.el6_4.4 will be installed

–> Finished Dependency Resolution

 

Dependencies Resolved

 

================================================================================

Package  Arch        Version                   Repository                 Size

================================================================================

Installing:

ksh      x86_64      20100621-19.el6_4.4       rhel-x86_64-server-6      687 k

 

Transaction Summary

================================================================================

Install       1 Package(s)

 

Total download size: 687 k

Installed size: 0

Is this ok [y/N]: y

Downloading Packages:

ksh-20100621-19.el6_4.4.x86_64.rpm                       | 687 kB     00:00

Running rpm_check_debug

Running Transaction Test

Transaction Test Succeeded

Running Transaction

Installing : ksh-20100621-19.el6_4.4.x86_64                               1/1

Verifying  : ksh-20100621-19.el6_4.4.x86_64                               1/1

 

Installed:

ksh.x86_64 0:20100621-19.el6_4.4

 

Complete!

A note about Fedora Linux users

Type the following command to install ksh on Fedora Linux:

$ sudo dnf install ksh

How can I find out path for ksh shell?

To find out path to the ksh, type:

$ whereis ksh

 

OR use the grep command as follows:

$ grep –color ksh /etc/shells

 

Sample outputs:

Fig.01: Finding ksh path

 

/bin/ksh is now added to list of acceptable shells, verify it with the cat command:

$ cat /etc/shells

 

Sample outputs:

/bin/bash

/bin/csh

/bin/ksh

/bin/sh

/bin/tcsh

/bin/zsh

How do I set ksh as a default shell?

The superuser (root) may change the login shell for any account using any one of the following syntax:

$ sudo chsh -s /bin/ksh UserNameHere

 

OR

# chsh -s /bin/ksh UserNameHere

 

In this example, set default login shell to /bin/ksh for nixcraft user:

# chsh -s /bin/ksh nixcraft

 

Regular user can type the following command to change their shell to the ksh:

$ chsh -s /bin/ksh

 

Logout and login again. Verify your shell with the following command:

$ echo $SHELL

 

Sample outputs:

/bin/ksh

To see ksh version, type:

$ ksh –version

 

Sample outputs:

version         sh (AT&T Research) 93t+ 2010-06-21

Sample ksh program

Create a file called test.ksh using a text editor:

#!/bin/ksh

# Name: test.ksh

# Purpose: My first ksh script

# Author: nixCraft <www.cyberciti.biz> under GPL v2.x+

# ————————————————————————

# set variables

FILE= /etc/passwd

NOW= $(date)

HOSTNAME= hostname

USERS_ACCOUNT= $(wc -l $FILE)

 

# Greet user

print  Hi, $USER. I m $0. I m $SHELL script running on $HOSTNAME at $NOW.

print

print  *** User accounts: $USERS_ACCOUNT

print  *** Current working directory: $PWD

 

print  *** Running for loop test just for fun:

for x in {1..3}

do

print  Welcome $x times.

done

Save and close the file. Run it as follows:

chmod +x test.sh

./test.sh

Sample outputs:

Fig.02 Ksh script in action.

See also:

Debian / Ubuntu Linux: Install KSH

See man pages for more info ksh(1),grep(1),whereis(1)

 

 

]]>
https://wiki.shopingserver.com/centos-rhel-install-kornshell-ksh/feed/ 0
Linux/Unix: Find Command Ignore Case Insensitive Search https://wiki.shopingserver.com/linux-unix-find-command-ignore-case-insensitive-search/ https://wiki.shopingserver.com/linux-unix-find-command-ignore-case-insensitive-search/#respond Sat, 06 Jan 2018 08:10:24 +0000 http://wiki.shopingserver.com/?p=18441 I

am a new Linux and Unix-command line user. I am using find command to search file called “fooBar.conf.sample” in my home directory. I do not know the case, it could be uppercase, lowercase, or a mix of both. How can search a file and ignore case on a Linux or Unix-like system?

 

The find command recursively descends the directory tree for each path provided, evaluating an expression. It is mainly used to search files and directories on Linux and Unix-like systems. The syntax is as follows to search files according to given criteria. You can search for files by name, owner, group, type, permissions, date, and other criteria:

find dir-to-look criteria what-to-do

OR

find [options] dir-to-look criteria what-to-do

In this example, search your $HOME for a file called hello.c:

find $HOME -name  hello.c  -print

This will search the whole $HOME (i.e. /home/username/) system for any files named “hello.c” and display their pathnames:

/Users/vivek/Downloads/hello.c

/Users/vivek/hello.c

However, it will not match HELLO.C or HellO.C. To match is case insensitive pass the -iname option as follows:

find $HOME -iname  hello.c  -print

Sample outputs:

/Users/vivek/Downloads/hello.c

/Users/vivek/Downloads/Y/Hello.C

/Users/vivek/Downloads/Z/HELLO.c

/Users/vivek/hello.c

Finally, pass the -type f option to only search for files:

find /dir/to/search -type f -iname  fooBar.conf.sample  -print

find $HOME -type f -iname  fooBar.conf.sample  -print

A note about AIX/HP-UX and other old Unix-like systems

The -iname works either on GNU or BSD (including OS X) version find command. If your version of find command does not supports -iname, try the following syntax using grep command:

find $HOME | grep -i  hello.c

find $HOME -name  *  -print | grep -i  hello.c

OR try

find $HOME -name  [hH][eE][lL][lL][oO].[cC]  -print

Sample outputs:

/Users/vivek/Downloads/Z/HELLO.C

/Users/vivek/Downloads/Z/HEllO.c

/Users/vivek/Downloads/hello.c

/Users/vivek/hello.c

See also

Solaris UNIX Case-Insensitive Find File Search

See all find command examples from our /faq/ sections.

Man pages – find(1),grep(1)

 

 

]]>
https://wiki.shopingserver.com/linux-unix-find-command-ignore-case-insensitive-search/feed/ 0
Unix: Find Directory Name From Path https://wiki.shopingserver.com/unix-find-directory-name-path/ https://wiki.shopingserver.com/unix-find-directory-name-path/#respond Sat, 06 Jan 2018 08:04:16 +0000 http://wiki.shopingserver.com/?p=18433 I

am a new Unix shell programer. How can I extract the directory name from a full path (say /nas01/data/backups/file.tar.gz)? How do I get directory name from its path on a Linux or Unix-like system?

 

You need to use the ”dirname” command[/donotprint]In other words, you can extract the directory name using dirname command.

Method #1: Use dirname command to get directory name

The syntax is:

dirname /path/to/file

OR

VAR=”$(dirname /path/to/my/myname.txt)”

OR

FOO= /path/to/my/folder/filename.avi

OUT= $(dirname ${FOO})

Examples

The following example displays output /nas01/data/backups:

dirname /nas01/data/backups/file.tar.gz

Sample outputs:

/nas01/data/backups

The following line sets the shell variable SRC to /nas01/data/backups:

SRC= $(dirname /nas01/data/backups/file.tar.gz)

echo  Dirpath – $SRC

Sample outputs:

Dirpath – /nas01/data/backups

Method #2: Extract the directory name from a full path using bash/ksh shell

The $ character is used for parameter expansion, and command substitution. You can use it for manipulating and/or expanding variables on demands without using external commands such as sed or awk. To remove from shortest rear (end) pattern:

${VAR%/*}

VAL= ${PATHNAME%/*}

In this example, set FILE to /nas01/data/backups/demo.avi:

FILE= /nas01/data/backups/demo.avi

echo  \$FILE = $FILE

To extract the directory name, type:

echo  ${FILE%/*}

# OR store to DIR #

DIR= ${FILE%/*}

echo  Dirpath – $DIR

Sample outputs:

Dirpath – /nas01/data/backups

See man pages dirname(1),bash(1),ksh(1) for more info.

 

 

]]>
https://wiki.shopingserver.com/unix-find-directory-name-path/feed/ 0
Unix / Linux: Initialize Dot Files Without Restarting The Current Shell Session https://wiki.shopingserver.com/unix-linux-initialize-dot-files-without-restarting-current-shell-session/ https://wiki.shopingserver.com/unix-linux-initialize-dot-files-without-restarting-current-shell-session/#respond Fri, 05 Jan 2018 16:03:32 +0000 http://wiki.shopingserver.com/?p=18369 I

‘m a new Linux, OS X or Unix-like system users. I’m using bash shell. I made changes to my $HOME/.profile ($HOME/.bash_profile) file. How can I restart my session without doing logout and login again?

 

You can always initialize your shell startup files without restarting a Unix or Linux based session. The command and input file name depends upon your shell you are using. You can use the following command to find out your shell name:

echo $SHELL

Sample outputs:

/bin/csh

Restart a Unix/Linux session based on ksh or bash shell

The syntax is:

. $HOME/.profile

. $HOME/.bash_profile

 

or Use source command ##

source $HOME/.profile

source $HOME/.bash_profile

The source or . is a shell builtin commands to read and execute commands from given FILENAM. If you have created a $HOME/.bashrc file, type:

. $HOME/.bashrc

 

or

 

source $HOME/.bashrc

Restart a Unix/Linux session based on csh or tcsh shell

The syntax is as follows for csh/tcsh based shell:

source .cshrc

source .login

For more info see your shell man pages – bash(1),tcsh(1),ksh(1) or type the following command:

help source

help .

 

 

]]>
https://wiki.shopingserver.com/unix-linux-initialize-dot-files-without-restarting-current-shell-session/feed/ 0
How to assign a grep command value to a variable in Linux/Unix https://wiki.shopingserver.com/assign-grep-command-value-variable-linux-unix/ https://wiki.shopingserver.com/assign-grep-command-value-variable-linux-unix/#respond Thu, 04 Jan 2018 07:01:47 +0000 http://wiki.shopingserver.com/?p=18018 H

ow do I store grep command output in shell variable? What is the syntax to store the command output into a variable in Linux or Unix?

 

You can use the grep command for any given input files, selecting lines that match one or more patterns. By default the output shown on screen. But, you can store output to variable in your shell scripts.

Syntax: Command substitution

Command substitution means nothing more but to run a shell command and store its output to a variable or display back using echo command. The syntax is:

VAR=command-name

VAR= grep word /path/to/file

or ##

 

VAR=$(command-name)

VAR= $(grep word /path/to/file)

Examples

To display date and time using echo command:

echo  Today is $(date)

or ##

echo  Today is date

You can store command output to a shell variable using the following syntax:

To store current date and time to a variable called todays:

todays=$(date)

You can display value of $todays, enter:

echo  $todays

In this example use grep command to search for a username called vivek and store output to a variable called myuser:

myuser= $(grep  ^vivek  /etc/passwd)

echo  $myuser

Sample outputs:

Fig.01: grep store output to shell variable and echo back on screen

 

You can store the output of a grep command in a variable at the same time as printing the output using the following tee command based syntax:

foo= $(grep  ^vivek  /etc/passwd | tee /dev/tty)

echo  $foo

This is useful to direct output from a grep command to the shell variable and display on screen at the same time.

 

 

]]>
https://wiki.shopingserver.com/assign-grep-command-value-variable-linux-unix/feed/ 0
How to use sed to find and replace text in files in Linux / Unix shell https://wiki.shopingserver.com/use-sed-find-replace-text-files-linux-unix-shell/ https://wiki.shopingserver.com/use-sed-find-replace-text-files-linux-unix-shell/#respond Wed, 03 Jan 2018 14:03:21 +0000 http://wiki.shopingserver.com/?p=17852 I

am a new Linux user. I wanted to find the text called “foo” and replaced to “bar” in the file named “hosts.txt.” How do I use the sed command to find and replace on Linux or UNIX-like system?

 

 

The sed stands for stream editor. It reads the given file, modifying the input as specified by a list of sed commands. By default, the input is written to the screen, but you can force to update file.

Syntax

The syntax is:

sed  s/word1/word2/g  input.file

sed  s/word1/word2/g  input.file > output.file

sed -i  s/word1/word2/g  input.file

sed -i -e  s/word1/word2/g  -e  s/xx/yy/g  input.file

 

The above replace all occurrences of characters in word1 in the pattern space with the corresponding characters from word2.

Examples that use sed to find and replace

Let us create a text file called hello.txt as follows:

$ cat hello.txt

The is a test file created by nixCrft for demo purpose.

foo is good.

Foo is nice.

I love FOO.

 

I am going to use s/ for substitute the found expression foo with bar as follows:

sed  s/foo/bar/g  hello.txt

 

Sample outputs:

The is a test file created by nixCrft for demo purpose.

bar is good.

Foo is nice.

I love FOO.

To update file pass the -i option:

sed -i  s/foo/bar/g  hello.txt

cat hello.txt

 

The g/ means global replace i.e. find all occurrences of foo and replace with bar using sed. If you removed the /g only first occurrence is changed:

sed -i  s/foo/bar/  hello.txt

 

The / act as delimiter characters. To match all cases of foo (foo, FOO, Foo, FoO) add I (capitalized I) option as follows:

sed -i  s/foo/bar/gI  hello.txt

cat hello.txt

 

Sample outputs:

The is a test file created by nixCrft for demo purpose.

bar is good.

bar is nice.

I love bar.

Please note that the BSD implementation of sed (FreeBSD/MacOS and co) does NOT support case-insensitive matching. You need to install gnu sed. Run the following command on Apple Mac OS:

$ brew install gnu-sed

################################

now use gsed command as follows ##

################################

$ sed -i  s/foo/bar/gI  hello.txt

$ cat hello.txt

sed command problems

Consider the following text file:

$ cat input.txt

http:// is outdate.

Consider using https:// for all your needs.

 

Find word ‘http://’ and replace with ‘https://www.cyberciti.biz’:

sed  s/http:///https://www.cyberciti.biz/g  input.txt

 

You will get an error that read as follows:

sed: 1:  s/http:///https://www.c … : bad flag in substitute command:  /

Our syntax is correct but the / delimiter character is also part of word1 and word2 in above example. Sed command allows you to change the delimiter / to something else. So I am going to use +:

sed  s+http://+https://www.cyberciti.biz+g  input.txt

 

Sample outputs:

https://www.cyberciti.biz is outdate.

Consider using https:// for all your needs.

Recap: Using sed to find and replace

The general syntax is:

find word1 and replace with word2 using sed ##

sed -i  s/word1/word2/g  input

you can change the delimiter to keep syntax simple ##

sed -i  s+word1+word2+g  input

sed -i  s_word1_word2_g  input

you can add I option to GNU sed to case insensitive search ##

sed -i  s/word1/word2/gI  input

sed -i  s_word1_word2_gI  input

 

 

]]>
https://wiki.shopingserver.com/use-sed-find-replace-text-files-linux-unix-shell/feed/ 0