Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

December 08, 2017

CLI Shell Console SublimeText Alternative

icon

I love SublimeText! With features like Ctrl-D, Multi-Cursor, and RegEx-Search&Replace, it’s just the best editor ever. Add tools from PackageControl.io and it’s unbeatable. Yes, Atom is probably on-par (plus it’s free), but I’ve already acclimated to ST and that’s just how it is.

But sometimes i just prefer/need to edit in the shell. Almost always, i use nano; However, when you suddenly need something more powerful, it’s back to SublimeText.

I had known about slap-edit, but it’s just too slow, especially through ssh tunnels.

Enter Suplemon! Wow, it’s a great CLI editor to supplement your workflow. If you already have python and pip, then just sudo pip3 install suplemon - -That’s it. Launch suplemon and check it out.

demo

Yes, I know everyone will say “Vim”, but i don’t have time be that good.

~~~

Written with StackEdit.

May 01, 2017

bash if shift-key pressed

shift-key

I had to write a custom bash launch script for a local machine, but wanted it to do one thing if the shift-key was pressed and another thing if it was not.

After searching, i found that bash is NOT capable of such a thing. However, there was a short and simpe C implementation found here: https://forums.gentoo.org/viewtopic-p-2455159.html#2455159

So after compiling the code (gcc shift_state.c -o shift_state ; chmod +x shift_state) and explicitly running it under sudo (required to access /dev/console), i found it did exactly what was needed.

So the only problem remaining was i didn’t want to run my bash script with sudo. To circumvent such, i ran sudo visudo and added the line myusername ALL=(ALL) NOPASSWD: /home/myusername/scripts/shift_state which would allow me to run sudo ~/scripts/shift_state without entering my password.

Subsequently, it was easy to implement a bash script as needed.

Code:

But wait, there's more!

Such could also be used to customize your XFCE Panel-based launch-bar. (Or any launch-bar for that matter.) For instance, In the past, I've created a panel item for Sublimetext. This is a "Launcher" item with two sublimetext commands, one launches and another launches with the -n parameter for a new window. However, it looks ugly and a bit cumbersome to launch:

With the shift_state method, I have replaced the Launcher commands with a single command: bash -c "if ! (( $(sudo ~/scripts/shift_state) )) ; then /opt/sublime_text/sublime_text %F ; else /opt/sublime_text/sublime_text -n %F ; fi". Now it looks better without a secondary command-arrow, and when I shift-click to launch, it provides me the same function in a quicker workflow way.

~~~

As Always, Good Luck! You can thank me with bitcoin.   

Written with StackEdit.

April 26, 2017

ghetto bash-prompt git-status

There are tons of awesome and beautiful bash-prompt and zsh-prompt git-status scripts out there.
https://duckduckgo.com/?q=bash+prompt+git+status&t=ffab&ia=software

You can find stuff like this for zsh and bash:


But this is NOT that.

I’m not a heavy developer, i just wanted something quick and easy. Enter my “ghetto bash-prompt git-status”



This will NOT change your existing prompt, it simply executes as the last instruction before your prompt is displayed.

If you want something better, look into:
- https://github.com/magicmonty/bash-git-prompt
- https://gist.github.com/clux/864a4168712b9c28515a27de77f7c503
- https://github.com/riobard/bash-powerline
- https://github.com/taketwo/powerline-shell
- https://github.com/brujoand/sbp
- http://volnitsky.com/project/git-prompt/


~~~

July 12, 2016

Erase last BASH command

enter image description here
Ever accidentally type your password on the commandline?
Want something better than editing the .bash_history file?
(Especially when you use cssh, parallel-ssh, psonsole or similar)
Below are some options:
#erase last command (least efficient)
history -d $(history | tail -n 2 | head -n 1 | awk '{print $1}')

#erase last command (more efficient)
history -d $(history | awk 'END{print $1-1}')

#erase last command and self (best)
history -d $(($HISTCMD-2)) && history -d $(($HISTCMD-1))

#ultimately add alias to .bashrc
alias eraselastcmd='history -d $(($HISTCMD-2)) && history -d $(($HISTCMD-1))'

#clear current session history
history -c

#don't save session history starting now
unset HISTFILE

#delete lines containing SOMETEXT from ~/.bash_history
 sed -i '/SOMETEXT/d' ~/.bash_history
Related options that can be set in ~/.bashrc
export HISTCONTROL=ignoreboth         # ignore duplicates and commands with " " (space-prefixed)
export HISTSIZE=                      # unlimited history
export HISTFILESIZE=                  # unlimited history
shopt -s histappend                   # append to history, don't overwrite it
export HISTIGNORE="ls:pwd:exit:date"  # do not record specified commands
-
good luck


Please consider crypto tipping:
  

May 26, 2015

Automated Hamachi Reset BASH Script

On occasion, Hamachi may be in a failed state on your always-on device. Alternately, maybe when awaking from sleep-mode, Hamachi may not be functional but still reporting online.

Thusly, it might be very useful for a cronjob to check the state and reset. This might especially be useful on your remote machines that you need connectivity to. For example maybe a remote machine that is in sleep mode, but WOL is possible from another remotely-accessible LAN device.

I have written a BASH script for checking Hamachi and forcing re-login if necessary. Maybe this script is not all-encompassing, but it’s a good start.

Let’s assume you have 2 or more client IP’s and also assume they are in an always-on state. (If for example you have 4 clients, but only 3 remain always-on, you will only use the 3 that you expect to be on.)

Below is my script which you will need to edit (IP addresses and hamachi network name). It will ping each hamachi neighbor and only reset if ALL are unreachable.  Alternatively it will go-online-only if failures>0 and failures<neighbors.

This script uses bash installed from Entware-NG or Optware or Optware-NG. You will have to heavily modify this script if you prefer the built-in ash shell that is default with Synology.
You should edit, save, and mark the script executable.  (e.g. chmod +x ~/scripts/check-hamachi.sh).

Since the script executes sudo /etc/init.d/logmein-hamachi start , you must add the command to your sudoers file:

Run the command EDITOR=nano sudo visudo and AT THE VERY LAST LINE (or elsewhere if you know what you are doing) add:
 username ALL=(root) /etc/init.d/logmein-hamachi , where username is your account.

Lastly cronjob (crontab -e) it to every 5 minutes under your own user account. (e.g.:*/5 * * * * ~/scripts/check-hamachi.sh)

As always, Good luck!
---

Please consider crypto tipping:
  

May 01, 2015

BASH copy preserving timestamps in Linux and OSX




The cp command annoys me sometimes in that i expect my files to retain their time-stamps.  However, such is not the default.  To set this behavior automatically, aliases may be used.

In a Linux ~/.bashrc file, include the follwing alias:
alias cp="cp --preserve=timestamps"

In an OSX ~/.bash_profile, include the following alias:
alias cp="cp -p"

In both cases you will need to exit and restart bash.

---


Please consider crypto tipping:
  

August 24, 2013

Diskfree Watcher script for Linux (Walkthough)

Problem: VMWare does not report Guest OS freespace, and as such there are no alarms triggered and emailed.

Solution: For Linux, script hourly "df" testing within the OS.

We will script the use of “df” to watch disk free space and perform sendmail if the usage is greater-than or equal-to a specified percentage.


Prerequisite: “df”, “sendmail”, “grep”, “awk”, “cut” -- please install them if not installed.
Prerequisite: You will need a valid mail-relay target or mail server target.

Assuming root account.


note: You will alter the script for your specific “df” output.
note: Your drive device will have to be determined manually.
note: “sendmail” my be in some other path than “/usr/sbin”
note: This was all performed on CentOS, a RedHat clone.  Other distributions may be slightly different.


Let’s first run the “df -h” command (diskfree -human_readable)


In my case, this is:
df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_netflow-lv_root
                      90G   64G   22G  75% /
tmpfs                 939M     0  939M   0% /dev/shm
/dev/sda1             485M  115M  346M  25% /boot
/dev/mapper/vg_netflow-lv_home
                     4.9G  160M  4.5G   4% /home


So I have a choice, I can use “df” against any of the listed file-systems. Each one has a Mount point (alias)


I’m interested in my root drive, so I’ll choose “df -h /dev/mapper/vg_netflow-lv_root”.  I could have as easily chose “df -h /” which would be the same in this case.
df -h /dev/mapper/vg_netflow-lv_root
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_netflow-lv_root
                      90G   64G   22G  75% /


Now what I’m interested in is the Use% (75% here).  I can “grep” for “% /” so that only that specific line is printed.
df -h /dev/mapper/vg_netflow-lv_root | grep "\% \/"
produces
                      90G   64G   22G  75% /


Now I’m still only interested in the 75%, which is the 4th item.  Let’s “awk” that.
df -h /dev/mapper/vg_netflow-lv_root | grep "\% \/" | awk '{ print $4 }'
75%


Now I only see 75%.  But I can’t ‘If-Then’ a number containing the %-sign, so I’ll “cut” it.
df -h /dev/mapper/vg_netflow-lv_root | grep "\% \/" | awk '{ print $4 }' | cut -d% -f1
75


This is the result I want to perform an action on -- so now a script can be written.  Here is mine:
cat /root/scripts/diskwatch.sh
#!/bin/sh
used=`df -h /dev/mapper/vg_netflow-lv_root | grep "\% \/" | awk '{ print $4 }' | cut -d% -f1`
echo "diskspace used: $used%";
if [ $used -ge 93 ] ;
then
/usr/sbin/sendmail "myaccount@mydomain.com" << EOF
From: root@netflow.mydomain.com
To: me@mydomain.com
Subject: Alert: netflow diskspace $used% used.
netflow diskspace $used% used.
EOF
fi


This script prints the usage to screen and also sends mail if the usage if greater-than_or_equal-to 93%.


