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

Tuesday, March 31, 2020

Bash: for loop alphabet

Source: For loop with Alphabet
[jerome@jeroboam] > for i in `seq 97 102`; do printf "\\$(printf %o $i)\n"; done
a
b
c
d
e
f

Wednesday, April 3, 2019

macOS: bash error: declare: -A: invalid option

[jerome@jeroboam] > ./avws-stores.sh 
+++ dirname ./avws-stores.sh
++ cd .
++ pwd
+ SCRIPT_DIR=/Users/jerome/src/boss-sms-vas/release
+ REPO_NAME=boss-sms-vas-stores
+ declare -A AWS_S3_BUCKETS
./avws-stores.sh: line 16: declare: -A: invalid option
declare: usage: declare [-afFirtx] [-p] [name[=value] ...]
Reason: The standard macOS bash version is an antiquity.
Source: Upgrading Bash on macOS
[jerome@jeroboam] > brew install bash
[jerome@jeroboam] > sudo vi /etc/shells
/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh
/usr/local/bin/bash
[jerome@jeroboam] > chsh -s /usr/local/bin/bash

Friday, September 19, 2014

Bash: delete last n lines of a file

head -n -100000 FILE > NEWFILE

Friday, September 5, 2014

Delete files older than

E.g. Delete files in current directory older than 90 days :
find . -mtime +90 -delete

Wednesday, January 8, 2014

Compute files size

find . -name "*" -ls | awk '{total += $7} END {print total}'

Wednesday, March 30, 2011

How do I copy a directory structure from one machine to another?

 $ tar cf - some_directory | ssh kramer "( cd /path/to/destination; tar xf - )"

tar cf – some_directory creates a tar file of some_directory but the dash tells tar to write to STDOUT instead of writing to an actual file on disk. The STDOUT from the first tar command is piped to STDIN of the next command. The right hand side of the pipe says to log into the host kramer using ssh and run the commands cd and tar xf -. The trick is with the commands “( cd /path/to/destination; tar xf – )”. The parens create a subshell, in which the current directory is changed to /path/to/destination, and tar xf – reads from STDIN and extracts the tar file. This STDIN is the same STDIN that was sent to us over the pipe from STDOUT of the first tar command. Thus the directory structure on jerry gets tar’d up, transfered to kramer, then extracted all in one fell swoop.

Bash sample init file

# This file controls the behaviour of line input editing for
# programs that use the GNU Readline library.  Existing
# programs include FTP, Bash, and GDB.
#
# You can re-read the inputrc file with C-x C-r.
# Lines beginning with '#' are comments.
#
# First, include any systemwide bindings and variable
# assignments from /etc/Inputrc
$include /etc/Inputrc

#
# Set various bindings for emacs mode.

set editing-mode emacs

$if mode=emacs

Meta-Control-h:    backward-kill-word    Text after the function name is ignored

#
# Arrow keys in keypad mode
#
#"\M-OD":        backward-char
#"\M-OC":        forward-char
#"\M-OA":        previous-history
#"\M-OB":        next-history
#
# Arrow keys in ANSI mode
#
"\M-[D":        backward-char
"\M-[C":        forward-char
"\M-[A":        previous-history
"\M-[B":        next-history
#
# Arrow keys in 8 bit keypad mode
#
#"\M-\C-OD":       backward-char
#"\M-\C-OC":       forward-char
#"\M-\C-OA":       previous-history
#"\M-\C-OB":       next-history
#
# Arrow keys in 8 bit ANSI mode
#
#"\M-\C-[D":       backward-char
#"\M-\C-[C":       forward-char
#"\M-\C-[A":       previous-history
#"\M-\C-[B":       next-history

C-q: quoted-insert

$endif

# An old-style binding.  This happens to be the default.
TAB: complete

# Macros that are convenient for shell interaction
$if Bash
# edit the path
"\C-xp": "PATH=${PATH}\e\C-e\C-a\ef\C-f" 
# prepare to type a quoted word --
# insert open and close double quotes
# and move to just after the open quote
"\C-x\"": "\"\"\C-b" 
# insert a backslash (testing backslash escapes
# in sequences and macros)
"\C-x\\": "\\" 
# Quote the current or previous word
"\C-xq": "\eb\"\ef\"" 
# Add a binding to refresh the line, which is unbound
"\C-xr": redraw-current-line
# Edit variable on current line.
"\M-\C-v": "\C-a\C-k$\C-y\M-\C-e\C-a\C-y=" 
$endif

# use a visible bell if one is available
set bell-style visible

# don't strip characters to 7 bits when reading
set input-meta on

# allow iso-latin1 characters to be inserted rather
# than converted to prefix-meta sequences
set convert-meta off

# display characters with the eighth bit set directly
# rather than as meta-prefixed characters
set output-meta on

# if there are more than 150 possible completions for
# a word, ask the user if he wants to see all of them
set completion-query-items 150

# For FTP
$if Ftp
"\C-xg": "get \M-?" 
"\C-xt": "put \M-?" 
"\M-.": yank-last-arg
$endif

How do I change every occurance of a string in multiple files?

Why, use perl pie!

perl -p -i -e 's/jerry/george/g' *.txt

How can I run long pipe lines of commands on a remote host via SSH without escaping all the meta characters?

$ ssh kramer <<EOF
ps -ef | grep http | awk '{print \$NF}'
EOF

The only tricks here are the use of a bash here document, and the fact that the command line is typed directly into ssh’s STDIN so there’s no need to escape things like pipes and semi-colons, etc. However, notice that you do still need to escape dollar signs because they’ll still be interpreted as shell variables.

How can I run a shell script on a remote host without copying the script out?

One way would be:

$ cat myscript.sh | ssh kramer /bin/sh

This one is pretty simple how it works, but it is often overlooked as an option for getting things done. The shell script is written to STDOUT. /bin/sh is executed on the remote server, and it reads myscript.sh on STDIN, thus executing the local copy of the script. This is way convenient for some things. The only problem I see with this one is that you can’t pass command line arguments to the script.

How do I diff two files on different machines (using bash)?

$ diff <(ssh -n george cat /etc/passwd) <(ssh -n kramer cat /etc/passwd)

This is just the Bash Process Substitution trick.

Tuesday, March 29, 2011

From within a bash executing script, how do I find the directory where the script lives?

A simple pwd doesn’t work because it gives the Present Working Directory, and we want to know the directory where the script actually resides on disk. I can’t remember the previous solution I came up with, but here’s another solution that should work:


#!/bin/bash
dir=$(echo $0 | sed -e "s,\([/]\),$(pwd)/\1,")
echo I live in $dir

Here is how it works (hopefully). $0 is the name of the script as it was executed, so this may be foo.sh, ./foo.sh, /tmp/foo.sh, etc.. This gets sent to sed, which then uses a basic regular expression that says “If the first character of $0 was NOT a forward slash, then prepend $0 with my present working directory ($(pwd)) followed by whatever that first character was (\1), then assign this new value to dir”. If the first character in $0 is a forward slash, then we were invoked via an absolute path and so we don’t want to change anything.

Modified files during last 10 days

find . -name '*' -mtime -10 -print