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
Fdisk Soldier
 
Join Date: Dec 2012
Posts: 52
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'
__________________
www.tacoshack.xyz
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: 1,027
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
Fdisk Soldier
 
Join Date: Dec 2012
Posts: 52
Default

lol, IdOp's typo reminder alias.

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

EDIT: Make that 'error free' =)
__________________
www.tacoshack.xyz
Reply With Quote
  #4   (View Single Post)  
Old 24th February 2015
Mike-Sanders Mike-Sanders is offline
Fdisk Soldier
 
Join Date: Dec 2012
Posts: 52
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.
__________________
www.tacoshack.xyz
Reply With Quote
  #5   (View Single Post)  
Old 1st March 2015
Mike-Sanders Mike-Sanders is offline
Fdisk Soldier
 
Join Date: Dec 2012
Posts: 52
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
__________________
www.tacoshack.xyz

Last edited by Mike-Sanders; 1st March 2015 at 03:44 PM.
Reply With Quote
  #6   (View Single Post)  
Old 24th October 2016
PapaParrot's Avatar
PapaParrot PapaParrot is offline
parrot
 
Join Date: Jul 2015
Location: Durango, Mx.
Posts: 472
Default

With some help from "perl monks",
I made this, for my grandaughter to practice her
first multiplication tables
a-multiplication.pl
Code:
#!/usr/bin/perl
#use strict;
#use warnings;

print "Por favor escribe tu nuombre:\n";
$name = <>;
chomp($name);
print "Hola!, ", $name, "!\n";
print " $name, Vamos a hacer tablas ,tecla algunas letras o numeros:\n";
$string = <>;
chomp($string);
print "Lo que teclado,tiene:\n",length($string),"  letras o numeros\n";
$_= <STDIN>;
print "[Instrucciones, ctrl-c o \"quit\" para salir]\n";
print "Entra para continuar\n";
$_ = <STDIN>;
OUTER: {print "\033[2J";
    my ($x, $y) = map { int rand $_ } qw/11 11/;
    INNER: {
        print "$x x $y = ";
        local $_ = <STDIN>;
        chomp;
        last OUTER if m/^quit$/;
        redo INNER unless m/^\d+$/;
        if ($_ == ($x*$y))
  { print "Muy buen es correcto!\n"; redo OUTER }
        else
          { print "Oops!No es correcto,haga otra vez\n"; redo INNER }

    }
}
Reply With Quote
  #7   (View Single Post)  
Old 29th October 2016
Head_on_a_Stick's Avatar
Head_on_a_Stick Head_on_a_Stick is offline
Real Name: Matthew
Bitchy Nerd Elitist
 
Join Date: Dec 2015
Location: London
Posts: 456
Default

I don't like display managers so instead of xdm(1) I use this line in ~/.profile:
Code:
[[ -z $DISPLAY && $(tty) = /dev/ttyC0 ]] && exec startx
This will autostart X but only for ttyC0, logins to the other TTYs result in a normal console session, which is useful for when I b0rk my desktop
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 06:24 AM.


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