Page not found – ShopingServer Wiki https://wiki.shopingserver.com Tutorials and Articles About Technology and Gadgets Wed, 02 Sep 2020 02:18:31 +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
sed Tip: Delete All Blank White Spaces https://wiki.shopingserver.com/sed-tip-delete-blank-white-spaces/ https://wiki.shopingserver.com/sed-tip-delete-blank-white-spaces/#respond Sat, 06 Jan 2018 10:22:36 +0000 http://wiki.shopingserver.com/?p=18600 I have a text file as follows:

foo

bar

foobar

How can I delete all leading and/or trailing blank spaces, tab from each line using sed command?

 

You can use sed command to delete all white (blank) spaces in a text file. You can also use other text processing utilities such as:

Perl.

Python.

Awk and friends.

Perl example

The syntax is:

perl -lape  s/\s+//sg  input > output

Sample outputs:

foo

bar

foobar

Or use -pie syntax to update file:

cat input

perl -lapi -e  s/\s+|^\n//sg   input

cat input

See perl(1) for more information.

Sed example

The syntax is:

sed -e  s/^[ \t]*//  -e  s/[ \t]*$//  input > output

OR updated file in a single go with the -i option:

sed -i -e  s/^[ \t]*//  -e  s/[ \t]*$//  input

See sed(1) for more information.

Awk example

The syntax is

awk  {$1=$1}{ print }  input > output

You can also use gsub() substring matching the regular expression function. See awk(1) for more information.

 

 

]]>
https://wiki.shopingserver.com/sed-tip-delete-blank-white-spaces/feed/ 0
Linux: Find Out Directory Size Command https://wiki.shopingserver.com/linux-find-directory-size-command/ https://wiki.shopingserver.com/linux-find-directory-size-command/#respond Sat, 06 Jan 2018 10:13:47 +0000 http://wiki.shopingserver.com/?p=18589 I am a new Linux user. How do I find out size of a directory on Linux operating systems using command line options?

 

You need to use the du command:

[a] Find and estimate file space usage.

[b] Summarize disk usage of each FILE/Directory/Folder.

[c] Shows the sizes of directories and files.

Syntax

The basic syntax is:

du

du dirName

du [options] dirName

Examples

Without any options, du command shows the names and used space for each directories including all sub-directories in the current directory:

du

 

Sample outputs:

Fig.01: du command in action

To find information about /etc and /home/nixcraft directory, enter:

du /path/to/dir

du /etc

du /home/nixcraft

du /root /home/nixcraft

Pass the -h option to get output in human readable format i.e. show output in kilobytes (K), megabytes (M) and gigabytes (G):

du -h /etc

du -h /dir1/file2

du -h /root

du -h

 

Sample outputs:

8.0K ./.vim

24K ./scripts

48K ./.ssh

16K ./.keychain

2.2M ./.lftp

2.4M .

Pass the -s option to see the total disk space used by a directory:

du -sh

du -sh /etc/

du -sh /etc /home/ /securebackup/

 

Sample outputs:

4.1M /etc

152K /home/

902M /securebackup/

Pass the -c to see a grand total for all of the files, type:

du -csh /root/ /etc/ /home/

 

Sample outputs:

2.4M /root/

4.1M /etc/

152K /home/

6.6M total

See also

UNIX disk usage command examples – include command line tool such as du, df, ncdu, and GUI tools.

See du(1) and examples.

 

 

]]>
https://wiki.shopingserver.com/linux-find-directory-size-command/feed/ 0
Linux: Find Out How Much Disk Space Left On Hard Drive https://wiki.shopingserver.com/linux-find-much-disk-space-left-hard-drive/ https://wiki.shopingserver.com/linux-find-much-disk-space-left-hard-drive/#respond Sat, 06 Jan 2018 10:12:32 +0000 http://wiki.shopingserver.com/?p=18587 I am a desktop support professional with experience working in a corporate call center environment. Recently, I started to admin RHEL based IBM Linux server. How do I determine how much disk space left in my Linux server?

How do I find out how much disk space I have in Linux for each partition?

 

You need to use the df command. It shows the amount of disk space available on the currently mounted file system. df is used to show or find out following information:

Used and available space.

File system mount points.

File system capacity.

The number of inodes available.

Find of whether there is sufficient space to upgrade or install new apps.

