My blog has moved!

You should be automatically redirected in 6 seconds. If not, visit
http://www.mattdturner.com/wordpress
and update your bookmarks.

AdSense

Pages

Showing posts with label Command Line. Show all posts
Showing posts with label Command Line. Show all posts

Monday, April 25, 2011

Useful .bashrc Functions

For those of you that read my other post about useful .cshrc functions, you know that I like to automate as many processes as possible.  If there is something redundant that I do frequently, I will script it or make a function to take care of it.

In an effort to assist those of you out there that like to automate processes but use bash instead of C-Shell, I have compiled a list of some useful functions to stick in your .bashrc file.  Here is a list of the functions that this post will contain:

  1. Extract:  This function will determine the type of archive you are dealing with, and issue the corresponding command to extract it.
  2. Compress:  This function will create a .tar.gz archive of whatever files you give.
  3. Swap:  This function will swap the names of 2 different files.
  4. Clock:  This is just a random function that will display a digital clock in your terminal window and update every second.
  5. Cpg:  This function will copy a file to a directory, and also cd you to a directory.
  6. Mvg:  This function works like Cpg, but moves the file instead of copying it.
  7. Findit:  This function will search for a file with the given pattern in the name.

Thursday, April 21, 2011

Mac Bluetooth Keyboard/Mouse Battery Level in Terminal

There are a few reasons why somebody might want to check the current battery percentage of a bluetooth mouse and/or keyboard within Terminal on a mac.  Some that come to mind are for use in GeekTool, if you are connected to the computer remotely through ssh, etc.

Well as it turns out, its actually very easy to do.  The tool that we will use to check the battery percentage is called "ioreg".  The Mac OS X manual describes ioreg as a tool that "displays the I/O Kit registry. It shows the hierarchical registry structure as an inverted tree."

There are many options available with the ioreg tool, however this article will just be discussing how to check the battery percentage of different devices.  See the below text for the code to view the battery percentage:

matthewturner/~:  ioreg -n "IOAppleBluetoothHIDDriver" | grep -i "batterypercent" | sed 's/[^[:digit:]]//g'
49
matthewturner/~:  ioreg -n "BNBMouseDevice" | grep -i "batterypercent" | sed 's/[^[:digit:]]//g'
45
matthewturner/~:  ioreg -n "BNBMouseDevice" | grep -i "batterypercent"
    | |   |     | | |   |   "BatteryPercent" = 45

There are a few things that should be noted about the above commands.

Wednesday, April 20, 2011

Automatically Make Shell Scripts Executable

I strongly believe that a script should be created to handle any repetitive process in the terminal.

By default, when you create a new script, it does not have executable permissions.  So for each script that you create, you then have to chmod the file to be executable.  However, its kind of hypocrital to say that a script should be created to replace any repetitive process and then not figure out a way to automate the 'chmod' command when creating a script.

Well, as it turns out, a simple addition to your vimrc file will automatically make any newly created script an executable (so long as you have the '#!' in the file).

" automatically give executable permissions if file begins with #! and contains
" '/bin/' in the path

au BufWritePost * if getline(1) =~ "^#!" | if getline(1) =~ "/bin/" | silent !chmod a+x <afile> | endif | endif
au BufWritePost * if getline(1) =~ "^#!" | if getline(1) =~ "/usr/bin/" | silent !chmod a+x <afile> | endif | endif

So just copy the above code into your .vimrc file (each uncommented line ends in 2 endif commands) and now you can create scripts that are executable by default.
 

Tuesday, April 19, 2011

How to Fix "Output Conversion Error"

As part of my research for my Ph.D. I am on a team that is currently developing an adjoint of the EPA's CMAQ air quality model.  In the process of integrating all parts of the model into the full adjoint model, I ran into an error that was rather difficult to resolve.

Running the model would result in many occurances of the following error:

forrtl: error (63): output conversion error, unit -5, file Internal Formatted Write
Image              PC                Routine            Line        Source             
ADJOINT_FWD        00000000009B34BD  Unknown               Unknown  Unknown
ADJOINT_FWD        00000000009B1FC5  Unknown               Unknown  Unknown
ADJOINT_FWD        0000000000969210  Unknown               Unknown  Unknown
ADJOINT_FWD        000000000092AADF  Unknown               Unknown  Unknown
ADJOINT_FWD        000000000092A312  Unknown               Unknown  Unknown
ADJOINT_FWD        000000000095305A  Unknown               Unknown  Unknown
ADJOINT_FWD        00000000005D9F94  ckdesc3_                  138  ckdesc3.f
ADJOINT_FWD        00000000005A9FD1  open3_                    216  open3.F
ADJOINT_FWD        000000000047B395  chk_files_impl_mp         170  CHK_FILES_IMPL.F
ADJOINT_FWD        0000000000485060  chk_files1_mp_chk         347  CHK_FILES.F
ADJOINT_FWD        00000000005666CB  vdiff_                    369  vdiffacm2.F
ADJOINT_FWD        0000000000496B7E  sciproc_                  228  sciproc.F
ADJOINT_FWD        000000000048DDB5  MAIN__                    205  driver_fwd.F
ADJOINT_FWD        0000000000404A1C  Unknown               Unknown  Unknown
libc.so.6          0000003FE9E1D994  Unknown               Unknown  Unknown
ADJOINT_FWD        0000000000404929  Unknown               Unknown  Unknown


     >>> WARNING in subroutine CRTFIL3 <<<
     Error creating netCDF variable for file ADJ_VDIFF_CHK   
     Illegal data type    0

      
     *** ERROR ABORT in subroutine CHK_FILE_CREATE_
     Could not open ADJ_VDIFF_CHK file
     Date and time  13:00:00  July 22, 2001   (2001203:130000)


