Register now and start sharing your code snippets.
-->

Using expect for automation of bulk scp copying.

Shell Script (Bash) posted 5 months ago by marko

Expect can come in handy when you can’t configure ssh public key authentication on the servers :) (and the system “architect” hasn’t yet realized the wonderfulness of a log host).

   1  #!/bin/bash
   2  #
   3  # Usage: script <username> <password> <build>
   4  #
   5  # Example ./copy_logs_from_production.sh marko hubbabubba current
   6  #
   7  
   8  username=$1
   9  password=$2
  10  build=$3
  11  mkdir $build
  12  
  13  instance_1_server=10.0.0.1
  14  instance_2_server=10.0.0.1
  15  instance_3_server=10.0.0.2
  16  instance_4_server=10.0.0.2
  17  instance_5_server=10.0.0.3
  18  instance_6_server=10.0.0.3
  19  instance_7_server=10.0.0.4
  20  instance_8_server=10.0.0.4
  21  instance_9_server=10.0.0.5
  22  
  23  servers=("$instance_1_server" "$instance_2_server" "$instance_3_server" "$instance_4_server" "$instance_5_server" "$instance_6_server" "$instance_7_server" "$instance_8_server" "$instance_9_server" )
  24  i=1
  25  for server in ${servers[@]}; do
  26    expect -c "
  27              # exp_internal 1 # uncomment for debugging
  28              spawn /usr/bin/scp $username@$server:/var/logs/application/$build/server${i}/error.log $build/error-${i}.log
  29              expect { 
  30                "*password:*" { send $password\r\n; interact } 
  31                eof { exit }
  32              }
  33              exit
  34              "
  35    let "i=i+1"
  36  done
  37  

Tagged expect, bash, automation, scp, bulk

Appending strings in bash without the line feed.

Shell Script (Bash) posted 5 months ago by marko

To append to a stream in bash without the trailing line feed use printf to format the output before appending it to the stream.

   1  i=42
   2  FILE=/tmp/atm
   3  printf "%s" "${i}" > $FILE

Tagged bash, printf, streams, linux

Work around for "bash: /bin/rm: Argument list too long"

Shell Script (Bash) posted 6 months ago by christian

Use find instead of rm:

   1  find . -name '*' | xargs rm

The command deletes all files in the current directory.

Tagged rm, delete, bash

Remove ID3 tags from MP3 files recursively.

Shell Script (Bash) posted 11 months ago by marko

Remove id3 tags from MP3 files recursively.

   1  #!/bin/bash
   2  # By Marko Haapala
   3  # requirements:
   4  # id3ed - apt-get install id3ed
   5  IFS=$'\n'
   6  for mp3_file in $(find ./ -type f -iname '*.mp3'); do
   7  	id3ed -r "${mp3_file}"
   8  done

Tagged id3ed, id3 tag, bash

Converting WMA files to MP3 on Linux

Shell Script (Bash) posted 11 months ago by marko

Use this to convert WMA files to MP3 in Linux.

   1  #!/bin/bash
   2  # By Marko Haapala
   3  # converts wma to mp3 recursively. does not delete any static files, so 
   4  # cleanup and renaming is needed afterwards. 
   5  #
   6  # requirements:
   7  # lame    - http://lame.sourceforge.net/download.php
   8  # mplayer - apt-get install mplayer or http://www.mplayerhq.hu/design7/dload.html
   9  
  10  
  11  current_directory=$(pwd)
  12  wma_files=$(find "${current_directory}" -type f -iname "*.wma")
  13  # Need to change IFS or files with filenames containing spaces will not
  14  # be handled correctly by for loop
  15  IFS=$'\n' 
  16  for wma_file in ${wma_files}; do 
  17  	mplayer -vo null -vc dummy -af resample=44100 \
  18  	-ao pcm -ao pcm:waveheader "${wma_file}" && lame -m s \
  19  	audiodump.wav -o "${wma_file}".mp3
  20  	rm audiodump.wav
  21  done

Tagged wma, mp3, convert script, bash