Batch script in linux

Batch file equivalent in Linux

Solution 3: As another answer already noted, scripts of command line commands are called «batch files» on Windows, but the term also means non-interactive processing in general. I guess it should be sifficient to rewrite your script: http://linuxcommand.org/writing_shell_scripts.php Question: Quick backstory: I’m a graduate student, and I know very little (read: almost nothing) about batch files.

Batch file equivalent in Linux

I have a few basic commands that I need to run on a Red Hat Linux 5 Virtual Machine with GNOME to start a couple servers.

cd /home/user/scripts sh runTHISthing.sh pub cd /home/user/logs tailf pub.log 

Not a crazy amount of code but sometimes I restart VM several times a day. In Windows, I would probably just create a batch file and put it on my desktop. Then it’s just a matter of double clicking and off it goes.

Is there anyway to do the same in Linux? I’ve tried to create a launcher that just runs the emtpy file in terminal with no result.

In UNIX, a ‘batchfile’ is known as a shell script. A typical BASH shell script will start with a magic line that tells the operating system which shell to execute the script with, so your example would end up:

 #!/bin/bash cd /home/user/scripts sh runTHISthing.sh pub cd /home/user/logs tailf pub.log 

NOTE: After you create the file, you have to mark it as executable, in order for the OS to try executing it:

Scripting — batch file equivalent for this code, A straightforward translation of your C code in batch would probably look more like the following, though: @echo off set /p i= :loop set /p j= echo %j% …

What’s the Linux equivalent to Windows batch files?

I have a batch file which I use to create an . apk file on my Windows machine. Now I need to be able to create the .apk file in Ubuntu but I don’t know how translate my .bat file to a script to be able to run it on Ubuntu.

Below is the batch file which works fine on Windows. Will you please give me some hints on how I can run it on Ubuntu?

@echo off set PAUSE_ERRORS=0 :user_configuration :: Path to Flex SDK set FLEX_SDK=C:\sdk\flex_sdk_4.5.1.21328 :: Path to Android SDK set ANDROID_SDK=C:\sdk\android :validation if not exist "%FLEX_SDK%\bin" goto flexsdk if not exist "%ANDROID_SDK%\platform-tools" goto androidsdk goto succeed :validation if not exist "%FLEX_SDK%\bin" goto flexsdk if not exist "%ANDROID_SDK%\platform-tools" goto androidsdk goto succeed :flexsdk echo. echo ERROR: incorrect path to Flex SDK echo. if %PAUSE_ERRORS%==1 pause exit :androidsdk echo. echo ERROR: incorrect path to Android SDK in 'bat\SetupSDK.bat' echo. if %PAUSE_ERRORS%==1 pause exit :succeed set PATH=%PATH%;%FLEX_SDK%\bin set PATH=%PATH%;%ANDROID_SDK%\platform-tools :: Android packaging set AND_CERT_NAME="PeymanApp" set AND_CERT_PASS=fd set AND_CERT_FILE=cert\SampleApp.p12 set AND_ICONS=icons/android set AND_SIGNING_OPTIONS=-storetype pkcs12 -keystore "%AND_CERT_FILE%" -storepass %AND_CERT_PASS% :: Application descriptor set APP_XML=application.xml :: Files to package set APP_DIR=bin set FILE_OR_DIR=-C %APP_DIR% . :: Your application ID (must match of Application descriptor) set APP_ID=air.com.doitflash.SampleApp :: Output packages set DIST_PATH=dist set DIST_NAME=PeymanApp :validation %SystemRoot%\System32\find /C "%APP_ID%" "%APP_XML%" > NUL if errorlevel 1 goto badid goto end_validation :badid echo. echo ERROR: Application ID (APP_ID) does NOT match Application descriptor '%APP_XML%' (id) echo. :end_validation set TARGET= set PLATFORM=android ::call bat\Packager.bat if "%PLATFORM%"=="android" goto android-config :android-config set CERT_FILE=%AND_CERT_FILE% set SIGNING_OPTIONS=%AND_SIGNING_OPTIONS% set ICONS=%AND_ICONS% set DIST_EXT=apk set TYPE=apk goto start :start if not exist "%CERT_FILE%" goto certificate :: Output file set FILE_OR_DIR=%FILE_OR_DIR% -C "%ICONS%" . if not exist "%DIST_PATH%" md "%DIST_PATH%" set OUTPUT=%DIST_PATH%\%DIST_NAME%%TARGET%.%DIST_EXT% :: Package echo true echo. call adt -package -target %TYPE%%TARGET% %OPTIONS% %SIGNING_OPTIONS% "%OUTPUT%" "%APP_XML%" %FILE_OR_DIR% -extdir lib/ echo. if errorlevel 1 goto failed goto end :certificate echo Certificate not found: %CERT_FILE% echo. if %PAUSE_ERRORS%==1 pause exit :failed echo APK setup creation FAILED. if %PAUSE_ERRORS%==1 pause exit :end 

You need to learn to write bash scripts . These are the Linux equivalent of windows batch files . The syntax isn’t too difficult to get your head around. I suggest googling as there are many great freely accessible tutorials .

Note that you will need to change your windows-style paths to Linux style. E.g.

my_drive_letter:\my_folder\myfile 
/media/my_drive_label/my_folder/my_file 

Источник

How do I create a batch file and run it? [duplicate]

Your question is more likely to receive useful answers if it contains some specifics. Such as what type of data is to be processed, including realistic examples of input and output.

@SergiyKolodyazhnyy The duplicate nominee is closed as too broad. As such the close vote on this one should be «too broad» as well. That said it seems like a reasonable question to me as I was once in the dark myself 🙂

@WinEunuuchs2Unix It is closed but does have an accepted answer, which will give OP what they want, or at least a starting point.

@EliahKagan If a mod agrees perhaps he can reopen that one and then merge the answers from here to there?

@WinEunuuchs2Unix That question, which this received close votes to be duped to, was actually reopened a few hours ago, then closed again as duplicate of that highly voted question. Both of them are listed as «originals» in the duplicate banner here (i.e., this question is duped to both of them). So if answers are to be merged, they’d probably be put there. Do you think that would be useful?

3 Answers 3

To create one use the .sh extension but it doesn’t really matter but it helps future users to quickly determine which file type it is. The bat name is mostly used on Windows but in Linux file name extensions do not really matter. Meaning I can call my file say run.de and it would still run in bash file but I believe it’s a good practice to name them with the .sh file extension.

For the editor, part uses any that is best for you between nano vim gedit emacs , but I believe gedit would be nice to start with.

Источник

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.

Источник

Читайте также:  Отобразить всех пользователей linux
Оцените статью
Adblock
detector