DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 23rd February 2015
Mike-Sanders Mike-Sanders is offline
Port Guard
 
Join Date: Dec 2012
Posts: 37
Thanked 0 Times in 0 Posts
Default Any interesting snippets?

Do any of you have any snippets or scripts you're working on?

Here's some quickies... fav is the last one, acts as a toggle - launches xorg if not currently running, else exits an xterm.

Code:
alias ~      'cd ~'
alias -      'cd -'
alias ..     'cd ..'
alias x      '[ $?DISPLAY -eq 0 ] && xinit || exit'
__________________
Mike's FreeBSD Stuff
Reply With Quote
  #2   (View Single Post)  
Old 23rd February 2015
IdOp's Avatar
IdOp IdOp is offline
Too dumb for a smartphone
 
Join Date: May 2008
Location: twisting on the daemon's fork(2)
Posts: 612
Thanked 14 Times in 13 Posts
Red face

These are (truly!) in my ~/.bashrc :

Code:
alias x=exit
alias X='echo "Turn the Caps Lock off, dummy!"'
Reply With Quote
  #3   (View Single Post)  
Old 24th February 2015
Mike-Sanders Mike-Sanders is offline
Port Guard
 
Join Date: Dec 2012
Posts: 37
Thanked 0 Times in 0 Posts
Default

lol, IdOp's typo reminder alias.

Ahh - spelling & grammar. Me? My work is always error three...

EDIT: Make that 'error free' =)
__________________
Mike's FreeBSD Stuff
Reply With Quote
  #4   (View Single Post)  
Old 24th February 2015
Mike-Sanders Mike-Sanders is offline
Port Guard
 
Join Date: Dec 2012
Posts: 37
Thanked 0 Times in 0 Posts
Default

Here's two ~/.tcshrc keybindings for commandline volume control...

Code:
bindkey -c '^[OP' 'mixer vol +10' # key F1 increase volume 10%
bindkey -c '^[OQ' 'mixer vol -10' # key F2 decrease volume 10%
Some background (regulars reading will know this already)...

These are embedded key sequences which would necessarily require an editor capable of inserting such (I used plain old vi). For instance, to insert the raw keycode for F1, you would type the escape sequence CTRL+V (that's two key strokes - the control key followed by the v key) then press the F1 key.

To create a list of raw keycodes...

. invoke: cat <<end > raw-keys.txt

. type CTRL+V then press a key

. repeat 2nd step to populate your list

. type 'end' to exit & save work to file

Note: The above works great from the physical console, but less so in some terminal emulators.
__________________
Mike's FreeBSD Stuff
Reply With Quote
  #5   (View Single Post)  
Old 1st March 2015
Mike-Sanders Mike-Sanders is offline
Port Guard
 
Join Date: Dec 2012
Posts: 37
Thanked 0 Times in 0 Posts
Default

For quite awhile now I've wanted to backup files on my server -from my local Windows box-

Of course that requires logging into my BSD server, running a backup script, & then logging out. Here, that chore takes about 15mins. during which I'd typically read some news, or maybe play a hand of Golf solitaire. Not unpleasant, but it kept me glued to that spot for the duration... What I needed was a way to automate the process. Make the task totally 'hands-off', 'cuz hey - thats what computers are for no?

To do this I'd need to first run the remote backup script, & then (just to be safe), copy the remote backup to my Win box.

Here then is my strategy...
  • a host running sshd (secure shell)
  • the ability to run a command from my Win box on my server (ssh/scp)
  • lots of error checking to verify the integrity of the backup

Putty (a freeware suite of Windows tools including secure shell client, secure copy, secure ftp) is just the ticket. plink allows you to run a remote command from your localhost, while pscp allows you to copy files to/from a remote host.

With these tools ready, I only needed to copy the backup script to the server (first script), & then launch it from the Windows side (2nd script, 'remote-backup.cmd').

At anyrate, comments are always welcome =)

Save this snippet server-side...

Code:
#!/bin/sh

<<DOC

this script creates a dated tar/gzip archive
of all directories/files in your home directory
in the form of 'user-date.tgz' while leaving
older backup archives intact

to use...

- save script to your home directory as 'backup.sh'
- flip on script's exec bit 'chmod +x ~/backup.sh'
- run the script '~/backup.sh'

DOC

TGZ=$1
OLD=$USER-[0-9]*.tgz

[ -z $TGZ ] && {
   echo backup file not specified...
   exit 1
}

cd $HOME

tar cvzf $TGZ --exclude=$OLD ./ || {
   echo backup error
   exit 1
}

cd -

# eof
Save this snippet client-side...

Code:
@echo off

:: save script on windows as remote-backup.cmd
::
:: requires plink & pscp, available in the putty package at:
:: http://www.chiark.greenend.org.uk/~sgtatham/putty/
::
::
:: you'll need to change these 3 items...
::
:: - remote host address
:: - remote host login name
:: - remote host password

set HST=192.168.0.8
set USR=warby-parker
set PSW=chesseburger

:: note: date formats vary between locales...
:: this script uses the format: year, month, day
:: to determine your local date format invoke:
::
:: echo %date%
::
:: to parse the date follow this example...
::
:: set var=%date:~2,4%
:: echo %var%
::
:: where 'var' is a substring starting from the 2nd
:: character of the main-string, 4 characters wide

:: windows us-english date format *but double check*

set Y=%date:~10,4%
set M=%date:~4,2%
set D=%date:~7,2%
set TGZ=%USR%-%Y%%M%%D%.tgz

:: dont change anything below...

cls
echo REMOTE BACKUP INITIATED...
plink -pw %PSW% %USR%@%HST% backup.sh %TGZ%

if not errorlevel 0 (
   echo ERROR CREATING REMOTE BACKUP...
   exit /b 1
)

echo TRANSFERRING BACKUP TO LOCALHOST...
pscp -pw %PSW% %USR%@%HST%:%TGZ% c:/%TGZ%

if not errorlevel 0 (
   echo ERROR TRANSFERRING BACKUP TO LOCALHOST...
   exit /b 1
)

echo REMOTE BACKUP COMPLETE...

:: eof
__________________
Mike's FreeBSD Stuff

Last edited by Mike-Sanders; 1st March 2015 at 03:44 PM.
Reply With Quote
Reply

Tags
alias, csh, script, shell, tcsh

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
NixOS - an interesting Linux distro. TerryP Off-Topic 2 21st February 2011 07:44 PM
Interesting disscussion on: Solaris vs HP-UX vs AIX vermaden Other BSD and UNIX/UNIX-like 2 19th May 2010 09:33 AM
Other Interesting "Security" Issue on GRUB 2 vermaden News 2 10th November 2009 01:19 PM
An interesting benchmark (kqueue vs. epoll) mdh Programming 0 9th October 2008 09:49 PM


All times are GMT. The time now is 10:16 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2015, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick