You are not logged in.
Merging with the One-Liners thread...
Offline
I find myself using tmux sessions for each project I'm working on and I like to have a consistent window layout (term in window 0 and code in window 1). I initially wrote a very simple script to rename my current tmux session and created the windows. I recently refactored it to handle cases where I'm not in tmux or if the session already has more than one window.
#!/bin/bash
session=$(basename $PWD)
if [ -z $TMUX ]; then
tmux new-session -s $session \; rename-window "term" \; new-window -n "code"
else
tmux rename-session $session
tmux set -t $session default-path $PWD
tmux rename-window -t $session:0 "term"
if [ $(tmux list-windows -t $session | wc -l) -gt 1 ]; then
tmux rename-window -t $session:1 "code"
else
tmux new-window -t $session:1 -n "code"
fi
fi
Offline
I usually repeat bash commands quite a lot (eg. when debugging a program I run the same command to compile it many times). I've made this script to erase duplicated consecutive lines from the user's .bash_history so as to navigate smoothly through all the commands in the history. It also creates a backup of the former .bash_history file.
Hope it helps
#! /usr/bin/python2
from os.path import expanduser
import os
home = expanduser('~') # Get home directory
try:
f_in = open(home + '/.bash_history', 'r')
except:
raise IOError('No .bash_history file found')
f_out = open(home + '/.bash_history.new', 'w')
last = None
for line in f_in.readlines():
if line != last:
last = line
f_out.write(line)
f_in.close()
f_out.close()
cmd = 'mv ' + home + '/.bash_history ' + home + '/.bash_history.backup'
os.system(cmd)
cmd = 'mv ' + home + '/.bash_history.new ' + home + '/.bash_history'
os.system(cmd)
Edit: I know it could be done with a single bash script line, I've made it just for fun (and because one of the scripts I've found didn't work at all).
Last edited by gugah (2013-02-15 19:48:45)
"The problem with quotes on the Internet is that it is hard to verify their authenticity." ~ Abraham Lincoln
Offline
@gugah
bash has it's own method for ignoring consecutive duplicate entries that you can add to .bashrc
export HISTCONTROL=ignoredups
not as much fun as making your own python script though...
You're just jealous because the voices only talk to me.
Offline
@moetunes
thanks for that tip!
"The problem with quotes on the Internet is that it is hard to verify their authenticity." ~ Abraham Lincoln
Offline
Two almost trivial utilities that have saved me quite some typing over the years are the following shell functions:
# mkdir and cd into new dir:
# for bash:
function mkcd() { mkdir "$@" && cd "${!#}"; }
# for zsh:
function mkcd() { mkdir "$@" && cd "${argv[${#argv}]}"; }
# leave and remove empty directory:
function rmcd() { cd .. && rmdir "${OLDPWD##*/}"; }
Using the last argument as the target directory in mkcd allows to use the -p option of mkdir, or to create multiple directories if you want.
Officer, I had to drive home - I was way too drunk to teleport!
Offline
In bash 'mkdir -p a/b/c && cd $_' works too and it's slightly shorter.
Offline
Yes, you are right. I had never used $_, but rather Alt-. to retrieve the last argument interactively. Now that I try it out, the same works for zsh too. Yay, size optimisation!
Officer, I had to drive home - I was way too drunk to teleport!
Offline
Convert from tabs to n-space, n-spaces to n-spaces, and space-n to tabs. I call it PeaceKeeper, because it's purpose is to keep peace between programmers.
Example:
pkeep -f s2s -s 4 -t 2 -n INFILE.TXT > OUTFILE.TXT
This would translate n-spaces to n-spaces (-f s2s.) It would take every 2-space (-t 2) indent, and turn it into 4-space (-s 4.) Then the output is simply redirected to OUTFILE.TXT. If you give the --self/-i flag, the output is written to the original file instead (-n INFILE.TXT)
It be downloaded from here, released under the GPL
SourceForge repo
Meh.
Offline
I wanted to open terminals in the most recently visited directory by default.
Since I'm using z, I thought of reusing its data file for that purpose:
#! /bin/dash
pgrep -x urxvtd > /dev/null || urxvtd -q -o -f
_Z_DATA=${_Z_DATA:-"$HOME/.z"}
if [ $# -eq 0 ] ; then
recpth=$(awk -F '|' '{print $3, $1}' "$_Z_DATA" | sort -n | tail -n 1 | cut -d ' ' -f 2-)
[ -d "$recpth" ] || recpth=$HOME
urxvtc -cd "$recpth"
else
urxvtc $@
fi
Last edited by bloom (2013-02-26 11:01:37)
Offline
My father receives many stupid and funny powerpoints from his friends. The best and quickest solution to read them is still using the Powerpoint Viewer distributed by Microsoft through Wine. Sadly there is no way to make Thunderbird open powerpoints directly with this program. So I tricked Thunderbird by telling it to open powerpoints using a script that opens the last powerpoint copied in the /tmp folder:
#!/bin/bash
PPTFILE=$(ls -t /tmp | grep .pp | head -1)
cd /tmp
wine "~/.wine/drive_c/Program Files (x86)/Microsoft Office/Office12/PPTVIEW.EXE" "$PPTFILE"
Offline
My father receives many stupid and funny powerpoints from his friends.
I can't believe there are enough stupid and funny Powerpoint presentations to necessitate a script.
Anyway, nice hack.
Offline
while true; do xsetroot -name $(( $(cat ~/keyboard1.txt | wc -c) / 96)); sleep 1; done &
sudo cat /dev/input/by-id/usb-046d_G15_Gaming_Keyboard-event-kbd > ~/keyboard1.txt &
Change the usb-046d_G15_Gaming_Keyboard-event-kbd value to your keyboard. This will count keys in DWM xrootset variable. I stole most of the code from somewhere, but credit to that guy!
*Note:I figured out this might not be optimal for SSDs, you could expand this into a small ramfs.*
add cat .hipsterarch to your bashrc, or zshrc
Copy this file to .hipsterarch
@@@ @@@ @@@@@@ @@@ @@@@@@
@@! !@@ @@! @@@ @@! @@! @@@
!@!@! @!@ !@! @!! @!@ !@!
!!: !!: !!! !!: !!: !!!
.: : :. : : ::.: : : :. :
@@@@@@ @@@ @@@ @@@ @@@@@@ @@@@@@@
!@@ @@! @@! @@! @@! @@@ !@@
!@@!! @!! !!@ @!@ @!@!@!@! !@! @!@!@
!:! !: !!: !! !!: !!! :!! !!:
::.: : ::.: ::: : : : :: :: :
@@@@@@@ @@@ @@@ @@@ @@@ @@@@@@@ @@@ @@@ @@@@@@@@ @@@@@@@@
@@! @@! @@@ @@! @@@ !@@ @@! @@! @@! @@!
@!! @!@!@!@! @!@ !@! !@! @!@!@ @!! !!@ @!!!:! @!!!:!
!!: !!: !!! !!: !!! :!! !!: !!: !!: !!: !!:
: : : : :.:: : :: :: : : ::.: : : : : :: :::
@@@@@@@ @@@@@@ @@@ @@@ @@@@@@@ @@@@@@ @@@@@@@ @@@@@@@@ @@@@@@@
!@@ @@! @@@ @@!@!@@@ !@@ !@@ @@! @@! @@! @@@
!@! @!@!@ @!@!@!@! @!@@!!@! !@! @!@!@ !@@!! @!! @!!!:! @!@!!@!
:!! !!: !!: !!! !!: !!! :!! !!: !:! !!: !!: !!: :!!
:: :: : : : : :: : :: :: : ::.: : : : :: ::: : : :
Last edited by Beg (2013-03-02 11:37:37)
Offline
Creates a make friendly header dependency graph for C sources:
#! /bin/dash
_deps() {
printf "%s\n" "$1"
local names="$(grep '^#include *"[^"]\+"' "$1" 2> /dev/null | sed 's/[^"]\+"\([^"]\+\)".*/\1/')"
for nm in $names ; do
_deps "$nm"
done
}
suffix=.o
while getopts 'hs:' opt ; do
case $opt in
s)
suffix=$OPTARG
;;
h)
printf "%s [-h|-s SUFFIX] CSOURCE ...\n" "${0##*/}"
;;
esac
done
shift $((OPTIND - 1))
while [ $# -gt 0 ] ; do
printf "%s:" "${1%.*}$suffix"
for nm in $(_deps "$1" | sort -t . -k 2 | uniq) ; do
printf " %s" "$nm"
done
printf "\n"
shift
done
Can be used like this:
deps *.c > Dependencies
And then, in your Makefile:
include Dependencies
EDIT
In fact, gcc -MM is roughly equivalent to deps!
Last edited by bloom (2013-03-20 08:14:55)
Offline
"is" written in C.
Is basically checks to see if files exist.
Part of my lovely Sethutils (I'm so egotistical).
http://kingdomofseth.com/projects.html
Last edited by sethradio (2013-04-01 21:20:35)
And lo, it came to pass, that the neophyte encountered the Beplattered One and humbly posed the question, "Oh great master, is it a sin to use vi?" And St. IGNUcius didst thus reply unto him, "No, my young hacker friend, it is not a sin. It is a penance."
Offline
"is" written in C.
Is basically checks to see if files exist.
Part of my lovely Sethutils (I'm so egotistical).
http://kingdomofseth.com/projects
What's the purpose of this? Why don't you just use the built-in functions of bash to check for file existence? Apart from you're using printf wrong - you can combine these three commands into one by just writing
printf("File \"%s\" exists.\n", argv[1]);
That's why the program throws a warning when compiling. Also the program always returns 0 - this implies that you have to compare for strings if you would want to use this program in a shell script - only that will take longer than solely checking for file existence.
Consider this as well-meant criticism
Offline
associates each lines of piped content to a number :
#! /bin/bash
# file with saved shortenings
SAVE_FILE="/tmp/shorten"
# locking directory with saved shortenings
LOCK_DIR="/tmp/shorten.lock"
# lock
function LOCKING_DIR() {
if mkdir "$LOCK_DIR"
then
echo 0
else
echo 1
fi
}
# shorten piped lines if no option or -a
if [ "$1" = "" ] || [ "$1" == "-a" ]
then
# lock directory
[ $(LOCKING_DIR) = 1 ] && exit 1
trap 'rm -rf "$LOCK_DIR"' 0
# get lines
[ -f "$SAVE_FILE" ] && lines=$(<"$SAVE_FILE") || lines=""
# get number of lines
[ -z "$lines" ] && num=0 || num=$(echo "$lines" | wc -l)
# read piped content line by line
while read line
do
[ "$line" == "" ] && continue
# prepended pwd if option -a
[ "$1" = "-a" ] && line=$(pwd)/$line
# test if line already in file
match=$(echo "$lines" | grep -nFm1 "$line")
if [ "$match" != "" ]
then
if [ "$line" = "$(echo "$match" | cut -f2- -d:)" ]
then
echo "$match"
continue
fi
fi
num=$(( $num + 1 ))
# add to file
echo "$line" >> "$SAVE_FILE"
# add to lines
[ -z "$lines" ] && lines="$line" || lines=$lines$(echo -e "\n")$line
# echo with shortening
echo "$num:$line"
done
exit 0
fi
# list shortenings if option -l
if [ "$1" = -l ]
then
num=0
[ -f "$SAVE_FILE" ] && grep -n "" "$SAVE_FILE"
exit 0
fi
# clear shortenings if option -c
if [ "$1" = -c ]
then
# lock directory
[ $(LOCKING_DIR) = 1 ] && exit 1
trap 'rm -rf "$LOCK_DIR"' 0
# removing file
rm -f "$SAVE_FILE"
exit 0
fi
# otherway, echo lines with numbers specified in parameters
[ -f "$SAVE_FILE" ] && for var in "$@"
do
case $var in
''|*[!0-9]*) ;;
*) sed -n "${var}p" "$SAVE_FILE" ;;
esac
done
can be used like this ( with the file in /usr/bin/shorten ) :
# ls
bar foo.d 日本
# ls | shorten
1:bar
2:foo.d
3:日本
# ls */ -d | shorten -a
4:/tmp/test/foo.d/
# shorten -l
1:bar
2:foo.d
3:日本
4:/tmp/test/foo.d/
# shorten
Doc, note: I dissent. A fast never prevents a fatness. I diet on cod.
5:Doc, note: I dissent. A fast never prevents a fatness. I diet on cod.
# shorten {1..3}
bar
foo
日本
# rm $(shorten 3)
# ls
bar foo.d
# shorten -c
# shorten -l
#
it's a cleaned version with some changes of what i use
Last edited by ea (2013-03-23 14:27:03)
Offline
You may be interested in vidir from moreutils (not trapdoor's).
Offline
What's the purpose of this? Why don't you just use the built-in functions of bash to check for file existence?
Hmm.. these utilities are made by a novice programmer, that obviously has not yet learned everything about bash.
Apart from you're using printf wrong - you can combine these three commands into one by just writing
printf("File \"%s\" exists.\n", argv[1]);
That's why the program throws a warning when compiling. Also the program always returns 0 - this implies that you have to compare for strings if you would want to use this program in a shell script - only that will take longer than solely checking for file existence.
Consider this as well-meant criticism
Yeah, I've been meaning to fix this for a long time, but I don't see any warnings from the compiler (even with -Wall). Thanks for the reminder though.
Last edited by sethradio (2013-04-01 21:25:04)
And lo, it came to pass, that the neophyte encountered the Beplattered One and humbly posed the question, "Oh great master, is it a sin to use vi?" And St. IGNUcius didst thus reply unto him, "No, my young hacker friend, it is not a sin. It is a penance."
Offline
I have made a little app, that log network traffic:
https://github.com/marazmista/simple-netstat
.: github :. || radeon-profile - radeon oss driver manager || tar-backup - gui for create backups using tar
Offline
pyUTF a python 3 script to convert text files in iso-8859-1 to UTF-8 . Useful to convert sub files downloaded with subdl.
usage : pyUTF <originalFile> <newFile>
#!/usr/bin/python
__author__ = 'felipe'
import sys
def main():
target = sys.argv[1]
output = sys.argv[2]
f = open(target, 'rb')
f2 = open(output, 'wb')
for line in f:
aux = line.decode('iso-8859-1')
line = aux.encode('utf-8')
f2.write(line)
f.close()
f2.close()
if __name__ == '__main__':
main()
Offline
pyUTF a python 3 script to convert text files in iso-8859-1 to UTF-8 . Useful to convert sub files downloaded with subdl.
iconv - character set conversion
iconv -f ISO-8859-1 -t UTF-8 filefoo
Offline
My addition. It's a one-liner to create aliases from the command line, re-source ~/.zshrc and clear the screen.
Note that you must add a source line for the aliases.zsh file manually if you're not an Oh My ZSH user.
function al() {
echo 'alias' $1'='\'$2\' >> ~/.oh-my-zsh/custom/aliases.zsh
source ~/.zshrc
clear
}
Usage:
al aliasname commandname
Note that commandname must be in quotes if it has spaces.
E.g.
al src 'source ~/.zshrc && clear'
Oh, and after you've put this function somewhere, run this for me:
al rmlck 'sudo rm /var/lib/pacman/db.lck'
Last edited by Awesoham (2013-04-16 16:55:36)
Offline
@awesoham
Why do you want us to remove our lock file ?
Never argue with stupid people,They will drag you down to their level and then beat you with experience.--Mark Twain
@github
Offline
Hi,
I recently switched to zsh and I found myself frequently editing .zshrc, and wanting to keep it clean (not echo >> stuff), I wrote this script:
#!/bin/zsh
[ "$#" -ne 2 ] && echo "Wrong number of arguments!" && exit 1;
filterSpaces() {
printf '%q' "$1"
}
param1=$(filterSpaces "$1")
param2=$(filterSpaces "$2")
sed -i '/'"$param1"'/ { a\ '"$param2"'
:a;n;ba;
}' .zshrc
cat ~/.zshrc
source ~/.zshrc
It seeks a pattern (the first parameter) in the file and append the second parameter to the following line. Example .zshrc:
#set options
setopt complete_in_word
#autoload stuff
autoload -U compinit && compinit
#aliases
/usr/local/bin/addOptionsZshrc.sh 'alias' 'alias "add"="/usr/local/bin/addOptionsZshrc.sh "'
#set options
setopt complete_in_word
#autoload stuff
autoload -U compinit && compinit
#aliases
alias "add"="/usr/local/bin/addOptionsZshrc.sh "
The next script is basically the same for deleting stuff (takes a pattern in the line to delete as argument):
#!/bin/zsh
[ "$#" -ne 1 ] && echo "Wrong number of arguments!" && exit 1;
filterSpaces() {
printf '%q' "$1"
}
param1=$(filterSpaces "$1")
sed -i '/'$param1'/ {
//d
}' .zshrc
cat ~/.zshrc
source ~/.zshrc
To alias it: add alias 'alias "del"="/usr/local/bin/delOptionsZshrc.sh "' (quotes are only necessary around strings with spaces) (:
I use it to edit my .zshrc but one could modify the script to take the name of another file in arguments .
Last edited by Resistance (2013-04-15 12:26:32)
Offline