Category Archives: bash

PHPHOST BLOG

Web Hosting Related Articles You May Need

[one-liner]: Debugging Bash Scripts

Background

From time to time it’s useful if you can turn up the debugging messages that come from Bash, when working out either interactive or shell script problems. Here are 2 methods that can help in getting down to the details.

Solution

There are essentially 2 methods.

Method #1: -x method

When writing a shell script you’ll sometimes want to […]

Continue reading

Posted in bash, one-liner, script, shell, Syndicated, tips & tricks | Comments Off on [one-liner]: Debugging Bash Scripts

[one-liner]: How do you capture the status of a command ($?) in Bash, when run through a pipe?

Background

While answering questions on the stackexchage website Unix & Linux I saw the following question which was about something I’d encountered, but until today never knew how to accomplish, so I’m posting it here for my own reference in the future.

The question?

How do you get the exit status ( $? ) from the command haconf […]

Continue reading

Posted in bash, one-liner, script, shell, Syndicated, tips & tricks | Comments Off on [one-liner]: How do you capture the status of a command ($?) in Bash, when run through a pipe?

[one-liner]: Overview of Bash I/O Redirection

Background

I recently came across this question on the Stackexchange site Unix & Linux. This question was interesting in the sense that it covered much of the I/O redirection facilities that are available in the Bash Shell, so for posterity sake I’m adding my answer to this question here on my blog.

Solution

a number 1 = standard […]

Continue reading

Posted in bash, one-liner, script, shell, Syndicated, tips & tricks | Comments Off on [one-liner]: Overview of Bash I/O Redirection

[one-liner]: Using scp to Copy Files that Contain Spaces

Background

Have you ever needed to copy either a single file or a directory that contains spaces using scp? Here are a couple of techniques for how to do this.

Solution

Here are several ways to accomplish the same thing. Copying a entire directory with spaces and copying a file with spaces using scp.

1
2
3
4
5
6
7
# Ways to copy entire […] Continue reading

Posted in bash, one-liner, script, Syndicated, sysadmin, tip, tips & tricks | Comments Off on [one-liner]: Using scp to Copy Files that Contain Spaces

Example Bash Script which Monitors if a Program has Died, and Restarts it

Background

Here’s a quick script that might prove useful if you need to watch if a program is running and restart it if it stops for whatever reason. It is by no means fault tolerant, but could be adapted to be more so if needed.

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/bin/bash
 
# NAME: check_dropbox.bash
# DESC: watch if dropbox is running
 
check_process() {
[…] Continue reading

Posted in bash, monitoring, script, Syndicated, tip, tutorials | Comments Off on Example Bash Script which Monitors if a Program has Died, and Restarts it

[one-liner]: Calculating & Sorting Disk Usage Using du

Background

Occasionally I want to calculate how much disk space is being consumed with a directory. Here are a couple of simple yet powerful one-liners that achieve this.

Solutions

method #1

This first choice is inefficient in the sense that it basically runs du twice. Once to generate a sorted list of the directories & files by size and […] Continue reading

Posted in bash, one-liner, shell, Syndicated, tip, tips & tricks | Comments Off on [one-liner]: Calculating & Sorting Disk Usage Using du

[one-liner]: Dynamic Variables Names in Bash

Background

I hate when people make little shell scripts that all do essentially the same thing. For example:

1
2
3
4
process_moe_dir.bash
process_larry_dir.bash
process_curly_dir.bash
process_schemp_dir.bash

Here’s a technique to roll these all into a single shell script that you can just call with a command line argument.

Solution

Instead of creating the 4 scripts above, we’re going to create just one script like this one […] Continue reading

Posted in bash, one-liner, Syndicated, tip, tips & tricks | Comments Off on [one-liner]: Dynamic Variables Names in Bash

[one-liner]: How to Use the Bash Shell’s export Command

Problem

Recently at my day job I’ve been having to go through some pretty old Bash scripts that I’ve basically inherited. As I’ve been going through them I’ve been seeing a lot of confusion as to the proper use of Bash’s export command. The major offense? Not really understand whether a particular variables needs to be […] Continue reading

Posted in bash, one-liner, shell, Syndicated, tip, tips & tricks | Comments Off on [one-liner]: How to Use the Bash Shell’s export Command

[one-liner]: Determining a Hard Drive’s Manufaturer Under Fedora 10 & CentOS 5

Background

I recently saw a post over on Linux Journal that discussed how to glean information about a system’s hard drive, such as its serial number, without having to actually open up the case and physically check it. So I thought I’d take the opportunity to write up a blog post with the specifics of how […] Continue reading

Posted in bash, centos, fedora, redhat, shell, Syndicated, tip, tips & tricks | Comments Off on [one-liner]: Determining a Hard Drive’s Manufaturer Under Fedora 10 & CentOS 5

[one-liner]: Shell Builtins

Background

I recently worked a problem in a previous post where it wasn’t clear which version of the command nohup was getting used. The version that was built into the C-shell interpreter, or the executable sitting under /usr/bin/nohup. This brought up an interesting point, how do you know what commands are builtins to the shell itself?

Enter […] Continue reading

Posted in bash, csh, linux, one-liner, shell, Syndicated, tip, tips & tricks | Comments Off on [one-liner]: Shell Builtins