I used to do a lot of path manipulation to set up my development environment (PATH, LD_LIBRARY_PATH, etc), and one part of my environment wasn't always aware of what the rest of the environment needed in the path. Thus resetting the entire PATH variable wasn't an option; modifying it made sense.
The original version of the functions used sed, which turned out to be really slow when called many times from my bashrc, and it could take up to 10 seconds to login. Switching to parameter substitution sped things up significantly.
The commands here don't clean up the path when they are done (so e.g. the path gets cluttered with colons). But the code is easy to read for a one-liner.
The full function looks like this:
remove_path() {
eval PATHVAL=":\$$1:"
PATHVAL=${PATHVAL//:$2:/:} # remove $2 from $PATHVAL
PATHVAL=${PATHVAL//::/:} # remove any double colons left over
PATHVAL=${PATHVAL#:} # remove colons from the beginning of $PATHVAL
PATHVAL=${PATHVAL%:} # remove colons from the end of $PATHVAL
export $1="$PATHVAL"
}
append_path() {
remove_path "$1" "$2"
eval PATHVAL="\$$1"
export $1="${PATHVAL}:$2"
}
prepend_path() {
remove_path "$1" "$2"
eval PATHVAL="\$$1"
export $1="$2:${PATHVAL}"
}
I tried using regexes to make this into a cleaner one-liner, but remove_path ended up being cryptic and not working as well:
rp() { eval "[[ ::\$$1:: =~ ^:+($2:)?((.*):$2:)?(.*):+$ ]]"; export $1=${BASH_REMATCH[3]}:${BASH_REMATCH[4]}; };
Show Sample Output
In order to create, let's say, 10 directories with a single process we can use the command:
mkdir test{1,2,3,4,5,6,7,8,9,10}
something extremely boring to type! Why not use seq?
seq -s, 1 10
and use its output inside the curly braces?
The obvious solution
mkdir test{$(seq -s, 1 10)}
is, unfortunately, too naive and doesn't work. The answer is the order of the shell expansions (feature of Bourne Shell, actually), where brace expansion happens before command substitution (according to Bash's manual).
The solution is to put another level of substitution, using the powerful and mystic command eval.
I found the trick in a similar problem in the post at http://stackoverflow.com/questions/6549037/bash-brace-expansion-in-scripts-not-working-due-to-unwanted-escaping
Got the idea from there http://fixunix.com/questions/15902-bash-checking-if-env-var-set.html Show Sample Output
Scan a file and print out a list of ASCII characters that are not used in the file which can then be safely used to delimit fields. Useful when needing to convert CSV files using "," to a single character delimiter. Piping it into less at the end (which could be redundant) stops the command characters being interpreted by the terminal.
Usage:
watch ls -l
Basic but usable replacement for the "watch" command for those systems which don't have it (e.g. the Solaris I'm trapped on).
Type Ctrl+V to escape the following Ctrl+L which clears the screen. It will be displayed as "^L".
If you're using GNIP as as data source provider for Twitter data in their "Activity Streams" format, use this search to pull out the geo coordinates from tweets as "latitude & longitude". You'll find that splunk creates a "multivalued" field out of the "geo.coordinates{}" field from a tweet. A mulitvalued field is an array, so by using "mvindex(field,position_in_array_starting_with_zero), we can create new fields on the fly for lat/lon. Show Sample Output
Creates a directory and then cds into it directly Show Sample Output
Connects to the last adb connection in history. Show Sample Output
echoprint identify your song, then return artist, song name and album name(release) in a JSON. jq parse it and mp3info set the data in your mp3 file.
of course it depends on:
mp3info
jq
echoprint
You need to set the environment variable
export CODEGEN_NEST_API_KEY='YOUR_ECHONEST_KEY_HERE'
You can use it with find, but probably will bypass the 120 request/minute of developer account key. So, use a sleep to do it.
Something like:
find -name \*.mp3 | while read $f; do eval echo $(echoprint-codegen "$f" | jq ' .[0].metadata | "mp3info -a \"" + .artist + "\" -t \"" + .title + "\" -l \"" + .release + "\" \"" + .filename + "\"" ' ) | bash; sleep 1; done
So this first obtains address of the DBUS session, as it's not available by default over SSH. Then it tells plasma-overlay to exit. `kquitapp` is pretty much an equivalent of the qdbus calls.
Additionally you cal also print the current directory in the end. cdb(){ range=$(eval "echo '{1..$1}'"); toPrint="'../%.0s' $range"; printfToEval=$(echo "printf $toPrint"); toCd=$(eval $printfToEval); eval "cd $toCd"; pwd; } Show Sample Output
This command will display the file, but you can change 'cat' to anything else
(type 'n' when prompted to cancel the command or anything else to proceed).
.
Some hints for newbies:
type
unset bar
to make 'bar' function annihilated.
For permanent usage you can put this (bar) function in your .bashrc (for bash) or in .profile (for sh).
With:
. ~/.bashrc
you can get all new inserted functions in .bashrc (so the function 'bar'
or whatever name you choose) immediately available.
Show Sample Output
The result will be a bunch of 'tail' commands running in the background in your current shell.
alias sp='screen -X eval "chdir $PWD"' # save the path in the current sp # create new window ctrl + ] pwd # has same path as last window
bind -x
was added in Bash 4.0 so this does not work with Bash 3.2 which comes with macOS.
Just an alternative for `acpi -b` Show Sample Output
Avoid eval by creating an variable by adding this to a variable and then call it by $cmd
With this command you can use shell variables inside sed scripts. This is useful if the script MUST remain in an external file, otherwise you can simply use an inline -e argument to sed.
VARNAMES='ID FORENAME LASTNAME ADDRESS CITY PHONE MOBILE MAIL ...' cat customer.csv | while read LINE ; do COUNT=1 for VAR in $VARNAMES ; do eval "${VAR}=`echo $LINE | /usr/bin/awk {'print $'$COUNT''}`" let COUNT=COUNT+1 done done Maybe you have a CSV-File with addresses, where you have to process each contact (one per line, write each value to own variable). Of course you can define every variable, but this way is more simple and faster (to write). VARNAMES includes the variable names. Pay attention: the number of names in VARNAMES have to be the same than in the CSV-file the fields. If the CSV is not seperated with ";", you can set the seperator after the awk-binary with -F"_" for example.
This command uses the top voted "Get your external IP" command from commandlinefu.com to get your external IP address. Use this and you will always be using the communities favourite command. This is a tongue-in-cheek entry and not recommended for actual usage.
commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.
Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.
» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10
Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):
Subscribe to the feed for: