1

I have a list of commands I would like to paste into the terminal so that they run one after the other.

Initially they run fine, but commands later on are being truncated.

Example command:

ogr2ogr -nlt PROMOTE_TO_MULTI -progress -skipfailures -overwrite -lco PRECISION=no -f PostgreSQL PG:"dbname='natural_earth' host='localhost' port='5432' user='natural_earth' password='natural_earth'" 50m_physical/ne_50m_lakes.shp

There are about 150 of these commands, they are stored in gedit with line wrapping off and are being pasted as a block straight into the terminal.

Expected result:

$ ogr2ogr -nlt PROMOTE_TO_MULTI -progress -skipfailures -overwrite -lco PRECISION=no -f PostgreSQL PG:"dbname='natural_earth' host='localhost' port='5432' user='natural_earth' password='natural_earth'" 50m_physical/ne_50m_lakes.shp
0...10...20...30...40...50...60...70...80...90...100 - done.

This works for about the first 30 commands, after which I get a varying level of truncation on the commands, for example:

$ ogr2o
ogr2o: command not found

$ ogr
ogr: command not found
$ ogr2ogr -nlt PROMOTE_TO_MULTI -progress -skipfailures -overwrite -lco PRECISION=no -f PostgreSQL PG:"dbname='natural_earth' host='localhost' port='5432' user='natural_earth' password='natural_earth'" 50m_physical/ne_50m_graticules_all/ne_50m_grati

FAILURE:
Unable to open datasource `50m_physical/ne_50m_graticules_all/ne_50m_grati'

So I am new to Linux. I was wondering if there was a better way to run these than copy & paste from gedit?

I am running Linux Mint 15 "olivia" Cinnamon 32-bit.

  • 1
    Presuming there's nothing else in the gedit buffer, save it as a file and run it through the shell via sh [filename]. – goldilocks Sep 25 '13 at 20:32
5

Just put the commands you want to run in a special file, and execute it as a shell script. Either by invoking a shell with an argument:

sh your_commands

or by prepending the commands with a hashbang and marking the file as executable with chmod a+x your_commands:

#!/bin/sh
your commands
go
here

which will make it behave as a regular binary and you will be able to just execute

 /path/to/your_commands

Alternatively you can also use the source functionality of your shell, which executes the commands from a file inside of the current shell (as opposed to spwaning a new shell which is what the two above will do):

source your_commands

or

. your_commands

(both have the same meaning).

4

Welcome to Stack Exchange, and also to Linux Mint!

You ask whether there is a better way than paste-into-terminal to run a long series of commands. As it happens, there is: save the commands into a file, and run it as a shell script.

E.g. if you save your commands in ~/scripts/myscript.sh (~ is the abbreviation for your home directory), you can run it by entering these commands:

# change directory to where the script is
cd ~/scripts
# run the script with bash (most scripts are bash scripts)
bash myscript.sh

Pay attention to your working directory

One thing to look out for: the directory from where you run bash myscript.sh will be used as the working directory. If your script talks about the file 50m_physical/lakes.sh, which is in your ~/mylakesproject directory, then this will work:

cd mylakesproject
bash ~/scripts/myscript.sh
# ~/mylakesproject/50m_physical/lakes.sh exists

and this will not work:

cd myotherproject
bash ~/scripts/myscript.sh
# ~/myotherproject/50m_physical/lakes.sh does not exist

and neither will this:

cd scripts
bash myscript.sh
# ~/scripts/50m_physical/lakes.sh does not exist

Good luck, and have fun!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.