executing windows command line operation from python [duplicate] - python-3.x

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to call external command in Python
I want to execute a windows command line operation using python. To execute the command I have to go to a particular directory within my system and then execute the command.
for example
1) Go to a particular directory c:\some\directory
2) then use command somecommand -x -y
I saw some posts on this topic but I was not able to figure them out properly.
Thanks

I assume you want to change the working directory then execute a command. So:
os.chdir(DIRECTORY);
os.system(COMMAND);
os.chdir - Set the current working directory.
os.system - Execute a "system" command.
If setting the working directory is not required you could just specify the full path to os.system.
Also, you might want to check out subprocess as it might be more what you're looking for.

Related

How to run bash file with one word [duplicate]

This question already has answers here:
How do I run a shell script without using "sh" or "bash" commands?
(13 answers)
Closed 1 year ago.
Issue
I want to run a bash file more easily, I've seen some applications where you only need to type word to execute the script.
Instead of typing ~/folder/file.sh in the terminal,
I only have to type a_word to run the file.
Is this possible with bash?
And also, this is on RPiOS's terminal, not sure if it differs.
Save your file to a location named in PATH. /usr/local/bin/a_word (no .sh) is a great example of such a location. Make sure it has executable permissions and starts with a shebang (like #!/usr/bin/env bash).
When you want to install something just for your own account, it's common practice to create a ~/bin directory and add it to your PATH (as by adding something like PATH=$PATH:$HOME/bin in ~/.bash_profile).
You have to define a so called alias.
Edit the file $HOME/.bashrc and add alias a_word = '$HOME/folder/file.sh', then logout and logon again.

Bash script output not showing up during execution? [duplicate]

This question already has answers here:
Can I export a variable to the environment from a Bash script without sourcing it?
(13 answers)
Closed 3 years ago.
I have created a virtual environment on my debian system and i made a script that activates it (should).
However when i execute the script nothing shows up, not even an error, my guess is that it is running in a different shell or something but I don't know how to solve it.
Here is the code of the script
#!/bin/bash
source ~/PythonEnv/environments/my_env/bin/activate
I have changed the permissions already with chmod u+x, so that is not a problem.
When i execute the script nothing shows up at all. Any thoughts???
Add set -x at the beginning of your bash script will do the trick.
-x Print commands and their arguments as they are executed.
You can see more bash options here
http://linuxcommand.org/lc3_man_pages/seth.html
Adding x-permissions is not necessary, since you are using source with an absolute path. Of course this sets the environment only which is executed by the shell script which you have posted here. If you want the changes in your interactive shell, it is pointless to do it inside a script. You have to source the activate script in your shell (respectively inside that process where you want the environment to be modified).

How can I run my script from anywhere(outside the current directory) in my system? [duplicate]

This question already has answers here:
Add a bash script to path
(5 answers)
Closed 4 years ago.
I have my script in /home/testing/program. My script name is test.sh. I want to run my script from other directory. I changed the permissions of the file. In my script I have redirected the output to some(output-test.sh) file. I need to run all my script from other directory. If i run the script in the current directory it works fine. Please help me to do this. Thanks in advance.
In Unix systems, absolute paths start with / . So you can use absolute path to run a script. In your case all you have to do is run this command from the terminal
sh /home/testing/program/test.sh . It does not matter from which directory you run the command from until you use the absolute path of the script.
Also the file in which the output is written should be defined with absolute path in script file. For example, in your case
/home/centos/rr/email-body.txt
so that the file is recognized by the OS even when the script is executed from the other directory or from any directory whatsoever.

bash can't find ./script1.sh when it is called from another script2.sh [duplicate]

This question already has answers here:
Run a script in the same directory as the current script
(4 answers)
Closed 5 years ago.
I have this issue of "./script1.sh not found" when called from another script2.sh which is actually bounded to a keyboard shortcut (control+e).
When I press control+e the script2.sh instantly opens up a new terminal and starts performing its task, but when it has to make a call to script1.sh, it shows this error i.e: sh: 1: ./script2.sh: not found
it works fine when I run it manually in the terminal which is meant to be the current working directory i.e HOME, but it doesn't work when starts from the shortcuts i.e control+e, it perform half its task.
The problem is that these scripts have set different paths as
"./exampleProgam" or "./exampleScript" and doesn't have a full path like "/home/user/program" or "/home/user/script" .
So i don't want to go throw around 70 files and manually change paths one by one to full paths like "/home/user/folder".
I will also have to write extra code in order to get the home dir path.
All these scripts and C programs are placed in one folder i.e folder1 in the home direcory: /home/user/folder1/script2.sh while script1.sh is placed in the
/home/user/script1.sh
.
and where does the terminal shell opens up by default.?? becuase when i press the control+e and the program starts in the shell , it doesn't shows any path etc but just starts executing.
I would strongly recommend you to go ahead and fix the path actually.
Or maybe you could add your code and ask for help on how to edit/modify those lines (maybe using sed, as mentioned in the comments)/
But since you have not shown your code, and if its really that hard to correct it now, maybe you can try adding cd /home/user/script/ as the first line in your script1.sh file. This will change the current directory for further commands.

Cannot run a shell script created on Windows [duplicate]

This question already has an answer here:
Why is a shell script giving syntax errors when the same code works elsewhere? [duplicate]
(1 answer)
Closed 7 years ago.
I seem to be having a weird problem, the solution for which might be dead simple and I am just being blind.
My development environment is Windows. I create a deployment archive file, within which contains a shell script file (called install.sh). I sftp this archive over to a linux environment, untar it and try to run execute the script (after chmod to make it executable) and I get this error:
syntax error: unexpected end of file
I don't notice any errors in the file. I delete this file, create a new install.sh, copy over the exact contents from my Windows env, chmod it again, run it again and this time it runs fine!
I have no idea why it does not run the first time I untar it. Any help appreciated!
Check your file:
cat --show-nonprinting file
Remove carriage returns from Windows/DOS:
tr -d "\r" < file > fixed_file
Linux and Windows uses different end of line (CL or CLRF), or maybe it's an encoding problem. You should check the differences between the running sh and the non-running one.

Resources