How can I automate the execution of a terminal command?

Using applications, configuring, problems
Post Reply
Message
Author
User avatar
Mike Walsh
Posts: 6351
Joined: Sat 28 Jun 2014, 12:42
Location: King's Lynn, UK.

How can I automate the execution of a terminal command?

#1 Post by Mike Walsh »

Afternoon, all.

We all know you can start any executable process from the terminal. You open the terminal, type in the path to the command, then hit 'Enter'.....and the process will start.

If it's a command-line app/program/process, you type the path-to-the-executable, hit 'Enter', and then the app/whatever will run in the terminal, showing the output as it does so. With me so far?

Now then; this is my question. How can I automate that process with a script?

I want to open a terminal (in this case, sakura), then, with the terminal open, execute the app/whatever within the terminal, and see the output progressing in the terminal.....just as though I'd manually typed it in. But without actually typing anything. (Does that make sense?)

Is this possible? I've tried several variations-on-a-theme, using example from AskUbuntu and SuperUser, but all I get is the terminal opening. The app/whatever won't actually run....or if it does, it's not showing me what's happening.

Where am I going wrong?

(Flash; this is probably in the wrong place. Can you move it, if deemed necessary? Thanks.)


Mike. :wink:
Last edited by Mike Walsh on Tue 12 Jun 2018, 18:15, edited 1 time in total.

dancytron
Posts: 1519
Joined: Wed 18 Jul 2012, 19:20

#2 Post by dancytron »

If I understand correctly, the way I've done it (blatantly copied from Fred) is to use 2 scripts:

As an example:
The starter script:
#!/bin/bash

xterm -T "Remove All Chrome Settings" -si -sb -fg white -bg SkyBlue4 -geometry 80x22 -e /usr/local/bin/RemoveAllChromeSettings.sh
The main script
#!/bin/sh

# Deletes all Chrome user settings and caches. Mostly for prior to remaster. Use caution.

echo "CAUTION"
echo "This script deletes all Chrome user settings and cache files for all users."
echo "This is mainly useful for remastering."
echo "If that is not what you want to do, press Control-C now."
read -sp "Press ENTER to continue if you are sure"
clear
echo
set -x #echo on
rm -r /home/cat/.config/google-chrome ; rm -r /home/cat/.cache/google-chrome ; rm -r /home/cat/chrome ; rm -r /home/puppy/chrome ; rm -r /root/.config/google-chrome ; rm -r /root/chrome ; rm -r /home/puppy/.config/google-chrome ; rm -r /home/puppy/.cache/google-chrome ; rm -r /root/.cache/google-chrome ; rm -r /live/image/ChromeSettings/google-chrome
set +x #echo off
read -sp "Press ENTER to close"

User avatar
Mike Walsh
Posts: 6351
Joined: Sat 28 Jun 2014, 12:42
Location: King's Lynn, UK.

#3 Post by Mike Walsh »

Hi, Dan.

Mm. O-kayyyy... yeah, fine.

What I want to be able to do is this: to click on a MenuEntry, to have a terminal open, then for the script execution command to enter, and the script to run, showing its output as it does so. I want to do all this as though I'd opened a terminal, typed a command in, then hit 'Enter'.....but I want this to happen without actually doing all that. Just a single click on a Menu Entry, and it'll automatically happen.

I'm probably not making much sense, am I? :lol:

(You're all thinking to yourselves, 'If he wants to run a command-line app, why doesn't he just open the terminal, type the command in, and hit the Enter key? What's so hard about that???')

That's exactly what I do want to do.....but I would like to automate it by clicking on a script, rather than physically performing those actions.


Mike. :wink:
Last edited by Mike Walsh on Tue 12 Jun 2018, 20:05, edited 1 time in total.

dancytron
Posts: 1519
Joined: Wed 18 Jul 2012, 19:20

#4 Post by dancytron »

That's what it does.

Look in any for Fred's distros in /usr/local/bin at any of the scripts that end with "-start" to see how he uses it.

Remember if you want to see it run, you have to pause it at the end otherwise the script will finish and close the window before you can read it.

mjmikulcik
Posts: 45
Joined: Sat 10 Jun 2017, 00:06

#5 Post by mjmikulcik »

You could try

xterm -hold -e (then whatever path for your executable)

for example:

Code: Select all

#!/bin/sh
xterm -hold -e ppm
The hold flag keeps the terminal from closing after it finishes

User avatar
Mike Walsh
Posts: 6351
Joined: Sat 28 Jun 2014, 12:42
Location: King's Lynn, UK.

#6 Post by Mike Walsh »

@ Dan/mjmikulclk:-

I'll give these a try - probably tomorrow, now - and let you know how I get on. Cheers, guys.


Mike. :wink:

User avatar
ETP
Posts: 1193
Joined: Tue 19 Oct 2010, 19:55
Location: UK

#7 Post by ETP »

Hi Mike,

Something along the lines as in the linked screenshot below may meet your needs.
It is a technique that I have used in all of my DLNA Pups since Nov 2015 without change.

The composite shot is from the current one, DLNA Quirky Xerus64-V2.
Start with the desktop file at the bottom of the screen which points at the
script on the top left. The terminal output (read only) is in the top right window.

Full sized screen-shot here: (Press F11 to toggle into full screen then click on link)

https://drive.google.com/uc?export=view ... VXVs1M-oLb
Regards ETP
[url=http://tinyurl.com/pxzq8o9][img]https://s17.postimg.cc/tl19y14y7/You_Tube_signature80px.png[/img][/url]
[url=http://tinyurl.com/kennels2/]Kennels[/url]

User avatar
nosystemdthanks
Posts: 703
Joined: Thu 03 May 2018, 16:13
Contact:

#8 Post by nosystemdthanks »

im 90% certain i get what youre saying mike, and while the solutions presented here are probably going to do exactly what you want, i will speak more generally and say:

most term window programs (xterm, lxterm, etc) have a command line option to "run a command" when they open.

so you just look up the switches for whichever term window youre using, and then look for the one that runs a command.

some likely have options for staying open after the command is run, or doing the opposite.

once you have the command to open the term with the program already running, you just put that in a script and voila-- a script to run a term with a command in it.

now, if you want to create a script that will do this for all sorts of commands rather than a specific one-- thats also very possible, either through further scripting-- or rox-filer already has this as a feature.

for now i assume you mean you want to open it with a specific command running. yes, thats what the solutions here do.

please note: i realise youre an experienced puppy user and should this come off as condescending, its really not intended-- we are all trying really hard not to be vague or misunderstood in this thread.
[color=green]The freedom to NOT run the software, to be free to avoid vendor lock-in through appropriate modularization/encapsulation and minimized dependencies; meaning any free software can be replaced with a user’s preferred alternatives.[/color]

User avatar
fredx181
Posts: 4448
Joined: Wed 11 Dec 2013, 12:37
Location: holland

#9 Post by fredx181 »

Hi Mike,
Perhaps best is that you give one or more examples of what you'd like to run in terminal.

Fred

User avatar
Mike Walsh
Posts: 6351
Joined: Sat 28 Jun 2014, 12:42
Location: King's Lynn, UK.

#10 Post by Mike Walsh »

fredx181 wrote:Hi Mike,
Perhaps best is that you give one or more examples of what you'd like to run in terminal.

Fred
Fair comment!

What I want to do this with is the attached .pet; a system information tool, which I found on OMG Ubuntu! earlier on yesterday. You install it, then run

Code: Select all

neofetch
.....in the terminal, for 'basic' info.

Code: Select all

neofetch --travis
.....gives more detailed info.

What I want to do is to add a Menu entry, because I think this could prove useful to newbies when we ask them their system info. You d/l it, you click to install, you go into the Menu and click the MenuEntry to run it.....and you get the info required.

Remember, most of these are coming to Puppy (and Linux in general) from Windows. Many of them don't even know what a terminal is, much less where to find it. This way, they get to give us what we ask of them, and they get to see a terminal for the first time.

Myself, I use Sakura.....but I'd set this to use urxvt, 'cos it comes as standard in Pup.

Perhaps I'm simply being over-complicated for the sake of it. Probably just as easy to get them to run it in the terminal first as last..... It's an enormous script, but 90% of it is different sets of ASCII character encoding to represent the logos of all the major Linux distros.....and many of the smaller ones, too. There's even a 'Puppy' logo, though I haven't yet figured out which bits need to be uncommented to make it active..!

----------------------------
nosystemdthanks wrote:please note: i realise youre an experienced puppy user and should this come off as condescending, its really not intended-- we are all trying really hard not to be vague or misunderstood in this thread.
'Experienced'; hmmm.....that's debatable! :lol: Anyway, I'm fairly laid back at the best of times; I don't easily take offence. (Unlike certain individuals from north of the Great Lakes.....who shall remain nameless.)

And I freely admit I don't always express myself as clearly as I could. I know what I want to say, but don't always put it into words as well as I might..... :)



Mike. :wink:
Attachments
neofetch-4.0.2_1-noarch.pet
Neofetch command-line system info tool
(75.06 KiB) Downloaded 70 times

User avatar
nosystemdthanks
Posts: 703
Joined: Thu 03 May 2018, 16:13
Contact:

#11 Post by nosystemdthanks »

urxvt -e sh -c 'neofetch ; read p'
[color=green]The freedom to NOT run the software, to be free to avoid vendor lock-in through appropriate modularization/encapsulation and minimized dependencies; meaning any free software can be replaced with a user’s preferred alternatives.[/color]

User avatar
Mike Walsh
Posts: 6351
Joined: Sat 28 Jun 2014, 12:42
Location: King's Lynn, UK.

#12 Post by Mike Walsh »

@ nosystemdthanks:-

Code: Select all

urxvt -e sh -c 'neofetch ; read p'
.....works a treat. That'll do nicely..! And by referring to an earlier post, and consulting the help file, I have it working how I want in Sakura, with

Code: Select all

sakura --hold -e sh -c 'neofetch ; read p'
So; thanks for the 'inspiration' on that one, too. Just out of curiosity, what do the '-e' and '-c' flags do? (Scratch that, the Sakura help file tells me '-e' is execute (obviously), but '-c'? 'Columns'? Not too sure what that actually does..... And, er....'read p'?)

(See, I may have produced many packages for Puppy & the community, but even there, I tend to go with what's relatively easy to put together, or where it's easy to 'modify' an existing package. I freely admit I'm no more than an enthusiastic amateur (definitely no 'script jockey'); when it comes to relatively simple stuff like this, I'm still learning!)

Anyway; cheers, boys & girls! :)


Mike. :wink:

User avatar
nosystemdthanks
Posts: 703
Joined: Thu 03 May 2018, 16:13
Contact:

#13 Post by nosystemdthanks »

Mike Walsh wrote:I tend to go with what's relatively easy to put together, or where it's easy to 'modify' an existing package. I freely admit I'm no more than an enthusiastic amateur;
i wrote my own programming language with that philosophy-- i mean: when writing the translator itself, i went with what was easy to put together, or where its easy to modify or wrap an existing command.

when i teach, i teach in this style. people can learn how to "go pro" once they are comfortable with the subject. linus torvalds himself started by playing with simple things: https://www.youtube.com/watch?v=S5S9LIT-hdc its obviously a good way to learn.

the educational system (in most of the world, really) creates a visceral fear of learning. making people excited to learn is a lot easier without the fear of making unimportant mistakes (though by the time youre working on mission critical, or studying medicine, some harshness is called for.)

i learned a lot of my foundational gnu/linux skills on this very forum. mounting iso files? learned it here. python? learned it from playing with this friendly educational ide on the sugar platform 3 years later: https://www.youtube.com/watch?v=WLHl0UfXDdo well, then i read "think python" but i probably wouldnt have without the friendly intro. and i already knew basic, from this: https://www.ebay.com/p/Vintage-Hands-on ... 1608184855
[color=green]The freedom to NOT run the software, to be free to avoid vendor lock-in through appropriate modularization/encapsulation and minimized dependencies; meaning any free software can be replaced with a user’s preferred alternatives.[/color]

Post Reply