Power to Build

Home » Posts tagged '*nix'

Tag Archives: *nix

Running scripts across shells in *nix


A friend asked me about running csh scripts on Linux. Apparently, they were porting from Solaris to Linux (how did a great Operating system like Solaris end up here? Another of those grave mistakes Sun Microsoft did!!). The linux box had korn shell as the default shell(1). When they ran it, it was giving a lot of errors. They were thinking of rewriting the script in ksh syntax. This definitely looked like a momentous task. I knew you can run one shell from another. (Any doubt? Simply type csh when you are in K-Shell. Now, you are in C-Shell – as long as it’s installed). Then why was the script failing with a lot of errors? We sat down to troubleshoot. In the end it turned out to be a simple PATH issue! If you are facing similar issues with your old shell scripts, then read on!

If your machine doesn’t have the target shell, you need to install it first (2). There is a linux port of the C-Shell called tcsh. Download and install it. See here for instructions on how to. It’s pretty straight forward. Though the program is tcsh, the installation creates some symbolic links with the name csh in /usr/bin and /bin, so you can run it as just csh.

If you try to simply run your csh script in another shell (Bash or ksh), it will fail. There are many differences between the 2 shells. For e.g. to define variables in csh, you will use setenv. To do this in ksh (and in bash) you will have to do export. So a csh script will not run inside ksh shell and vice versa. You will have to either rewrite or force it to run inside the corresponding shell.

To force a script to run in a specific shell, you typically add shebang as the first line inside the script. This helps the script to run with the right script interpreter automatically. (instead of associating specific types with programs, as we do in Windows). But, this doesn’t work when you are running the script inside a different shell, it tries to interpret it using current shell. To run a script inside a target shell, you need to get into the target shell, then type the script to run. You can do this in one step, by “sourcing” the script into the target shell, by using the fast (-f) option as,

csh -f <script>

Now back the problem my friend had. She was indeed using the above syntax to run her C-Shell script. Still it failed with several “command not found” errors, particularly on date command. Hmm! This script used to work in C-Shell on Solaris and date is a common unix utility, it must exist everywhere! I went into C-Shell(just type csh on command prompt, it will switch to C-Shell) and tried date command. It wasn’t there! So, now we have a more specific issue. Find out where the date program is.

to do this, I typed “which date” on K-Shell and the mystery resolved. This command used to be in /usr/bin directory on Solaris and on the linux box it was in /bin. The PATH variable used in the script included /usr/bin, but not /bin. This was the reason why date command wasn’t working. Once we fixed the PATH, everything was fine again.

The lesson is, you don’t always have to rewrite your script when you are changing machines/shells. You *can* invoke any type of shell scripts, from any other type of shells. Chances are there is a port for your favorite shell on your new *nix machine.

Also, when you are getting “command not found” type of errors, try to use the which command to find out where a program is and try to include its path in the environment variable, $PATH. You can also look at the man pages for the command you cannot find. If the command is installed, man will list the path information also.

And finally, never assume anything in the *nix world!

Note:
(1) Suse Linux typically comes with Bash as the default shell. Since our standard is ksh, I think they must have installed and changed default to ksh. See here for how to switch shell associated with a login.

(2) C-shell is an older shell which was very popular in the early days of Unix. Several places have phased out of this, because of its limitations. Ksh or Bash is more modern and Bash is typically default on Linux boxes. So, unless there is a real need for it, or you are a C-Shell junkie, you don’t really want to switch to C-Shell. See here and here for some notes on why you shouldn’t be using C-shell.

My Journey with Ubuntu Contd – Switching Desktop


Last time, I posted about upgrading to Ubuntu 12.04, Precise Pangolin. It has a user interface (desktop) called Unity. It looked different from Gnome desktop I got used to in ver 10.x. It was an adjustment, but it was easy to get used to. One problem I had though was that it kept freezing up at random. I believe this is a bug in the interface, judging by the bug reports opened for the issue.

Installing a new Desktop

While researching the issue, I found out that I could replace my desktop interface easily with Gnome desktop I used to like!! This blew my mind!! What? Replacing desktop? You mean re-installing complete OS, right? At least, that’s what I am used in the MS-Windows environment. No, not with Linux. Here is a link I used in getting the new desktop installed. This put Gnome right beside Unity. All I had to was switch to Gnome when I logged in (See Fig. 1). So far, it’s working fine. Apart from this, my applications and settings are still there. It connected to network and printer right away, so I didn’t miss anything. (Note: While logging in I had to use Gnome (No effects), to get the same desktop look I had in 10.x.).

Gnome in Pangolin

Fig 1. Gnome in Pangolin

(Note: to get to this list, click on the ubuntu icon while on the login screen. This is like pressing F8 to get to Safe/Normal mode in MS Windows).

