aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJwanMan <public@mn.900k.es>2017-02-26 17:51:17 +0000
committerGitHub <noreply@github.com>2017-02-26 17:51:17 +0000
commitc3b1972cbf700bd309010944b16176c016547766 (patch)
treea0f37439d1e6fc76ea8a9eabbfb33357ae4d4fbe
parent1a304d5f288d76f548f584b093428b15a02f5072 (diff)
Add files via upload
-rw-r--r--use.desktop7
-rw-r--r--use.sh184
2 files changed, 191 insertions, 0 deletions
diff --git a/use.desktop b/use.desktop
new file mode 100644
index 0000000..279b358
--- /dev/null
+++ b/use.desktop
@@ -0,0 +1,7 @@
+[Desktop Entry]
+Name=Script manager
+Exec=use
+Terminal=true
+Type=Application
+Categories=Utility;
+
diff --git a/use.sh b/use.sh
new file mode 100644
index 0000000..ebc79af
--- /dev/null
+++ b/use.sh
@@ -0,0 +1,184 @@
+#!/bin/bash
+# This file is public domain.
+#
+# This tiny utility can be
+#
+# This is an interactive command you can place on your desktop, menu
+# or wherever.
+#
+# The scripts you manage with this should have a proper sha-bang (#!)
+# line. If you want this to start a GUI, you can use nohup in the
+# command line. Using nohup in the sha-bang has the potential unwanted
+# effects of exploding (if you forget to put the name of the shell
+# after the word 'nohup') or scattering nohup.out files all over the
+# place (if you start it from different places), so it's discouraged.
+#
+# The environment variables affecting this are:
+# $USE_DIR: The directory to store (and read) the scripts. By
+# default, it is '~/.userc'.
+# $EDITOR: The text editor to edit the scripts. By default, it is
+# 'nano'.
+# $SHOW: The default command to show the scripts (not editing). By
+# default, it is 'less' (you can use cat, nano...).
+
+
+# Normalize environment variables.
+USE_DIR=${USE_DIR:-~/.userc}
+[ "$USE_DIR" != / ] && USE_DIR=${USE_DIR%/}
+
+EDITOR=${EDITOR:-nano}
+SHOW=${SHOW:-less}
+
+# If if doesn't exist, create it if possible.
+if [ ! -d "$USE_DIR" ]; then
+ if [ -e "$USE_DIR" ]; then
+ echo "$0: $USE_DIR: Not a directory."
+ exit
+ elif mkdir -p "$USE_DIR"; then
+ echo "$0: Created directory '$USE_DIR'."
+ else
+ exit
+ fi
+fi
+
+# Check read/list permissions.
+if [ ! -r "$USE_DIR" -o ! -x "$USE_DIR" ]; then
+ echo "$0: $USE_DIR: Unable to access directory contents."
+ exit
+fi
+
+cd "$USE_DIR"
+
+# Functions
+user_wait() {
+ echo -n "Press any key to continue..."
+ read
+}
+
+error_ro() {
+ echo "$0: $USE_DIR: Read-only directory."
+ user_wait
+}
+
+read_num () {
+ echo -n "Script number: "
+ read num
+ while [ -z "${use[num]}" ]; do
+ [ -z "$num" ] && return 0
+ echo "$0: Invalid number."
+ echo -n "Script number: "
+ read num
+ done
+ return 0
+}
+
+new_script () {
+ if $writable; then
+ echo -n "Name: "
+ read name
+ [ -z "$name" ] && return 0
+ touch "$name"
+ chmod +x "$name"
+ $EDITOR "$name"
+ else
+ error_ro
+ fi
+}
+
+delete_script () {
+ if $writeable; then
+ read_num
+ rm "${use[num]}"
+ else
+ error_ro
+ fi
+}
+
+edit_script () {
+ read_num
+ while [ ! -w "${use[num]}" ]; do
+ echo "$0: ${use[num]}: Read-only file."
+ read_num
+ done
+ $EDITOR "${use[num]}"
+}
+
+
+rename_script () {
+ if $writeable; then
+ read_num
+ echo -n "New name: "
+ read name
+ [ -z "$name" ] && return 0
+ mv "${use[num]}" "$name"
+ else
+ error_ro
+ fi
+}
+
+show_script () {
+ read_num
+ $SHOW "${use[num]}"
+}
+
+
+# Menu
+menu () {
+ clear
+
+ # Check write permission.
+ if [ -w "$USE_DIR" ]; then
+ writeable=true
+ else
+ echo "NOTE: Running in read-only mode."
+ writeable=false
+ fi
+
+ # Get directory contents
+ typeset -i cnt=0
+ use[0]=""
+ for f in *; do
+ if [ -f "$f" -a -x "$f" ]; then
+ cnt=cnt+1
+ use[cnt]=$f
+ fi
+ done
+
+ # Start listing options
+ if $writeable; then
+ echo " n) New script"
+ echo " d) Delete script"
+ echo " r) Rename script"
+ fi
+
+ echo " e) Edit script"
+ echo " s) Show script"
+
+ for i in `seq $cnt`; do
+ echo -n " $i) ${use[i]}"
+ [ ! -w "${use[i]}" ] && echo -n " (read only)"
+ echo
+ done
+
+ # Ask
+ echo
+ echo -n "Choose an option: "
+ read opt
+
+ case $opt in
+ n|N) new_script ;;
+ d|D) delete_script ;;
+ e|E) edit_script ;;
+ r|R) rename_script ;;
+ s|S) show_script ;;
+ *)
+ if [ -n "${use[opt]}" ]; then
+ "$USE_DIR/${use[opt]}"
+ exit
+ fi
+ esac
+}
+
+while true; do
+ menu
+done