Note the “used”-variable assignment -- The command is single-back-quoted.  The ` character on the ~ (tilde) key.
Change the trigger-limit (93 here) as you see fit. And also, all your email information as it pertains to you and your server.  Make sure to mark your script executable, in my case
chmod +x /root/scripts/diskwatch.sh


You will need to configure your OS for sendmail.  As, stated above, you will need a valid email server or relay target.  This write-up does not go into such.


Now configure sendmail, In my case, I used postfix sendmail, so I added
relayhost = mail.mydomain.com
to the file “/etc/postfix/main.cf” then restarted the postfix service
service postfix restart


You should test sendmail by typing the part of the script between “then” and “fi” directly in the command-line.


If it works, you are ready to cron-job the script.
crontab -e
add
0 * * * * /root/scripts/diskwatch.sh
for hourly execution.

Good Luck!
-----------------
Please consider crypto tipping:
  

March 25, 2013

Useful Text Processing Commands In Linux BASH





I've come back to this time and time again. I hope you find it useful!

#replace inline-file
sed -i 's/old/new/g' file.txt

#de-duplicate
awk '!x[$0]++' input.txt > output.txt
#OR
perl -ne 'print unless $dup{$_}++;' input.txt > output.txt
#OR
awk '{if (++dup[$0] == 1) print $0;}' input.txt > output.txt

#save only line containing TXT
sed '/TXT/!d' input.txt > output.txt
#OR

grep -F "TXT" input.txt > output.txt
#delete line matching TXT
sed '/TXT/d' input.txt > output.txt
#OR
grep -v -F "TXT" input.txt > output.txt

#trim trailing spaces inline
sed -i 's/[[:space:]]*$//' filename.txt

#delete blank lines
sed '/^[[:space:]]*$/d' input.txt > output.txt

#delete blank lines inline-file
sed -i '/^[[:space:]]*$/d' input.txt

#del blank lines - does not work in ALL cases
sed '/^$/d' input.txt > output.txt

#del blank lines - does not work in ALL cases
awk NF input.txt > output.txt

# trim leading, middle, and trailing spaces (reconstitutes records )
echo '         text     txt   info     ' | awk '{$1=$1}1'


#append text to lines
sed 's/$/APPEND/' input.txt
#OR
cat input.txt | awk '{ print $0 "APPEND" }' > output.txt
#OR
awk '{ print $0 "APPEND" }' < input.txt > output.txt

#output IP's only
grep -E -o '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)' input.txt > output.txt

#output numbered quartet (IP-like)
grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' input.txt

#count occurences
sort input.txt | uniq -c > output.txt

#top 10 stats of unique text, where $1 means 1st column, $2 second column, etc
grep sometext input.txt | grep someothertext | awk '{ print $1 }' | sort -n | uniq -c | sort -rn | head > output.txt

#top 20 stats of IP's performing nslookup to ".ru" sites, where input.txt is from a Windows Server DNS log.
grep -F '(2)ru(0)' input.txt | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' | sort -n | uniq -c | sort -rn | head -n 20 > output.txt

#grep or
egrep 'string1|string2' input.txt
grep 'string1\|string2' input.txt

#grep non-comment, non-empty-lines -- see 'grep or' above
cat /etc/rsyslog.conf | grep -v '^#\|^$'
cat /etc/php.ini | grep -v '^;\|^$'

#inverse head
#if "head" is first 10, then
tail -n +11 input.txt

#inverse tail
#if "tail" is last 10, then
head -n -11 input.txt

#remove lines in file2.txt from file1.txt
awk 'NR==FNR{a[$0]++;next} !a[$0]' file2.txt file1.txt > filtered.txt

#find international characters (possibly limited charset) (e.g. finds íóëã)
grep -P '[^\x00-\x7f]' input.txt

#replace international characters with plain-text (e.g. íóëã to ioea)
perl -C -MText::Unidecode -n -e 'print unidecode( $_)' < input.txt

#replace ` with '
sed "s/\`/'/g" input.txt

#de-timestamping (zero time from datestamp)
echo "2015-06-26 07:33:55" | awk '{$0=substr($0,1,11)"00:00:00"; print $0}'
#OR
echo "2015-06-26 07:33:55" | sed 's/[0-1][0-9]:[0-5][0-9]:[0-5][0-9]/00:00:00/g'

#find files that do not contain TEXT from a set of specific files
find ./ -iname "filename" -exec grep -L TEXT {} \;
# e.g. find src/main/target/ -iname "target.h" -exec grep -L USE_.*_EXTI {} \;

#take a .yml file that has some lines with "income:" <value> and replace the value with 10% of the value
#ex.
#      WATER_WORKER:
#        income: 30.0
#        experience: 100.0
perl -pe 's/(income:) (\d+.*)/($1)." ".($2*0.10)/ge' jobConfig.yml > jobConfig.yml.new



Grep AND, OR, NOT

sed one-liners

PERL Regular Expressions

~~~
As Always, Good Luck! You can thank me with bitcoin.