Difference between revisions of "John's hacks"

From ProgClub
Jump to: navigation, search
Line 1: Line 1:
 
[[User:John|I]] decided I might start documenting some hacks I've come across in my travels which I think are neat.
 
[[User:John|I]] decided I might start documenting some hacks I've come across in my travels which I think are neat.
  
== Removing last comma (PHP) ==
+
= PHP =
 +
 
 +
== Removing last comma in PHP ==
  
 
Sometimes you're processing a list to build a string and you want a comma after all items except the last one. You can do that like this:
 
Sometimes you're processing a list to build a string and you want a comma after all items except the last one. You can do that like this:
Line 53: Line 55:
 
  return 'my list: ' . implode( ', ', $list ) . '.';
 
  return 'my list: ' . implode( ', ', $list ) . '.';
  
== Passing shell args (PHP) ==
+
== Passing shell args in PHP ==
  
 
Put your args in $args and use escapeshellarg() to escape them...
 
Put your args in $args and use escapeshellarg() to escape them...
Line 61: Line 63:
 
  exec( "$program $args" );
 
  exec( "$program $args" );
  
== Passing shell args (BASH) ==
+
= BASH =
 +
 
 +
== Passing shell args in BASH ==
  
 
As can be seen for example [https://svn.jj5.net/jjrepo/jj5-bin/branches/0.3/restore.sh here].
 
As can be seen for example [https://svn.jj5.net/jjrepo/jj5-bin/branches/0.3/restore.sh here].
Line 81: Line 85:
 
  if (( a > 0 && a <= 3 )); then ...; fi
 
  if (( a > 0 && a <= 3 )); then ...; fi
 
  if (( a == 4 )); then ...; fi
 
  if (( a == 4 )); then ...; fi
 +
 +
== Finding next available log file in BASH ==
 +
 +
local i=1;
 +
 +
while true; do
 +
 +
  local log=/var/tmp/whatever.sh.log.$i
 +
 +
  [ ! -f "$log" ] && break;
 +
 +
  i=$(( i + 1 ));
 +
 +
done;

Revision as of 00:12, 8 May 2020

I decided I might start documenting some hacks I've come across in my travels which I think are neat.

PHP

Removing last comma in PHP

Sometimes you're processing a list to build a string and you want a comma after all items except the last one. You can do that like this:

// 2020-04-14 jj5 - it's important to handle this case...
//
if ( count( $list ) === 0 ) { return 'list is empty'; }

$result = 'my list: ';
$is_first = true;

foreach ( $list as $item ) {

  if ( $is_first ) {

    $is_first = false;

  }
  else {

    $result .= ', ';

  }

  $result .= $item;

}

return $result . '.';

But that can be simplified like this:

// 2020-04-14 jj5 - it's important to handle this case...
//
if ( count( $list ) === 0 ) { return 'list is empty'; }

$result = 'my list: ';

foreach ( $list as $item ) {

  $result .= "$item, ";

}

// 2020-04-14 jj5 - the neat hack is dropping the last two characters here...
//
return substr( $result, 0, -2 ) . '.';

Although the whole thing can often (but not always) be simplified as something like this (you might also need to check for an empty list):

return 'my list: ' . implode( ', ', $list ) . '.';

Passing shell args in PHP

Put your args in $args and use escapeshellarg() to escape them...

$args = join( ' ', array_map( 'escapeshellarg', $args ) );

exec( "$program $args" );

BASH

Passing shell args in BASH

As can be seen for example here.

local args=();
args+=( --human-readable );
args+=( --acls --xattrs );
args+=( --recursive --del --force );
args+=( --times --executability --perms );
args+=( --links --hard-links --sparse );
args+=( --numeric-ids --owner --group );
args+=( --compress-level=6 );
whatever "${args[@]}";

Numeric if statements in BASH

if (( a == 1 )); then ...; fi
if (( a > 0 && a <= 2 )); then ...; fi 
if (( a > 0 && a <= 3 )); then ...; fi
if (( a == 4 )); then ...; fi

Finding next available log file in BASH

local i=1;

while true; do

  local log=/var/tmp/whatever.sh.log.$i

  [ ! -f "$log" ] && break;

  i=$(( i + 1 ));

done;