You can get the path of the directory in which a bash script is located FROM that bash script.
Read more about this problem on stackoverflow.
Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts
Friday, December 23, 2011
Monday, December 12, 2011
Friday, July 29, 2011
Get links from a page with bash
Get anchor elements from a webpage can be done with
where you replace URL with your choice, just like here:
curl URL 2>&1 | grep -o -E 'href="([^"#]+)"' | cut -d'"' -f2where you replace URL with your choice, just like here:
curl http://cookpad.com 2>&1 | grep -o -E 'href="([^"#]+)"' | cut -d'"' -f2
Tuesday, July 26, 2011
Switching back to the previous application with AppleScript
If you want to simulate cmd + tab with AppleScript, here it is
You can use the same thing in bash
tell application "System Events"
tell process "finder"
activate
keystroke tab using {command down}
end tell
end tell
You can use the same thing in bash
echo 'tell application "System Events"
tell process "finder"
activate
keystroke tab using {command down}
end tell
end tell' | osascript
Friday, June 24, 2011
Image width/height with Imagemagick in Command line
Imagemagick comes handy when you need information from an image.
More information on Imagemagick's identify.
# width
w=`identify -format "%w" image.png`
# height
h=`identify -format "%h" image.png`
More information on Imagemagick's identify.
Sunday, May 15, 2011
bash history
Bash history preserved in ~/.bash_history there are a few ways to search it.
the output would be like that
the followings will reference to line '502 git st'
Or press Ctrl-R, and as you start typing the search goes back in reverse order to the first command that matches the letters you’ve typed.
Once you’ve found the command you have several options:
- Run it verbatim – just press Enter
- Cycle through other commands that match the letters you’ve typed – press Ctrl-R successively
- Quit the search and back to the command line empty-handed – press Ctrl-G
if you're using the history a lot you may want to add these line to your bash init file (~/.bash_profile)
I copy and pasted from the following articles
working-with-history-in-bash, bash-history-reverse-intelligent-search, man history
i like Bash a lot.
history | grep
history | grep gitthe output would be like that
475 git checkout -f
479 gitx
481 git co sp/sprint1
487 git co master
488 git branch -D sp/sprint1
489 git br
491 git co sp/sprint1
496 git diff | gitx
502 git st
510 history | grep git
the followings will reference to line '502 git st'
> !502
# counting back from the last one
> !-1
# same as !-1
> !!
using the last event starting with 'git'
> !git
Or press Ctrl-R, and as you start typing the search goes back in reverse order to the first command that matches the letters you’ve typed.
Once you’ve found the command you have several options:
- Run it verbatim – just press Enter
- Cycle through other commands that match the letters you’ve typed – press Ctrl-R successively
- Quit the search and back to the command line empty-handed – press Ctrl-G
if you're using the history a lot you may want to add these line to your bash init file (~/.bash_profile)
# remove duplicates from the history
export HISTCONTROL=erasedups
# increases the history size
export HISTSIZE=10000
# ensures that when you exit a shell, the history from the session is appended to bash_history
shopt -s histappend
I copy and pasted from the following articles
working-with-history-in-bash, bash-history-reverse-intelligent-search, man history
i like Bash a lot.
Thursday, April 21, 2011
Get HTTP status code with curl in bash
curl --write-out %{http_code} --silent --output /dev/null URL
and then wrapping it into a function
getHTTPCode () {
echo $(curl --write-out %{http_code} --silent --output /dev/null $1)
}
response=$(getHTTPCode URL)
echo $response
Saturday, March 26, 2011
Turning off case-sensitive tab completion in bash
/etc/inputrc deals with the mapping of the keyboard for certain situations.
Adding
to the file turns off case-sensitivity in tab completion.
Adding
set completion-ignore-case on
to the file turns off case-sensitivity in tab completion.
echo set completion-ignore-case on | sudo tee -a /etc/inputrc
Monday, February 28, 2011
Installing NodeJS from source on Snow Leopard
git clone https://github.com/joyent/node.git
cd node
mkdir ~/local
./configure --prefix=$HOME/local/node
make
make install
export PATH=$HOME/local/node/bin:$PATH
Now if you open a new terminal window
node --version
you should see the new version number.
Get page response time with bash
(time curl URL --head) 2>&1 | grep real | cut -c 6-
for instance
(time curl http://google.com --head) 2>&1 | grep real | cut -c 6-
the response is something like this
0m0.072s
Monday, February 7, 2011
Editing files in the user's path
I have lots of scripts in my path but the actual location differs.
If i have to edit them i often do
for example
The which utility takes a command name and searches the path for each executable file that would be run had this commands actually been invoke.
If i have to edit them i often do
vim `which FILE_NAME`for example
vim `which port_utility`The which utility takes a command name and searches the path for each executable file that would be run had this commands actually been invoke.
Wednesday, February 2, 2011
Reload the browser when files change on OSX
Web front-end development often looks like this, you change the code, which can be HTML, CSS, JavaScript, Ruby whatever and then you head over to the browser and reload it to see the changes.
This is not a big deal, but what if the browser could automatically does the reload.
My solution has three parts.
In my case this looks like this
You can find the reload_browser.applescript here and the focus-textmate.applescript here.
This is not a big deal, but what if the browser could automatically does the reload.
My solution has three parts.
- You can detect file change with Kicker.
- Set the focus to the browser and reload it with AppleScript.
- Set the focus back to your texteditor.
kicker -e "osascript reload-browser.applescript;focus-textmate.applescript" FOLDER_TO_WATCH
In my case this looks like this
kicker -e "osascript ~/workspace/MacGyver/reload-browser.applescript;osascript ~/workspace/MacGyver/focus-textmate.applescript" src/
You can find the reload_browser.applescript here and the focus-textmate.applescript here.
Tuesday, January 25, 2011
Telling time in different timezones with bash on OSX
You can use the TZ variable to affect the execution of date command.
Current time in Japan Standard Time (JST)
Pacific Standard Time (PST)
Eastern Standard Time (EST)
Central Standard Time (CST)
Coordinated Universal Time (UTC) / Greenwich Mean Time (GMT)
Central European Time (CET)
Here is how i use it. telltime.sh
Current time in Japan Standard Time (JST)
TZ=Asia/Tokyo datePacific Standard Time (PST)
TZ=America/Los_Angeles dateEastern Standard Time (EST)
TZ=America/New_York dateCentral Standard Time (CST)
TZ=America/Chicago dateCoordinated Universal Time (UTC) / Greenwich Mean Time (GMT)
TZ=Europe/London dateCentral European Time (CET)
TZ=Europe/Budapest dateHere is how i use it. telltime.sh
Thursday, January 6, 2011
Iterating through an array in bash
locations=( Roppongi Shibuya Ebisu )
for name in ${locations[@]}
do
echo $name
done
Sunday, June 27, 2010
Finding in files
Finding a string in files is useful and not that straightforward. But don't despair, we have bash.
This searches in the current directory including its subdirectories and finds files that contain foobar.
find . -exec grep -q 'foobar' '{}' \; -print
This searches in the current directory including its subdirectories and finds files that contain foobar.
Tuesday, June 15, 2010
Monitoring file system activity on OS X
My hard disk is quite slow, so It seemed a good idea to see what's going on under the hood, why the filesystem is so busy.
I came across fslogger, which is a great user-space program that subscribes to the kernel's file system change notification service.
Sounds great and actually monitoring file system activity with fslogger is quite easy. It must be run as root but that's it.
The output is little bit verbose but you can cut it with awk.
Helpful.
I came across fslogger, which is a great user-space program that subscribes to the kernel's file system change notification service.
Sounds great and actually monitoring file system activity with fslogger is quite easy. It must be run as root but that's it.
The output is little bit verbose but you can cut it with awk.
# changed files
sudo fslogger | awk '/FSE_ARG_STRING/ { print $5 }'
# file change type
sudo fslogger | awk '/type.*=/ { print $3 }'
# process that caused the change
sudo fslogger | awk '/pid.*=.*\(.*\)/ { print $4$5 }'
Helpful.
Labels:
bash,
command line,
filesystem,
mac,
osx
Monday, June 14, 2010
Unix sort by column
Sort is a very useful unix utility. Simplest usage is here::
but that's not very interesting. Let's say we want to sort all the files by size:
What this command does exactly sort by 5th field using numeric order. Another useful option you may use is -t which allows you to select the field separator.
ls | sort but that's not very interesting. Let's say we want to sort all the files by size:
ls -l | sort -k5 -nWhat this command does exactly sort by 5th field using numeric order. Another useful option you may use is -t which allows you to select the field separator.
Subscribe to:
Posts (Atom)