Run text files in terminal
That's called a "shell script."
Add this to the top of your file:
#!/bin/sh
Then execute this command:
chmod +x filename
Then execute it like a program:
./filename
Alternately, you can execute the shell directly, telling it to execute the commands in your file:
sh -e filename
How to call Terminal Commands in a TEXT File?
You have two options.
- Add this into your
package.json
file
...
"scripts": {
...
"start": "npm install express && npm install body-parser && node app.js && ls"
},
Now you can use npm start
to run all these commands at one go.
- Add a bash script in your project directory.
The file should be namedyour-script-name.sh
.
Inside add
#!/bin/bash
npm install express && npm install body-parser && node app.js && ls
You can run the script using ./your-script-name.sh
in your terminal.
Run text file as commands in Bash
you can make a shell script with those commands, and then chmod +x <scriptname.sh>
, and then just run it by
./scriptname.sh
Its very simple to write a bash script
Mockup sh file:
#!/bin/sh
sudo command1
sudo command2
.
.
.
sudo commandn
Running my code in terminal doesn't work because in cannot find one of the files
data.txt
is not in the directory that you are executing this script from.
From your PS1 it looks like you are in your home directory, which is where the program is looking for data.txt
.
Try navigating to the directory with the data.txt
file and then running the same command. Or making the path absolute in the program that you have written.
how can I execute command lines in txt file at Terminal?
Assuming you are using a unix/bash command line.
What you are looking for is a bash/sh script.
I would copy and rename your file.txt
file to file.sh
You will probably need to make it executable. $ chmod u+x file.sh
You will also want to add #!/bin/bash
to the top of your script.
You may need to adjust some syntax but that should be enough to get you rolling.
https://www.linux.com/training-tutorials/writing-simple-bash-script/
you could also try just running the file as is $ sh file.txt
.
How to compile and run Java with jar and txt-files from Terminal
You're doing it wrong. I would REALLY suggest using Maven for something like this. I know it might be too much, but it's worth it. Maven is definitely a standard and it makes everything much easier. :)
1. Mavenize your project
RMB on your project and select Add Framework Support...
and then select Maven from the list, and apply.
You have now a new pom.xml
file. Enter it.
Change your artifactId
and groupId
accordingly.
Apply proper plugins next:
<build>
<plugins>
<!-- That allows you to shade your libraries to your jar !-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- That plugin compiles your jar with Java 8 !-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- That plugin adds a manifest and makes your jar file !-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass> YOUR MAIN CLASS </mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
<!-- This allow Maven to filter your resources folder! !-->
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
(just copy and paste that)
Remember! You need to change YOUR MAIN CLASS
to your proper main class where your main(String[] args)
method is! That allows Maven to create a manifest file (which will allow you to open the jar from terminal)
2. Add your JSoup dependency
Look how easy is that, no downloading, just paste that:
<dependencies>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.7.2</version>
</dependency>
</dependencies>
Wait! Remember to click on Enable auto-import
in the down right corner! That will allow Maven to auto-download your dependencies.
3. Add your files
Put your out.txt
file into resources
folder, and your .java
files into src/main/java/
folder, and you're ready!
Now you can obtain your stream using
ClassLoader loader = YourClass.class.getClassLoader();
InputStream inputStream = loader.getResourceAsStream("out.txt");
Keep in mind that you shouldn't write to that file, only read. If you want to write, then you should create that file elsewhere.
4. Building
To build your jar just open Maven Tools
like so:
And then hit install
command from it:
Wait a little and your jar file ready to use should be in /target/
folder in your root project.
If you have any questions, ask them! I will help you with usage of Maven. ;)
How to run commands on each text file of a directory and delete each text file?
Open a command prompt window and run for /?
. The output help explains how to used command FOR to do something on files found in a directory or with numbers or on various strings read from somewhere.
You can use for your simple task:
@echo off
for %%I in (*.txt) do (
echo File: "%%~fI"
echo del "%%~fI"
)
pause
Replace the first line with command ECHO in command block of FOR by the command line(s) to use on found *.txt file which name is hold by case-sensitive loop variable I
.
Remove echo
on second line in command block if the batch file is working as expected by you.
Note: This batch file just outputs the name of each found *.txt file with full path and the command line which would be used for deleting the *.txt file without doing the deletion.
Import text file to python script to run terminal commands
In order to save the values to a dictionary, you can do the following:
config = dict() # construct an empty dictionary. Fill it with key-value pairs a every iteration
f = open("demofile.txt", "r")
for x in f:
x = x.split(':')
attribute = x[0].strip() # .strip() removes preceding and trailing whitespaces
value = x[1].strip()
config[attribute] = value # save the attribute and its value to the dictionary
In this case config
is the dictionary that has all values of assigned to attribute
as keys and the corresponding value
's as values. I have also added the .strip()
method to the items you read in order to remove any whitespaces (since the way your example.txt is formatted, x[1]
will have values " ubuntu"
and " no"
rather than "ubuntu"
and "no"
).
Now you can construct your if statement like this:
if config['os'] == 'ubuntu' and config['required'] == 'no':
# exec terminal command
elif config['os'] == 'debian' and config['required'] == 'yes':
# exec another terminal command
Related Topics
File Size in Human Readable Format
How to Capture Network Frames in a Kernel Module
How to Get Debugging Symbols Working in Linux Perf Tool Inside Docker Containers
Change System Date Time in Docker Containers Without Impacting Host
Retrieve Plain Text Script from Compiled Bash Script
Determine Usb Device File Path
How to Run an Opengl Application Installed on a Linux MAChine from My Windows MAChine
How to Sort with Multiple Lines in Bash
Debian 8. Failed to Load Iwlwifi
Option to Display Control Characters in Gedit
Using Perf to Monitor Raw Event Counters
Udev - Run Program on Usb Flash Drive Insert
How to Read the Last Line of a Text File into a Variable Using Bash
Compiling a Linux Program for Arm Architecture - Running on a Host Os
Start Rails Server Automatically When Ever I Start My Ubuntu MAChine