Coming from MS Windows environment, I didn’t think this was possible*. Not that I am a Micorsoft basher or anything. I’ve worked on MS Windows, most of my working career. I believe, MS-Windows is part of the evolution process a lot of us went through in the Software industry. Even so, I couldn’t avoid being disappointed with MS-Windows. First of all, there is no such option with MS-Windows. With windows, if you wanted a different desktop, you would have to reinstall different version of OS. Remember Vista? When it came out it was buggy. Microsoft  offered that you could “downgrade” to XP one time. But then, you lost the shiny new OS called Vista. On top of that, when I switch to a new OS like that, I am sure to face an issue or 2 to get it to work with the set-up I have. No, this was not supposed to be possible.

(Note *: There were some custom desktops available from 3rd party, but nothing fitted in smoothly, as Gnome did here! Further, they typically ran on top of the existing windows desktop, so they were slower).

Secret lies in Linux Architecture

So, I researched this a bit more. Apparently, Ubuntu (actually any linux and Unix)  OS is built in a modular way. One layer can be easily replaced without disturbing another. As you know, underneath it all, there is kernel, the core shell in the OS. Then there is command line interface which is what we were used to, if you were using Unix. Add to that, a windowing layer, you got a Desktop. So far, this is the same way in Windows. (At least it used to be through Windows 98). But there ends the similarity. Fig 1, shows the building blocks of a Linux OS.

Linux Archirecture

Linux Architecture – Courtesy Paul Cooper (pgc) on Flikr

So, when I installed Gnome, I basically added to the desktop layer at the very top. The underlying layers (including Window Manager and X-Windows) were unaffected. This is why, my set-up was unaffected. Wow, truly modular programming! Similarly, I could actually replace a hardware and Linux would simply find appropriate drivers (or you can install it) and everything is working again! I couldn’t help compare this to MS-Windows, if I changed hardware (even as simple as moving the hard drive with the OS to a new machine), it will be a nightmare to get it working again. Kudos to Linux community.

I really hope they fix the bug(s) in Unity, so I can get back to it in the future. But, I am not exactly missing anything here. Because, Gnome is actively developed in the community. Further, my desktop is definitely running faster than with Unity. (Another complaint about Unity on the web).

P.S: If you are wondering how I got the screen capture of the logon page, that’s another strength of Linux (Again, you couldn’t do this in windows). There is a whole slew of things to read up on X-windows. I will post more on this later. For now, refer to the link in the references below.

References

  1. Gnome Desktop
  2. Unity Desktop
  3. Ubuntu
  4. How to add Gnome Desktop to Ubuntu 12.04
  5. Another site about installing Gnome Desktop
  6. How to get screen capture of the Login page

Now I can proudly say, my PC is

Ubuntu – Thunderbird on Linux


I’ve used Mozilla Thunderbird as my mail client on Windows, for a while now. This worked out better for me than Eudora, Outlook express. Ever since I lost my account at Yahoo some years back, I store important messages on PC. I switched Gmail for the fact that they allowed POP client. (On Yahoo, I had to pay for that service!)

Since I moved to Linux last year, I’ve tried various software on it. I had Evolution as my mail client as it comes as default on Ubuntu. It was very similar to Thunderbird, but there were some issues: For some reason the address lookup never worked for me there. Also, it had some file permission problems constantly, so my sent mail will sit in outbox forever. Worse yet, it used to silently crash and restart itself every now and then. I am sure, I could have googled for a solution, but I decided to go back to Thunderbird. I love it.

(Googling for “ThunderBird vs Evolution” brings backs hundreds of threads like the one below. They also talk about Kontact, Claws etc. Must explore in the future.
http://ubuntuforums.org/archive/index.php/t-256684.html. )

When I got Thunderbird, I set it up POP settings for Google. Since, I have 2 PCs connecting POP to Google mail, messages started disappearing on either, depending on which one got it first. (Nature of POP, once you downloaded a message, it’s no longer available to download. See below link). Then I discovered IMAP. It’s always been there, but suddenly it occurred to me that I could use that for a multi-client setup. I did that, and it’s been working OK. One thing with IMAP is that it’s a direct access to the Internet Mailbox. So, nothing is stored on your PC. But, IMAP has an option to store locally as well. So, on one PC I set it up to download and the other just connect directly. Below link has a nice article about IMAP vs POP mail.

http://www1.umn.edu/adcs/guides/email/imapvspop.html

Linux: Wget, Yum


One of my co-workers was trying to install Oracle linux update to Fedora linux box. She was trying to do a wget command based on some web site instructions. Her wget failed and was not able to get the repository file at public-yum.oracle.com.

wget http://public-yum.oracle.com/public-yum-ol6.repo

Per suggestions on the web, we tried using host or nslookup for the website. They could not find the web site.

Where as, google site was always found. host google.com and nslookup google.com worked fine. But not to the oracle server. Not even oracle.com showed up when we tried host, nslookup and ping commands.

After googling it up and trying to find solutions, we got to a point, where it pointed to some kind of network issue, probably proxy? I asked her to download the above file manually (she was always able to go to the web page directly in browser) and save to /etc/yum.repos.d directory. Then we tried,

yum list command.

This gave a different error:

IOError: <urlopen error (-2, ‘Name or service not known’)>

 

Some more web searches (http://serverfault.com/questions/76421/wget-cant-resolve-host) revealed this to be related to DNS. That was kind of strange. As we were able to ping and nslookup to google.com and we were always able to go to the oracle site in browser.

Anyways, we decided to edit the /etc/resolve.conf file and add some public name servers to it.

nameserver <dns ip address>



This didn’t help. Then she logged into a unix box and we looked at the /etc/resolve.conf there. It had only one name server. We copied to the file on Linux box. Bingo!! It worked. So, the problem was that the name servers she had listed in the resolve file didn’t resolve oracle servers for some reason.

 

Yum, Wget etc

I really didn’t know much about Yum and Wget until today. While working on this problem, learned a few things:

Wget is a cool utility to download files. Per the wikipedia page , wget (formerly geturl) is a non-interactive replacement for FTP, but it also works with other protocols like HTTP, HTTPS etc. It can download recursively.

Yum is a package manager/installer for Linux. See here for a definition:

http://www.wisegeek.com/what-is-yum.htm

Turns out the .repo file we were having problem with in the first place, is the repository that has some configurations to be used by Yum to download more files and install them.

Note: If you are editing any of the /etc/ files and you already have the console window open, chances the new settings will not be effective. Close all the console windows and start a new one.

ubuntu & I


I’ve a long experience with the Unix world. When I first picked up Linux, it was all command line (I think it was Redhat). I kept hearing about it and I noticed that it’s commands overlapped with Unix shells in many ways. I told myself, yeah I know Linux. This was more than 10 years ago.

Since then I’ve had chances to work on Linux command line versions several times when I worked on some web projects in PHP/MYSQL. Still the same impression. It’s like Unix.

Few years ago, I was at Fry’s. They had a PC for $125+. This was when a decent windows PC cost like $500 – $1000. I was thrilled. Picked up one and took it home. It had some flavor of Linux on it. To my surprise, it had a windowing version of Linux. I was thrilled. I took out my windows PC (I believe Win2K or XP) and plugged in the new PC. I connected to Ethernet, mouse, keyboard, speaker, monitor and even the printer. To my surprise Internet connected right away. So did the monitor, keyboard, mouse. After some fiddling I was able to get sound working also. I’ve worked with windows and experienced the typical windows problems like driver issues, software issues, hardware incompatibility issues before. Different versions of windows didn’t work with the same hardware. Here I got a totally strange Operating System (Actually it was strange because the flavor of Linux I had was from Taiwan and it printed a lot of Chinese characters – that added to my surprise as well) and it worked like a charm. I was impressed. But, I didn’t keep the PC for long, as there some other devices that failed with it.

Till recently It’s been touch and go with Linux. Recently, my windows PC crashed. I must admit, I installed EASUS partitioning software and some registry compacter and these screwed up a lot of paths in the registry. I tried to restore from Restore points, alas it wouldn’t. I tried to create one, it failed. I had to actually reinstall windows from the backup that comes with HP PC. This was frustrating experience. My PC had been slower and slower, which is what drove to touch the partition and registry in the first place. Now that I reinstalled windows, I was hoping my PC was going to perform better. To my disappointment, it really didn’t.

My father was visiting me and in his spare he decided to build a PC. I had different old PC’s hanging around. He got one working by replacing the motherboard with Intel Atom based board. It has a memory restriction of 1GB. We got an old Windows XP drive installed and was actually run Windows. (Not quite easily though – Windows seems to build configuration of the PC where it was first installed, so when I tried to use XP in a different PC, it failed to boot). I didn’t remember the windows password. After looking around, we found a Linux based password extraction program!!! The entire operating system on a CD and we didn’t even have to install it. It ran right out of the CD.

This is when we decided to give Ubuntu a shot. I eventually installed Ubuntu. The windowing interface it had, blew me away. I loved it. And as usual, the PC with Linux worked with all the devices right out of the box. Actually the Windows on the Atom PC did have problem with Audio and to my surprise, Audio worked with Ubuntu.

Then I dusted off an old 10GB drive and installed Ubuntu on it. Now, this is my main PC. Ever since, I am hooked to Ubuntu. Everyday I keep discovering new software to install and still have some more space to do other things. So far, this older PC with less memory and hard disk space than my 2-year old Windows Vista PC, the run for the money. Now, I don’t use my Windows PC directly. When I want to I remote desktop from my Ubuntu PC.

I have plans to upgrade my Ubuntu PC. (I don’t have the intention to get rid of Windows PC yet, as there are several software on it that I still use). But, with Wine installed, I am discovering new sides of Linux. I will write more about my experience with Linux here.