Syntax

The basic syntax is as follows:

df

df /path/to/dev

df [options]

df [options] /path/to/dev

Examples

Type the following command:

# df

# df -H

 

Sample outputs:

Fig.01: df command in action

The following example will provide information only for the partition/device that contains the /home directory:

# df /home

# df -h /home

 

To see inode usage instead of block usage, type:

# df -i

# df -i /

# df -ih /

# df -i /dev/md0

 

Sample outputs:

Filesystem            Inodes   IUsed   IFree IUse% Mounted on

/dev/md0             7872512   35813 7836699    1% /

Pass the -T to find out file system type:

# df -T -h

 

Sample outputs:

Filesystem    Type    Size  Used Avail Use% Mounted on

/dev/md0      ext4    119G  1.8G  111G   2% /

tmpfs        tmpfs   1002M     0 1002M   0% /lib/init/rw

udev         tmpfs   1000M  260K 1000M   1% /dev

tmpfs        tmpfs   1002M     0 1002M   0% /dev/shm

/dev/md2      ext4    1.5T  658G  745G  47% /data

/dev/mapper/cryptvg-mybackup

ext3    591G   78G  484G  14% /securebackup

DF COMMAND OPTIONS

From the df(1):

-a, –all             include dummy file systems

-B, –block-size=SIZE  use SIZE-byte blocks

–total           produce a grand total

-h, –human-readable  print sizes in human readable format (e.g., 1K 234M 2G)

-H, –si              likewise, but use powers of 1000 not 1024

-i, –inodes          list inode information instead of block usage

-k                    like –block-size=1K

-l, –local           limit listing to local file systems

–no-sync         do not invoke sync before getting usage info (default)

-P, –portability     use the POSIX output format

–sync            invoke sync before getting usage info

-t, –type=TYPE       limit listing to file systems of type TYPE

-T, –print-type      print file system type

-x, –exclude-type=TYPE   limit listing to file systems not of type TYPE

 

 

]]>
https://wiki.shopingserver.com/linux-find-much-disk-space-left-hard-drive/feed/ 0
Awk Floating Point Number Addition Results Are Unexpected https://wiki.shopingserver.com/awk-floating-point-number-addition-results-unexpected/ https://wiki.shopingserver.com/awk-floating-point-number-addition-results-unexpected/#respond Sat, 06 Jan 2018 10:09:47 +0000 http://wiki.shopingserver.com/?p=18583 I am using awk to grep ‘foo’ from a text file and cacluate sum of field # 7. But, result is rounded to an integer. I need exact result such as 385858.66 and not 385858 using the following command:

grep ‘foo’ 2012-2013.txt | awk ‘BEGIN{ sum=0.0}{ sub(“,”,””,$7); sum +=$7}END{ print “$” sum}’

$682444

I want $682444.57 as output. How can I force “awk” to do floating point math?

 

Floating-point numbers or “real” numbers are that have a fractional part. awk uses double-precision floating-point numbers to represent all numeric values. In other words, all numbers in awk are floating-point numbers i.e. all calculation done using floating-point numbers.

Example: Awk floating point calculation

The following example uses a file called trade.txt, which contains a list of week names as well as four profit values per week:

week1 12.5 12.5 13.5 18.5

week2 11.5 11.10 12.10 13.70

week3 8.5  8.10 8.5 12.5

week4 9.5 11.5 13.5 16.5

week5 8 7 13 17

The following awk program takes the file trade.txt and prints the sum of all four values:

awk  { sum = $2 + $3 + $4 + $5; print $1, sum }  trade.txt

Sample outputs:

week1 57

week2 48.4

week3 37.6

week4 51

week5 45

The following awk program takes the file trade.txt and prints the average of all four values:

awk  { sum = $2 + $3 + $4 + $5; avg = sum/4; print $1, sum , avg}  trade.txt

Sample outputs:

week1 57 14.25

week2 48.4 12.1

week3 37.6 9.4

week4 51 12.75

week5 45 11.25

To avoid surprises use printf to format text to make your output more beautiful and meaningful:

awk  { sum = $2 + $3 + $4 + $5; avg = sum/4; printf  %s: $%.2f ($%05.2f)\n ,$1, sum, avg}  trade.txt

OR

