Command Script

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
lpmorgan1

Command Script

Post by lpmorgan1 »

I can type command line instructions in the terminal window, but I want to do like I used to do in dos.

Instead of memorizing complex commands, I would create a BAT file, sometimes with a string of commands.
Sometimes it was just one command with complex options.

I would then just run the BAT file.

I am certain there is a way to do this in Linux Mint.

Would somebody share it?

Larry of the Traveling Morgans
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
mank_in

Re: Command Script

Post by mank_in »

mauler5858

Re: Command Script

Post by mauler5858 »

You can use bash shell scripting in order to accomplish most of these things.

The basis of a bash script starts like this:

Code: Select all

#!/bin/bash

YOUR CODE HERE
You then need to mark the file executable and then it can be run from the command line via:

Code: Select all

. YOURSCRIPTNAME
I would, however, spend some time learning the terminal and its commands. it will pay off in the long run.
BrianD

Re: Command Script

Post by BrianD »

actually, you can write in Bash (or any other shell) script, perl, awk, python, etc, etc...

instead of naming your script file with a ".BAT" extension (like you did in DOS/Windows), in Linux/Unix you just start the script with a comment that signifies the appropriate interpreter for the script that follows, i.e.:

#!/bin/bash ...or
#!/usr/bin/perl ...or
#!/usr/bin/python ...etc

...so then, an example bash script:

Code: Select all

#!/bin/bash
# this line is a comment, like any other line beginning with a '#' symbol
#
# ...these, too!
ls -hort ~            # ...and, any character after the '#' on any given line is ignored by the interpreter, so you can 'document' your scripts
                         # this line, for instance, does a directory listing of the currently logged-in user's home directory, in human readable, long form, sorted in reverse chronological order
echo "all done!"  # this will print 'all done!' on standard out at the conclusion of the script

# fini
when the editing is complete, just add the 'executable' bit to the script file you created by running "chmod +x <script_file_name>"
-- where <script_file_name> is replaced with the actual name of the script file you've created

...once you've finished your script, and you wish to make it available to others (or just have it handy for yourself), you can place it in /usr/local/bin (which is on the $PATH), and you can then invoke it by name from anywhere on the system.
BrianD

Re: Command Script

Post by BrianD »

...additionally, there are several references on bash scripting, perl scripting, and python scripting available on the internet, as well as several books that will serve as learning material and/or reference. I would heartily recommend any title published by O'Reilly & Associates (you can find them at your local bookstore, on Amazon, or directly from www.oreilly.com (and, if you belong to a Linux User Group, you can take a discount when you purchase online, directly).

some examples (also books that I actually own and have read):
Learning the bash Shell http://shop.oreilly.com/product/9780596009656.do
Learning Perl http://shop.oreilly.com/product/0636920018452.do
Think Python http://shop.oreilly.com/product/0636920025696.do
User avatar
xenopeek
Level 25
Level 25
Posts: 29509
Joined: Wed Jul 06, 2011 3:58 am

Re: Command Script

Post by xenopeek »

Bash is the easiest scripting language to get started with, as it accepts the same commands as you have been using on the terminal. A very good introduction is Introduction to the Command Line. See the link on that page, you can also read if for free online or download it.
Image
lpmorgan1

Re: Command Script

Post by lpmorgan1 »

Regarding:
I would, however, spend some time learning the terminal and its commands. it will pay off in the long run.

Sorry, I am good at figuring things out. Never was good at memorizing. Since I turned 65 it has been worse. Still enjoy learning new things though.
It would be NICE to have a good memory. I just don't.
Locked

Return to “Scripts & Bash”