Skip to content

Instantly share code, notes, and snippets.

@ychaouche
Created May 20, 2026 12:07
Show Gist options
  • Select an option

  • Save ychaouche/d4abcfd418ea69b8bdfdd76260938c27 to your computer and use it in GitHub Desktop.

Select an option

Save ychaouche/d4abcfd418ea69b8bdfdd76260938c27 to your computer and use it in GitHub Desktop.
13:01:45 ~/NOTES/LOG/WEB -3- $ bash -x type.command mail.spam.stats scp _messagerie:/tmp/
+ (( 3 ))
+ searchfor=mail.spam.stats
+ execute=scp
+ shift 2
++ type -t mail.spam.stats
+ commandtype=file
+ [[ file != \f\i\l\e ]]
+++ type mail.spam.stats
++ string.word.last 'mail.spam.stats is /home/ychaouche/SCRIPTS/mail.spam.stats'
/home/ychaouche/SYNCHRO/bin/type.command: line 53: string.word.last: command not found
+ path=
+ path=
+ args=messagerie:/tmp/
++ dirname ''
+ args=messagerie:/tmp/
+ scp messagerie:/tmp/
usage: scp [-12346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]
[-l limit] [-o ssh_option] [-P port] [-S program]
[[user@]host1:]file1 ... [[user@]host2:]file2
13:03:32 ~/NOTES/LOG/WEB -3- $ type type.command
type.command is hashed (/home/ychaouche/SYNCHRO/bin/type.command)
13:04:19 ~/NOTES/LOG/WEB -3- $ type.cat type.command
#!/bin/bash -i
# we need to be interactive to work with user supplied aliases and functions
# type.command pretty.ip.command cp _ . -> cp /path/to/pretty.ip.command .
# type.command pretty.ip.command chmod +x _ -> chmod +x /path/to/pretty.ip.command
# type.command pretty.ip.command ls -> ls /path/to/pretty.ip.command
# type.command pretty.ip.command file -> file /path/to/pretty.ip.command
# type.command net.ip.reputation ls -lh _
# ----------------- -- -----
# searchfor | |
# execute-+ |
# args
#set -x
if ! (($#))
then
mybasename="$(basename "$0")"
echo "usage:"
echo
echo " $mybasename command execute args"
echo
echo "use _ as a placeholder in args to replace it with the result of type command"
echo "use % as a placeholder for the path of the command (useful for cp, mv...)"
echo "examples:"
echo
echo " $mybasename pretty.ip.command cp _ . -> cp /path/to/pretty.ip.command ."
echo " $mybasename pretty.prompt mv _ %txt.prompt -> mv /path/to/pretty.prompt /path/to/txt.prompt"
echo " $mybasename pretty.ip.command chmod +x _ -> chmod +x /path/to/pretty.ip.command"
echo " $mybasename pretty.ip.command ls -> ls /path/to/pretty.ip.command"
echo
exit 1
fi
searchfor="$1"
execute="$2"
shift 2
# is this a command, function, alias or builtin?
commandtype=$(type -t "$searchfor")
if [[ "$commandtype" != "file" ]]
then
type "$searchfor"
exit
fi
# type $searchfor returns : "$searchfor is /path/to/file".
# This captures "/path/to/file" in path
path="$(string.word.last "$(type "$searchfor")")"
# take care of the hashed paths
# [[ "$path" =~ \(.*\) ]] && path=${path:1: -1}
path=${path//[()]}
# we need to replace "_" with path
args=${@/_/"$path"}
# and replace any "%" with dirname($path)
args=${args/\%/"$(dirname "$path")/"}
# Print the constructed command
# echo "execute='$execute' args='${args[*]}'"
# Execute the constructed command
"$execute" ${args[*]}
13:04:21 ~/NOTES/LOG/WEB -3- $
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment