Batch file in linux

How to create batch files on Windows & Linux to automate small and complex tasks

While working on our computers we sometimes need to do repetitive tasks almost every single day. However, there is a simple way you can automate those tasks by creating batch files. A batch file is a file, which will contain the instructions that need to be executed and you can simply execute the same to get your work done. That is the simplest way you can use a batch file, however, there are even some other ways batch files can be important to you. Depending upon which system or platform you are using, creating and executing a batch file in the subject to vary and if you are using Linux operating system you might already know about these batch files, as most Linux users need to work with batch files quite some time.

In order to create a simplest batch file, you have to open a text editor, enter all the commands save it with the correct extension. After you are done creating the batch file, you can simply execute it and get your work done in no time. Well, lot of manual what is sorted out with batch files. So today, I will be explaining how you can create a batch file on Windows and Linux computers for your everyday tasks and also to open multiple applications at the same time and a number of other purposes. The process of creating batch files is different for Windows and Linux is different. So, I will be explaining the method to create a batch file and execute them one by one.

Create batch files on Windows

  • To create a batch file on Windows, simply open Notepad, Notepad++, or any other text editing app that you use for all your requirements.
  • Now, you can write the commands that you need to execute one by one in separate lines.
  • Here I am creating a batch file, which will create a new folder corresponding to the current date (not the exact date) in the ‘ Daily_work ’ folder on C drive of your Windows computer.
c: (Move to C drive) cd\ (Go to the root of C drive, if the working directory is set to something else in the same drive) cd Daily_work (Navigate to the ‘Daily_work’ folder) mkdir %date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4% (Create the directory with the date)

Batch files on Windows and Linux 10

  • After you are done writing all the commands, you have to save the text file with the extension ‘ .bat ’. I am saving the file with the name ‘ daily_folder.bat ’.

 save the text file with the extension ‘.bat’.

  • Now, it’s time to execute the batch file by double-clicking on it, or by choosing ‘Open’ from the right-click context menu. A Command Prompt window will appear and disappear really fast if things work correctly. If it stops in the middle, there must be something wrong in the batch file. A new folder will be created in the directory that has the current date as its name after the execution is complete.

execute the batch file by double-clicking

  • As the batch file will be created in the current working directory you can even execute the batch file by navigating to the directory on command prompt and by typing in in the name of the batch file along with the extension, which is ‘ .bat ’.

Batch files on Windows

  • Depending upon your requirements, you can add a number of other commands and all those commands will be executed automatically on running the batch file created by you. You can display text on the screen with the ‘ECHO’ command, show a specific title in the command prompt window with the ‘TITLE’ command, or even pause the execution of the commands.
  • There are a number of ways, a batch file can be beneficial for you to make your everyday work, a lot easier.

Create batch files on Linux

Now, let’s find out how you can create a batch file and execute the same on a Linux computer. As the commands on Linux are different from that on Windows and Command Prompt, the overall batch files will also look different in the case of Linux.

  • To create a batch file on Linux simply open your favourite text editor on Linux or use the ‘gedit’ command to use Gedit to create a new text file on your Linux computer. You can even create a batch file using ‘nano’ or ‘vi’ .
  • Now, type in all the commands that correspond to your everyday work.
  • Once you have prepared your batch file, you will have to save the file with any name, but with the extension ‘ .sh ’ at the end of the file name.
  • Let’s use a batch file that will create a directory with the date as the name, just the same that we did in the case of Windows.

mkdir ~/Documents/Daily_work/”$(date +”%d-%m-%Y”)”

Batch files on Windows and Linux 60

  • Now that you have written it, save it as any name, which ‘ daily_bat.sh ’ for me. Make sure, you keep ‘ .sh ’ in the end.

Batch files on Windows and Linux 70

  • Now that the file is created you will have to make the batch file executable by typing in the following command on a Linux terminal.
chmod +x filename.sh 
  • The name of the batch file for me is ‘ daily_bat.sh ’, and thus, my command will be as shown below.
chmod +x daily_bat.sh

Batch files on Windows and Linux 75

  • Now that the batch file has been made executable, you can execute the content of the batch file by typing in the following command.
./daily_bat.sh
  • Here, the ‘ . ’ Correspond to the active directory. You can even replace the dot ‘ . ’ with the path to the batch file.

Batch files on Windows and Linux 80

  • Now, the execution will start.
  • You can also execute the same directly from the graphical user interface, without touching the terminal at all.
  • Open your File Manager on Linux, which is the default Ubuntu file manager for me, and open ‘Preferences’ from the drop-down menu in the taskbar. However, it can be different, depending upon the file manager that you are actually using.

File Manager on Linux

  • Open the ‘Behavior’ tab, and mark the option that says, ‘ Run them ’, corresponding to ‘ Executable Text Files ’.

Executable Text Files

  • Now exit the window.
  • After this, you can simply double click on the batch file, or the file, and the tasks within the batch or executable text file will start getting executed right away.

Creating batch files can surely come in handy in multiple situations, and you might encounter executable text files on Linux while installing some programs, or at the time of running them.

So that was all about how you can automate different small tasks, if not big ones, with batch files on Windows and Linux. Do you have any questions? Feel free to comment on the same below.

Источник

Run commands in linux shell using batch file

batch command is used to read commands from standard input or a specified file and execute them when system load levels permit i.e. when the load average drops below 1.5 . See the average load is lower than 1.5 that’s why the job queue is empty and command executed instantly.

Run commands in linux shell using batch file

I need to be able to run the following commands on a rooted Android phone connected to my PC:

su mount -o rw,remount /system chmod 777 /system/app/ exit exit 

And then I need to execute the following command on my PC:

adb push ..\bin\MyApp_signed.apk /system/app/ 

And finally, I need to do some clean up and reboot the phone:

su chmod 755 /system/app/ reboot exit exit 

How can I create a windows batch file so that those shell commands are passed to the android shell?

adb shell allows you to run a shell command on the device from your computer. If your su command supports the -c option, you can do this:

adb shell su -c "mount -o rw,remount /system" adb shell su -c "chmod 777 /system/app/" adb push ..\bin\MyApp_signed.apk /system/app/ adb shell su -c "chmod 755 /system/app/" adb shell su -c reboot 

If your su command doesn’t support commands, you could make a script to leave on the device that bundles the device side of it.

Useful List of Batch Scripting Commands, What is Batch Scripting? A batch script is a file which contains a sequence of command needs to be executed on windows command prompt, batch script is …

How to run .bat(Batch) Files on Linux (Ubuntu)

//How to run a . bat ( Batch ) File on ubuntu or any linux ——Step 1: Install Wine:Open your Terminal Ctrl

Batch command in Linux with Examples

batch command is used to read commands from standard input or a specified file and execute them when system load levels permit i.e. when the load average drops below 1.5 .

It is important to note that batch does not accepts any parameters.

Other Similar Commands:

  • atq: Used to display the queue of pending jobs(this is because at and batch both uses the same job queue).
  • atrm: Used to remove the specified job from job queue.

  • Executing some commands using batch.
    • See the average load is lower than 1.5 that’s why the job queue is empty and command executed instantly.
    • Use ctrl +d when done giving commands to batch.
  • Execute Batch file in Windows Subsystem for Linux, Is it possible to execute a .bat file from a Windows Subsystem for Linux (eg. Ubuntu)? It is trivial to run an .exe file from a WSL, but I haven’t found …

    Run bat file on remote Windows from Linux

    I am working on continious integration process and I am using Linux platform for my Jenkins but now I need to run bat file on remote pc(Windows) to start automation tests processing and send report. Is there any possibility to do this?

    Run another instance of jenkins on that windows and use a remote trigger plugin to trigger the bat.

    You can use NodeLabel Parameter Plugin + Parametrized Trigger Plugin to run job on one or several Windows machines. To start job on all machines in parallel, read this page

    There’s a bit of a learning curve to this approach, but I suggest installing Cygwin on the Windows box. Among Cygwin’s many ports of UNIX software is the SSH daemon. This is a very detailed tutorial for installing sshd on Windows: http://inside.mines.edu/fs_home/gmurray/HowTo/sshNotes.html . It should be easy to get the SSH client going on Linux.

    Then you’d run something like this: ssh windowsuser@windowsbox ‘c:\path\script.bat’

    Invoking windows batch file from Linux, 1 The most direct way is probably to install an ssh server on the windows box. Cygwin includes an ssh server. Depending on how precise your …

    Invoking windows batch file from Linux

    I have an application running only on Windows and a batch file that launches it. I want to invoke this batch file from Linux, meaning something like Linux batch will launch the windows batch with parameters and this in its turn run my application.

    You could install an ssh server in the windows box (Cygwin has one), then from linux do something like:

    ssh user@windows-box c:/path/to/batch.cmd 

    and that should launch your application in the windows box.

    The most direct way is probably to install an ssh server on the windows box. Cygwin includes an ssh server.

    Depending on how precise your timing needs are, you might be able to have an «at» job on the windows box that runs periodically (every 5 minutes?) and runs if it sees that a particular file exists, deleting the file. Then you could use Samba/smbclient to create the file. You would need to turn on filesharing on the windows box for this to work.

    If the windows box has a web server, you could write a CGI, and trigger it using wget or cURL.

    Our build process currently goes the other way: a windows sever kicks off things on the Linux server using plink (part of PuTTY). You might be able to set something similar up.

    This may cause a security issue. Our information security person did not allow me to invoke any programs directly.

    The safer way is to set up server on Windows computer. This can be a web-server for example. And then invoke your process inside PHP/Perl/Python script.

    How to run a bat file in windows from linux systm, Another solution would be, as @Matthew Strawbridge suggested, to have a script running quietly in the background of the computer, periodically …

    Источник

    Читайте также:  Astra linux special edition freeipa
    Оцените статью
    Adblock
    detector