awk  { sum = $2 + $3 + $4 + $5; avg = sum/4; printf  %s: $%.2f ($%5.2f)\n ,$1, sum, avg}  trade.txt

Sample outputs:

week1: $57.00 ($14.25)

week2: $48.40 ($12.10)

week3: $37.60 ($ 9.40)

week4: $51.00 ($12.75)

week5: $45.00 ($11.25)

To fix your problem, replace the following awk code

grep  foo  2012-2013.txt  | awk  BEGIN{ sum=0.0}{ sub( , ,  ,$7); sum +=$7}END{ print  $  sum}

with:

grep  foo  2012-2013.txt  | awk  BEGIN{ sum=0.0}{ sub( , ,  ,$7); sum +=$7}END{  printf  $%.2f\n , sum}

You can skip the grep command and use awk as follows to match and perform sum of all $7:

awk  /foo/{ sub( , ,  ,$7); sum = old + $7; old=sum}END{ printf  $%.2f\n , sum}  2013-2014.txt

Recommended readings

See awk(1) for more info.

 

 

]]>
https://wiki.shopingserver.com/awk-floating-point-number-addition-results-unexpected/feed/ 0
Nginx Redirect Mobile / Smart Phone Traffic To Mobile Version Of the Web Site https://wiki.shopingserver.com/nginx-redirect-mobile-smart-phone-traffic-mobile-version-web-site/ https://wiki.shopingserver.com/nginx-redirect-mobile-smart-phone-traffic-mobile-version-web-site/#respond Sat, 06 Jan 2018 10:08:23 +0000 http://wiki.shopingserver.com/?p=18581 I am a new nginx user and I would like to redirect all mobile / smart phone users from www.example.com to m.example.com domain. How do I detect a mobile phone browser in nginx?

How do I redirect all mobile users to sub-domain using regex based rules?

And How can I automatically redirects visitors on mobile devices to its mobile version at http://m.example.com/ and also allow mobile devices to the desktop website at www.example.com if visiting via http://www.example.com/?desktop=true?

 

You can easily redirect all mobile users using nginx as follows:

m.example.com – Mobile domain name. Make sure app is configured to display same page as served on www.example.com

All desktop clients need to use www.example.com. However, m.example.com i.e. all mobile phone user can browser desktop version if visiting via www.example.com/?desktop=true

