#display simple prepared list of terminal commands?

1 messages · Page 1 of 1 (latest)

edgy breach
#

This could truly be a bash function and a text file. I can scratch something up later maybe. Function that loads the lines of the text file into an array, assigns then each a number or symbol, then displays the list, waits for input, runs the corresponding like and quits. Ezpz. The hardest bit would be the array methinks.

primal wind
#

which i will do with whatever knowledge remains of the one programming class i took like a decade ago

#

if that's the only option

edgy breach
edgy breach
#

Got this far tonight, will have to do more tomorrow, have to work early:

line-launcher () {
XIFS=$IFS 

linefile=$HOME/.config/runthese 

mapfile -t array <$(cat $linefile)

echo -e "line-launcher runs your choice of line from $linefile\nThere is no error checking, the contents of that file are your responsibility.\n\n\tPlease choose from the following:"

selector=1

# trouble with the display here, probably will switch out for a 'for i in 0..${!array[-1]}'
IFS='\n'
for c in ${array[@]}: 
  do
    echo -e "\t\t$selector\t$c"
done
IFS=$XIFS 

# need to read a new selection variable here

# then use it in an eval(${array[$selection]}) line or something here 
}
edgy breach
#

@primal wind

#
line-launcher () {
# Stash the value of the IFS variable for later
# restoration.
XIFS=$IFS 

# This is the file with your pre-built commands.
linefile=$HOME/.config/launchlines

# Convert the file to an array we can use in the
# script.
mapfile -t array < $linefile

# Usage and warning.
echo -e "line-launcher runs your choice of line from $linefile\nThere is no error checking, the contents of that file are your responsibility.\n\n\tPlease choose from the following:"

# setting IFS here because otherwise the for loop
# will evaluate each space-separated word as an
# individual component to loop through. Setting
# IFS to 'newline' makes the for loop split whole
# lines instead.
IFS=$'\n'
# The iterator which we will be incrementing.
selector=1
# The loop that builds the display.
for c in ${array[@]}: 
  do
    # An if statement could be added here that
    # wouid skip over lines in the file that
    # are blank or contain the word placeholder
    # so that if an item is removed, it does not
    # have to change the selector options for
    # lines farther down the file. I.e., if you
    # remove the 3rd line, then all later options
    # will move up, ruining muscle memory. By
    # excluding blank lines from being displayed,
    # but still incrementing the counter, later
    # items will keep their numbering.

    # Wrap both of these lines in the above if
    # statement, if you decide to add that.

    # Output the choice to the screen
    echo -e "\t\t$selector\t$c"
    # Increment the selector
    let selector++

# Repeat until entire file has been displayed
done

# Restore the IFS variable after finishing the loop.
IFS=$XIFS 

# Get input from the user to select the line to run.
# "Evaluates true" if the input is a number or letter 'q'.
unset number
until [[ $number == +([0-9]) ]] || [[ $number == "q" ]];
  do
    read -r -p "please enter a number (or 'q' to quit): " number
done

# Finally, read the selected line from the file and execute it.
# Awk probably could have done all of this, if I were an actual wizard.
awk -v sel="$number" 'NR == sel { system($0); exit }' $linefile

# function ends here
}


# If the file is being sourced, do nothing, else run the function.
# This allows you to include the file in your .bashrc,
# or run it directly.
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then line-launcher; fi
#

here is the contents of my ~/.config/launchlines:

ls -l /
echo foo bar
uname -a
which bash
#

Which gives me output like this:

[user@host] line-launcher $ ./line-launcher.sh 
line-launcher runs your choice of line from /home/xaxas/.config/launchlines
There is no error checking, the contents of that file are your responsibility.

        Please choose from the following:
                1       ls -l /
                2       echo foo bar
                3       uname -a
                4       which bash:
please enter a number (or 'q' to quit): 2
foo bar
[user@host] line-launcher $ ./line-launcher.sh 
line-launcher runs your choice of line from /home/xaxas/.config/launchlines
There is no error checking, the contents of that file are your responsibility.

        Please choose from the following:
                1       ls -l /
                2       echo foo bar
                3       uname -a
                4       which bash:
please enter a number (or 'q' to quit): 3
Linux trilobyte 6.10.5-arch1-1 #1 SMP PREEMPT_DYNAMIC Thu, 15 Aug 2024 00:25:30 +0000 x86_64 GNU/Linux
[user@host] line-launcher $ ./line-launcher.sh 
line-launcher runs your choice of line from /home/xaxas/.config/launchlines
There is no error checking, the contents of that file are your responsibility.

        Please choose from the following:
                1       ls -l /
                2       echo foo bar
                3       uname -a
                4       which bash:
please enter a number (or 'q' to quit): 4
/usr/bin/bash