I spent a lot of time searching online, however I was unable to find a solution for my problem.  After days of debugging, I finally found the source of the problem.

Useful .cshrc Commands

Throughout the years of using the command line in both Linux and Mac, I have spent quite a bit of time modifying my .cshrc file to my liking. 

This is mainly just a list of some commands that I think are useful.

# Prompt - Displayed as “@:
set prompt="`whoami`@%B`hostname -s`%B:" #Custom Prompt settings

alias lsl 'ls -larth'
alias qstatme 'qstat -u mturner'
alias qsubm 'qsub -I -V -l walltime=08:00:00,nodes=1:ppn=24'
alias quota '/usr/bin/quota -s -u mturner'


The “lsl” alias should be self explanatory.
“qstatme” displays the current PBS queue for my username only.
“qsubm” will start an interactive PBS job, using 1 full node, and requesting 8 hours.
“quota” will display my quota

# set title
alias cwdcmd 'echo -n "\033]0;$cwd\007"'


“cwdcmd” will change the title of the terminal window to match the current working directory

Monday, April 18, 2011

Some Useful .cshrc Functions

If you use the command line a lot, there are numerous common commands that can be automated to save time.

Like what? Well, how about swapping the names of 2 files; or maybe unarchiving any filetype.

People who use bash are lucky, bash allows you to actually create functions in the .bashrc file. Those of use who use c-shell, however, have to deal with aliasing.

Below are a few “functions” that i have in my .cshrc file which save me a lot of time.

Swap 2 files


alias swap 'mv \!:1 tmp.1 | mv \!:2 \!:1 | mv tmp.1 \!:2'
Use: mturner@prospero: swap file1.f file2.f
Result: The filenames have been swapped for these 2 files


Move and go


alias mvg 'mv \!:1 \!:2 && cd \!:2'
Use: mturner@prospero: mvg file1.f ~/Desktop
Result: file1.f will be moved to the specified directory, and you are automatically cd’ed to the specified directory

Sunday, April 17, 2011

/etc/motd Formatting

It is good practice to announce server maintenance dates and scheduled downtime dates many days prior to the actual downtime. The easiest way to ensure that all users become aware of the scheduled downtime is to place an alert in /etc/motd.

For those of you that don’t know, /etc/motd is the file that gets printed to the screen when a user first logs into the system.

However, in the case of our server (possibly most servers), users tend to ignore the contents of motd since it very rarely changes. In order to attract the attention of any user that logs into the system, I have begun adding formatting (bold, underline, colors, etc) to the motd file on our server.

Formatting can be easily added to any motd using ANSI codes. For example, below is a copy of a sample maintenance announcement for our server:





Notice that the text “Server Maintenance” is what first got your attention. This is because of the different color, bold text, and blinking text (blinking text is not supported in IE, Chrome, and Safari).

Using the Debugging Tool Valgrind

Valgrind is an excellent, and sometimes necessary, tool for debugging troublesome programs. According to Valgrind’s website, the program can “automatically detect many memory management and threading bugs, avoiding hours of frustrating bug-hunting, making your programs more stable. You can also perform detailed profiling to help speed up your programs”.

Valgrind contains a variety of tools for use while debugging. The program can be used to debug a wide variety of programming languages including, but not limited to, C, C++, Java, Perl, Python, Fortran, Ada, etc. Using these tools, you can:

  • Detect memory errors (using the tool Memcheck)
  • Make programs run faster (using the cache and branch-prediction profilerCachegrind)
  • Detect thread errors (using the tool Helgrind and/or DRD)
  • Profile heap usage (using the tool Massif and/or DHAT)


How to Use Valgrind’s Memcheck Tool


Possibly the best thing about Valgrind is the fact that it is very easy to use. There is only 1 thing that you need to do in order to prepare your program for use with Memcheck; compile your program with the -g and -O0 compiler flags. That’s it. Now all you have to do is run your program using valgrind.

How to Stop "Thanks for Flying Vim" Message

If you are like me, you probably do a lot of your coding on a remote computer. I have known some people who will download all files from a remote computer onto their local system, make changes in a text editor, and then upload the files back to the remote server. This is a very inefficient way of coding, and is just one of the reasons why i choose to write all of my code in VIM.

If you have ready my other posts (specifically Useful .cshrc commands), you know that I like to have the title of the terminal set to my current working directory. However, after exiting VIM, I always end up with the frustrating “Thanks for flying VIM” message in the title.

I had read on other blogs and forums that adding set notitle to your .vimrc file will prevent the “Thanks for flying VIM” message from showing up.

Well, that didn’t work for me. After a lot of scouring the internet, I was able to find the solution.
let &titleold=getcwd()
Adding this to your .vimrc file will, upon exit from vim, set the terminal title to the current working directory.

More .cshrc Commands

The following .cshrc commands are ones that I feel should be included in the default .cshrc file for the entire system (/etc/csh.cshrc).

# set file completion
set filec
# set to list all possible matches
set autolist
# set auto correct
set autocorrect

Each command is fairly self-explanatory.

Group Access to All Files

On every cluster, there is most likely a group of users who want to allow the entire group to have read, write, and execute privileges to every file they create. However, changing the permissions on each file individually is a major nuisance.

You can, however, change the default set of permissions for every file and folder that a user creates. This is done using the “umask” command.

For our air-quality modeling group here at CU Boulder, each user’s primary group is set to “henze”. Then, within each user’s .cshrc or .bashrc file, the umask command is used to modify the default permissions.

In our case, we use
        umask 0007
This makes it so that every file or folder that the user creates has permissions 770 (drwxrwx---).

This is a simple change that helps keep groups of users happy.