Make sure line Disallow: /*? added to /robots.txt on www.example.com

/robots.txt – It is a good idea to block all bots on m.example.com. This ensures that mobile users will see lightweight page; but all bots refer to your main site. This is SEO feature. Sample /robots.txt for m.example.com

User-agent: *

Disallow: /

From the robots.org page:

The “User-agent: *” means this section applies to all robots. The “Disallow: /” tells the robot that it should not visit any pages on the site. Bad robots can ignore your /robots.txt.

Especially malware robots that scan the web for security vulnerabilities, and email address harvesters used by spammers will pay no attention. However, good robots such as Googlebot will follow your /robots.txt file.

Sample /robots.txt for www.example.com:

User-agent: *

Disallow: /*?

Nginx configurations

Edit the nginx.conf file and append the following after server directive:

set $mobile_rewrite do_not_perform;

 

chi http_user_agent for mobile / smart phones ##

if ($http_user_agent ~*  (android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino ) {

set $mobile_rewrite perform;

}

 

if ($http_user_agent ~*  ^(1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-) ) {

set $mobile_rewrite perform;

}

 

redirect to m.example.com ##

if ($mobile_rewrite = perform) {

rewrite ^ http://m.example.com$request_uri? redirect;

break;

}

Adding exceptions

You can allow user to browse and view desktop version of your site if url has www.example.com/?desktop=true. You can set cookie as follows:

set $force_dt_cookie    ;

 

if ($args ~  desktop=true ) {

set $mobile_rewrite do_not_perform;

set $force_dt_cookie   desktop=true ;

}

 

add_header Set-Cookie $force_dt_cookie;

 

 

if ($http_cookie ~  desktop=true ) {

set $mobile_rewrite do_not_perform;

}

Save and close the file. Restart or reload the nginx server, enter:

# /usr/sbin/nginx -s reload

 

OR

# /etc/init.d/nginx reload

Test it

Use the curl command as follows to see redirection:

curl -I -A  UserAgentString  http://www.example.com

curl -I -A  Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3  http://www.example.com

curl -I -A  Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3   http://www.example.com/?desktop=true

References

Mobile browser detection with Nginx.

Mobile redirection with Nginx.

 

 

]]>
https://wiki.shopingserver.com/nginx-redirect-mobile-smart-phone-traffic-mobile-version-web-site/feed/ 0
Linux: Log Everyone Out Of The System https://wiki.shopingserver.com/linux-log-everyone-system/ https://wiki.shopingserver.com/linux-log-everyone-system/#respond Sat, 06 Jan 2018 10:06:18 +0000 http://wiki.shopingserver.com/?p=18579 I can login as root user on Debian or Ubuntu/RHEL/CentOS Linux based system. I need to log everyone off (all ssh users) to install new kernel and/or hardware. How do I do this on Linux?

What is the best way to logout ALL USERS remotely over the ssh based session in Linux like operating systems?

 

You need to use the following commands:

a] who or w command – Show who is logging on and what they are doing.

b] pkill command – Kill user session and forcefully logout of the system.

c] shutdown command – Arranges for the system to be bring down in a safe way.

Examples

Use the who command to see list of logged in users as follows:

# w

 

OR

# who

 

Sample outputs:

root     pts/0        Jul 29 13:53 (10.1.6.120)

nixcraft pts/1        Jul 29 12:30 (10.1.6.121)

sailee   pts/2        Jul 29 12:33 (10.1.6.121)

To force and logout nixcraft and sailee user, enter:

# pkill -KILL -u nixcraft

# pkill -KILL -u sailee

 

Alternatively, just try bash and friends kung-fu and save time:

### warning must be run as root or via sudo ###

who | awk  !/root/{ cmd= /sbin/pkill -KILL -u   $1; system(cmd)}

OR

### warning must be run as root or via sudo ###

Safe version 🙂 ###

who | awk  $1 !~ /root/{ cmd= /sbin/pkill -KILL -u   $1; system(cmd)}

Finally, you can shutdown the system as follows:

# shutdown -h now

Instead of killing all users one by one you can type the following shutdown command with the warning message:

# shutdown -h +10  Server is going down for maintenance in 10 minute. Please save ALL your work ASAP and logout of the system.

Please note that this method will not work with ftp/smtp/pop3 and all other user accounts on the server. I recommend that you set maintenance windows for your server when network traffic is at a minimum or when users/client computers were not engaged in other activities on the server. For example, week-end or the period between midnight and 3:00 a.m. can be set as maintenance windows for your system.

Recommended readings

See man pages – who(1),w(1),pkill(1),shutdown(8)

 

 

]]>
https://wiki.shopingserver.com/linux-log-everyone-system/feed/ 0
Unix: csh Shell Loop Example https://wiki.shopingserver.com/unix-csh-shell-loop-example/ https://wiki.shopingserver.com/unix-csh-shell-loop-example/#respond Sat, 06 Jan 2018 10:04:59 +0000 http://wiki.shopingserver.com/?p=18577 C

an you give me a simple loop example in csh shell in Linux or Unix like operating systems?

 

The C shell (csh) or the improved version, tcsh is a Unix shell that was originally created by Bill Joy at University of California, Berkeley in the late 1970s.

Syntax

The syntax is as follows:

while ( condition )

# do something

# command 1

# command 2

end

OR

set i = 1

while ( i < 5 ) # do something till i < 5 # command 1 # command 2 @ i++ end

OR

foreach n ( 1 2 3 4 5 )

#command1

#command2

end

Examples

The following csh code will print welcome message five times on screen:

#!/bin/csh

# demoloop.csh – Sample loop script

set j = 1

while ( $j <= 5 )

echo  Welcome $j times

@ j++

end

Save and close the file. Run it as follows:

chmod +x demoloop.csh

./demoloop.csh

 

Sample outputs:

Welcome 1 times

Welcome 2 times

Welcome 3 times

Welcome 4 times

Welcome 5 times

csh foreach example

#!/bin/csh

echo  Setting name servers….

foreach i ( ns1.cyberciti.biz ns2.cyberciti.biz  )

echo $i

end

Sample outputs:

Setting name servers….

ns1.cyberciti.biz

ns2.cyberciti.biz

You can use wild card with foreach as follows:

#!/bin/csh

foreach i (*)

if (-f $i) then

echo  $i is a file.

endif

if (-d $i) then

echo  $i is a directory.

endif

end

Sample outputs:

mycal.pl is a file.

skl is a directory.

x is a file.

x.pl is a file.

y is a file.

Please note that csh was popular for many innovative features but csh has never been as popular for scripting. If you are writing system level rc scripts avoid using csh. You may want to use /bin/sh for any scripts that might have to run on other systems.

Recommended readings

tcsh(1)

 

 

]]>
https://wiki.shopingserver.com/unix-csh-shell-loop-example/feed/ 0
Bash: Display Web Page Content In Terminal https://wiki.shopingserver.com/bash-display-web-page-content-terminal/ https://wiki.shopingserver.com/bash-display-web-page-content-terminal/#respond Sat, 06 Jan 2018 09:59:47 +0000 http://wiki.shopingserver.com/?p=18569 H

ow can I fetch HTML web page content from bash and display on screen using shell utilities?

 

You can use any one of the following tool or combination of all of them to get the contents of a webpage in a shell:

[1] curl command – It is a tool to transfer data from or to a server using http/https/ftp and much more.

[2] lynx command – It is a fully-featured World Wide Web (WWW) client/browser for users running terminals.

[3] wget command – It is a free utility for non-interactive download of files from the Web. It supports HTTP, HTTPS, and FTP protocols, as well as retrieval through HTTP proxies.

[4] w3m command – It is a text based Web browser and pager.

Installation

Above tools may not be installed on your Linux or Unix like operating systems.

Note: You need to login as root user to install required tools.

Debian / Ubuntu Linux install curl, wget, lynx, and w3m

Open a terminal and and then type:

$ sudo apt-get install curl wget lynx w3m

Fedora / RHEL / CentOS Linux install curl, wget, lynx, and w3m

Open a terminal and and then type:

$ sudo yum install curl wget lynx w3m

FreeBSD Unix install curl, wget, lynx, and w3m (binary package)

Open a terminal and and then type:

$ sudo pkg_add -v -r curl lynx w3m wget

Examples

You can use curl command to download the page:

curl http://www.cyberciti.biz/

curl http://www.cyberciti.biz/faq/bash-for-loop/

Use curl and store output into a variable as follows:

page= $(curl http://www.cyberciti.biz/)

page= $(curl http://www.cyberciti.biz/faq/bash-for-loop/)

To display content use echo or printf command as follows:

echo  $page

printf  %s  $page

lynx command examples

Use the lynx command as follows:

lynx -dump www.cyberciti.biz

lynx -dump www.cyberciti.biz/faq/bash-for-loop/

The -dump option dumps the formatted output of the default document or those specified on the command line to standard output. Unlike interactive mode, all documents are processed.

wget command examples

The syntax is as follows:

wget -O – http://www.cyberciti.biz

wget -O – http://www.cyberciti.biz/faq/bash-for-loop/

OR use wget command to grab the page and store it into a variable called page:

page= $(wget -O – http://www.cyberciti.biz)

display page ##

echo  $page

or pass it to lynx / w3m ##

echo  $page  | w3m -dump -T text/html

echo  $page  | lynx -dump -stdin

w3m command examples

The syntax is as follows to dump web page content in terminal using the w3m command:

w3m -dump http://www.cyberciti.biz/

w3m -dump http://www.cyberciti.biz/faq/bash-for-loop/

OR use w3m command to grab the page and store it into a variable called page:

page= $(w3m -dump http://www.cyberciti.biz/)

echo  $page

Practical examples

Get the definition of linux from a dictionary:

$ curl dict://dict.org/d:linux

 

Sample outputs:

220 pan.alephnull.com dictd 1.12.0/rf on Linux 3.0.0-14-server  <21853866.27331.1375614736@pan.alephnull.com>

250 ok

150 1 definitions retrieved

151  linux  wn  WordNet (r) 3.0 (2006)

Linux

n 1: an open-source version of the UNIX operating system

.

250 ok [d/m/c = 1/0/30; 0.000r 0.000u 0.000s]

221 bye [d/m/c = 0/0/0; 0.000r 0.000u 0.000s]

Backup your del.icio.us bookmarks:

$ wget –user=Your-Username-Here –password=Your-Password-Here https://api.del.icio.us/v1/posts/all -O my-old-bookmarks.xml

$ more my-old-bookmarks.xml

 

Grab all .mp3s from url:

mp3=$(lynx -dump http://server1.cyberciti.biz/media/index.html  | grep  http://  | awk  /mp3/{print $2} )

for i in $mp3

wget $i

done

See also

See man pages for more info – curl(1),w3m(1),lynx(1),wget(1)

 

 

]]>
https://wiki.shopingserver.com/bash-display-web-page-content-terminal/feed/ 0
Linux / Unix: Shell Script Find Out In Which Directory Script File Resides https://wiki.shopingserver.com/linux-unix-shell-script-find-directory-script-file-resides/ https://wiki.shopingserver.com/linux-unix-shell-script-find-directory-script-file-resides/#respond Sat, 06 Jan 2018 09:58:13 +0000 http://wiki.shopingserver.com/?p=18567 I need to find out in which directory my bash script resides so that I can read config file called .backup .ignore .target. For example, if my script resides in >/home/foo/script.sh, I need to read /home/foo/.{backup,ignore,target} files.

How do I find out the current directory location and shell script directory location in Bash running on Linux or Unix like operating systems?

 

You can use any one of the following method to find out the portion of pathname:

basename command – Display filename portion of pathname.

dirname command – Display directory portion of pathname.

Bash parameter substitution.

$0 expands to the name of the shell or shell script.

Examples: Shell script find out which directory the script file resides

The following example display directory path or portion of /home/nixcraft/scripts/foo.sh:

dirname /home/nixcraft/scripts/foo.sh

Sample outputs:

/home/nixcraft/scripts

The following line sets the shell variable i to /home/nixcraft/scripts:

i=dirname /home/nixcraft/scripts/foo.sh

echo  $i

OR

i=$(dirname /home/nixcraft/scripts/foo.sh)

echo  $i

In bash script use $0 instead of /home/nixcraft/scripts/foo.sh:

#!/bin/bash

script= $0

basename= $(dirname $script)

 

echo  Script name $script resides in $basename directory.

Sample outputs:

Script name /tmp/test.sh resides in /tmp directory.

Using bash shell ${var%pattern} syntax

To Remove from shortest rear (end) pattern use the following syntax:

var=${path%/*}

For example:

x= /Users/nixcraft/scripts/bar.sh

echo  ${x%/*}

y= ${x%/*}

echo  $y

An updated version of the above script:

#!/bin/bash

# Purpose : Linux / Unix shell script find out which directory this script file resides

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

# ————————————————————————————-

script= $0

basename= ${script%/*}

config1= ${basename}/.backup

config2= ${basename}/.ignore

config3= ${basename}/.target

 

echo  Script name $script resides in $basename directory.

echo  Reading config file $config1 $config2 $config3, please wait…

Run it as:

$ chmod +x /tmp/test.sh

$ /tmp/test.sh

 

Sample outputs:

Fig.01 Sample run from test.sh

A note about finding physical or real path

You may not get a real physical path and real path may be a symbolic link. To get physical path use realpath command. The realpath command uses the realpath() function to resolve all symbolic links, extra / characters and references to /./ and /../ in path. This is useful for shell scripting and security related applications.

Another recommended option is to use the readlink command to display value of a symbolic link or canonical file name:

#!/bin/bash

# Purpose : Linux / Unix shell script find out which directory this script file resides

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

# ————————————————————————————-

 

Who am i? ##

Get real path ##

_script= $(readlink -f ${BASH_SOURCE[0]})

 

Delete last component from $_script ##

_mydir= $(dirname $_script)

 

Delete /path/to/dir/ component from $_script ##

_myfile= $(basename $_script)

echo  Script : $_script

echo  Directory portion of $_script : $_mydir

echo  Filename portion of $_script : $_myfile

Save and close the file. Run it as follows:

./demo.bash

cd /home/vivek/

../../tmp/demo.bash

/tmp/demo.bash

Sample outputs:

Fig.02: Finding real path

See also

See man pages for more info – basename(1), dirname(1), bash(1)

 

 

]]>
https://wiki.shopingserver.com/linux-unix-shell-script-find-directory-script-file-resides/feed/ 0