Save the Output in a Shell Script Array

I wanted to do some shell processing on the contents of a directory. To do this I needed to put the contents of the directory in an array. Fortunately this is not difficult with bash.

  1. First capture the output.

    ls_output=$(ls /images)

  2. Next declare an array and reference the output with this array.

    declare -a img_files
    img_files=($ls_output)

  3. Now you can access the array directly or loop through it.

    echo ${img_files[0]}
    for img_file in ${img_files[@]}
    do
      ...
    done

Thanks O’Reilly’s Bash Cookbook.

Save the Output in a Shell Script Variable

I was counting how many times a file was referenced by other files. I then wanted to do some processing based on this number.

To do this I saved the output of this count in a variable which I was then able to reference later in the shell script.

num_files=`find . -type f -name '*.html' | xargs grep $img_file | wc -l`

# delete this file if it is not referenced
if [ "0" = $num_files ]
then
  rm $IMAGE_DIR/$img_file
fi

Cygwin Bash cannot execute DOS formatted scripts

As of bash 3.2.9-10, bash no longer executes DOS formatted scripts as detailed in this announcement. I understand the reasoning for this is that:

  1. cygwin is supposed to mimic Linux, not Windows
  2. it is faster to assume that the scripts are in UNIX format

To get around this restriction I did what was recommended in this bug report, i.e. I created a Windows user environment variable named “SHELLOPTS” with the value “igncr”.

Unable to start Cygwin shell from Windows command prompt

After installing Cygwin when I tried to start the shell from a Windows command prompt by entering the “sh” command a window would pop up with this error.

The procedure entry point rl_getenv_hook could not be located in the dynamic link library cygreadline6.dll.

After seeing this error I would have to manually kill the sh process using the Windows Task Manager.

There was a thread about this on the Cygwin mailing list. I did what was suggested and discovered that bin/sh.exe and bin/bash.exe were exactly the same. Yet for some reason when I ran bash from the Windows command prompt I did not get this error.

I then tried rerunning the Cygwin installer. Even though nothing was installed that seemed to solve the problem, i.e. I was able to run sh from the Windows command prompt. This was possibly because the bash postinstall script was run correctly.

Update: After reboot I am experiencing the same problem again. Aargh! 🙁

Installing the Microsoft Loopback Adapter

If you want to install Oracle, that great big beast, the computer you intend to install must have a static IP address. However for many people their IP address is dynamic, that is it comes from a DHCP server. To get around this restriction you can install the Microsoft Loopback Adapter which creates a virtual static IP.

To install the Microsoft Loopback Adapter follow these instructions. You should be able to start the Add Hardware Wizard by selecting Add Hardware in the Control Panel.  In Windows XP if you cannot find Add Hardware in the Control Panel then you can start the Add Hardware Wizard by selecting Printers and Other Hardware in the Control Panel. You should see an Add Hardware entry on the top of the left column.

After installing the Microsoft Loopback Adapter go to the Control Panel, select Network and Internet Connections and then Network Connections. Select the Local Area Connection that is a Microsoft Loopback Adapter (usually Local Area Connection 2). Select the Internet Protocol (TCP/IP in Windows XP, TCP/IPv4 in Windows Vista) and then press the Properties button. Next select Use the following IP address and enter an IP address that should not conflict with your LAN. I use 192.168.1.200 and set the Subnet Mask to 255.255.255.0.

After you set the IP address you can use the Windows host file to assign a friendly address to this IP address. For example add this entry to C:\WINDOWS\system32\drivers\etc\hosts.

192.168.1.200 cool.example.com

HOWTO Map a Network Drive Even When You’re Not Logged In

Often when you’re consultant you find yourself stumbling into a LAN with no proper access. IT doesn’t have time to set you up so instead of waiting you have your client friend tell you his login information and then you can start mounting drives from a DOS command prompt like this.

C:\Documents and Settings\fkim>net use z: \\iis1\e$ password /user:domain\username

HOWTO: Disable Windows Error Reporting?

Occasionally you will see a window popup like this one.

windows error reporting

This happens when a program error or a system error occurs. Windows prompts you to send an error report, theoretically so that Microsoft can use the information to make Windows more robust.

Personally I don’t trust Microsoft so I wanted to turn this off. Fortunately Microsoft provides a way as outlined in this article.

Hibernation: Insufficient System Resources Exist to Complete the API

pianoOccasionally when I try to hibernate I receive an “Insufficient System Resources Exist to Complete the API” error message. When this happens I am forced to shutdown instead of hibernate.

This is a known issue for computers with 1 GB of RAM or more. Microsoft has a KB article about this. To obtain the hotfix Microsoft requires you go through Microsoft support which is a big pain so I have made it available here .

Windows hosts file not working

On Windows you can manually match hostnames to IP addresses using the hosts file located in C:\WINDOWS\system32\drivers\etc. An example of a hosts file:

127.0.0.1       localhost
#10.22.1.18     foo foo.bar.ca
16.17.18.19     toronto
16.17.18.125    boston

If you find that the contents of the hosts file are not being picked up by Windows it might be because the file is corrupted though the corruption is not evident in your editor. The best thing to do is to delete or move that file and create a brand new hosts file and hopefully the contents of this hosts file will be picked up by Windows.