Archive for the ‘Tips and Tricks’ Category
Posted by Derek@TheDailyLinux »
Add Comment »
Simply put, perldoc is the man page of perl applications. Use it and use it often when dealing with perl utilities. For example:
perldoc open
Reveals…
System::Library::Perl:User.Contributed PeSystem::Library::Perl::5.8.8::open(3)
NAME
open − perl pragma to set default PerlIO layers for input and output
SYNOPSIS
use open IN => ":crlf", OUT => ":bytes";
use open OUT => ’:utf8’;
use open IO => ":encoding(iso−8859−7)";
use open IO => ’:locale’;
use open ’:utf8’;
use open ’:locale’;
use open ’:encoding(iso−8859−7)’;
use open ’:std’;
DESCRIPTION
Full‐fledged support for I/O layers is now implemented provided Perl is
configured to use PerlIO as its IO system (which is now the default).
...
...
Posted by Derek@TheDailyLinux »
Add Comment »
I’ve been on a kick recently about doing everything in the terminal and I became excited about the possibility of doing it through the terminal. I was excited until my little research on the subject revealed that this horse has been beaten left and right and up and down and diagonally. There were so many different ways of doing it, I sorta lost the urge to create a post about it. Well, here I am anyways listing the ones I found to be the most interesting. Check ‘em out if you’re interested…
http://fungi.yuggoth.org/weather/
http://linuxshellaccount.blogspot.com/2008/09/bash-script-to-get-weather-forecasts.html
telnet rainmaker.wunderground.com
curl http://weather.noaa.gov/pub/data/forecasts/city/state/city.txt
Posted by Derek@TheDailyLinux »
Add Comment »
A quick and easy way to troubleshoot network connections in an embedded Linux environment is to use the ping, tcpdump, and arp commands. This will allow you to see what might be happening with the traffic on your network at the packet level. Once scenario would be that you’re not getting a successful ping between two computers on the network. All reason tells you that it should be working, but maybe there’s something wrong at the driver level? To start troubleshooting, fire up tcpdump on the box being pinged (ip: 192.168.1.100) and specify the network interface and IP address of the machine doing the pinging (ip: 192.168.1.101):
tcpdump -i eth0 host 192.168.1.101
Now, run the ping test:
ping 192.168.1.100
At this point, if you get an output from tcpdump, you’ll know that the packet has made its way to the target machine (192.168.1.100). The next step would be to see if you ever received an acknowledgment from the target machine. Run arp on the machine doing the pinging (192.168.1.101):
arp -an
If you see something like ? (192.168.1.112) at (incomplete) on en1 [ethernet], that means you never got an acknowledgment back from the target machine (192.168.1.100).
At this point in this scenario, we would start to dive into what could be wrong. In this particular situation, it was a driver issue with assigning a MAC address to the interface.
This is just one scenario to get you familiar with how to use tcpdump and arp and gather information about the networking issue. Please refer to the utility man pages for more information.
Posted by Derek@TheDailyLinux »
Add Comment »
find /path/to/directory -type d -empty -exec echo {} \;
I think it’s easy to get the idea based on the example above, but just in case you need inspiration, you could automatically remove these empty directories with the following (Careful! This uses ‘-rf’ flag which shows no warnings or mercy!):
find /path/to/directory -type d -empty -exec rm -rf {} \;
Check out some additional methods here:
http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/
Posted by Derek@TheDailyLinux »
Add Comment »
In an embedded system without a battery backed real time clock (RTC), a time-based ‘fsck’ check can be quite annoying. To disable the time-based check, use the tune2fs command on the drive containing the root partition like so:
tune2fs -i 0 /dev/<device node>
There’s more information here at this link for those of you interested:
http://serverfault.com/questions/28343/will-my-system-fsck-when-i-reboot
Posted by Derek@TheDailyLinux »
Add Comment »
When using Linux serial ports, the tty has a default 5 to 10ms latency time built-in. To drop this, use the low_latency parameter of setserial. This is useful for getting a more real-time response in Linux serial ports. Be sure to check out the man pages for more information.
Minimize the receive latency of the serial device at the cost of greater CPU utilization. (Normally there is an average of 5-10ms latency before characters are handed off to the line discpline to minimize overhead.) This is off by default, but certain real-time applications may find this useful.
Posted by Derek@TheDailyLinux »
Add Comment »
Here are some basic Linux commands that will allow you to navigate easier within the terminal. These commands include dirs, pushd, popd, and cd.
cd
This is probably one of the most known commands since you really can’t get around a Linux terminal without it. There are some pretty neat tricks you can use to more quickly navigate directories. These include:
cd /path/to/dir
- Change current working directory (
pwd) to /path/to/dir
cd ..
- Change back one directory level
cd ~
- Change to your home directory at /home/username/
cd -
- Change to the directory you were in last
In order to talk about the next commands, you need to understand what a stack is. Imagine a spring-loaded plate holder like the ones you’d find at an all you can eat buffet (this is dirs). The employees fill this plate holder by pushing a plate onto the plate holder. Then another plate is pushed onto the top of that plate (this is pushd). This is repeated again and again and then soon, you have a full stack of plates. In order to get to any of the plates in the middle, you must pull the plate on top off, and then the next, and then the next until you get to the plate you want (this is popd). This can continue until the stack is empty. This example is known as a Last In First Out (LIFO) or First In Last Out (FILO) data structure and that’s exactly what we’re dealing with here.
Now that you have an understanding of what a stack is, let’s chat about the next three commands: dirs, pushd, popd.
dirs
This is the command you use to display the stack of currently remembered directories.
dirs -v
- View all directories on the stack with numbers
dirs +N OR dirs -N
- Display Nth directory counting left to right, OR right to left
cd `dirs +N`
- Change directory to dirs stack number N
pushd
This command will push a given directory to the top of the stack of remembered directories.
pushd .
- Push the current directory onto the stack
pushd /path/to/dir
- Push the /path/to/dir onto the stack
popd
This command will pull the top directory off the stack of remembered directories.
popd
- Pull the top direcotry (0) off of the stack, resulting in changing directory to (1)
Posted by Derek@TheDailyLinux »
Add Comment »
It’s so simple it doesn’t really need an entry, but there will be a time when somebody asks “how can I compare two different directories or file systems”, and I will say, “this way…”
diff -rq /dir1 /dir2
The -q option above will only tell you which files differ. It will not go into details and display each differing line of each file.
Posted by Derek@TheDailyLinux »
Add Comment »
Here’s a core utility that will surely be of use for Linux or Mac users: cURL. In my own description, it’s sorta like wget, but different. Description from the website:
curl is a tool to transfer data from or to a server, using one of the supported protocols (HTTP, HTTPS, FTP, FTPS, SCP, SFTP, TFTP, DICT, TELNET, LDAP or FILE). The command is designed to work without user interaction.
Also, it might be good to note this as well now that I brought up wget:
Curl is not a wget clone. That is a common misconception. Never, during
curl’s development, have we intended curl to replace wget or compete on its
market. Curl is targeted at single-shot file transfers.
I took some time to play around with it a bit and found that it’s quite easy to use and the documentation and man pages are nicely laid out. For example, here’s how I used curl to download an image from a password protected website:
curl http://www.thewebsite.com/image.jpg --user theusername:thepassword > image.jpg
I wanted to see if I could download multiple files at a time, so I became a little fancier and threw in a for loop with the seq command for counting. Since I knew all of my images were taken in order (100.jpg -> 110.jpg), I could use something like:
for i in $(seq 100 110); do curl http://www.thewebsite.com/$i.jpg --user theusername:thepassword > $i.jpg; done
I’m sure that this hasn’t even scratched the surface for what curl can do, so be sure to check out the man pages and have fun with it!
Posted by Derek@TheDailyLinux »
Add Comment »
Let’s pretend that you’ve made a mistake when creating a binary image and you didn’t realize it until after you painstakingly created this image for use in a production process. Well, instead of going back and doing it all over again, you can simply use hexedit to edit the binary file directly, saving you precious time.
hexedit -m binaryfile.dd
In my particular case, I was trying to create an SD card image for use in an SBC product and I didn’t realize until it was too late that there was a type-o in a script. I used hexedit to fix the type-o and everything worked out heavenly.