Batch files on linux?
I need to create batch file on linux but no idea how to do it.
On dos/windows system it enough to write commands in notepad and then save file as bat. How that going in linux?Is it same? I have batch file with following commands: mkdir directory cp *.* dir1 dir 2 cp *.* dir1 dir 3 cp *.* dir1 dir 4 cp *.* dir1 dir 5 cp *.* dir1 dir 6 cp *.* dir1 dir 7 Now how tu run that file. |
Hi,
You can put all the commands in a file and parse it. I.e: After putting your example commands in a file (called mkcp in this example), you can do the following from the command line: . mkcp That's a dot followed by a space and the name of the file. All the commands inside the file are executed relative to the dir you are in. You can also make it a script by placing this #!/bin/bash as the first line, make it executable (chmod 755 <filename>) and execute it (./<filename>). Hope this helps. |
Erhm. Actually .. you pretty much *have to* specify
Code:
#!/usr/bin/env bash |
Hi,
@introuble: No you don't :) Without the hash-bang commands are executed in the current shell, with a hash-bang commands are executed in a new (child) shell. Code:
$ ls -l tester The lines are given to the shell one at the time and executed as 'one-liners'. But this is just 'dumb' shell behavior and has its limits. An example of different behavior: Put an exit in tester and the shell exits, not the tester file. If it would be a real script (including the hash-bang line) the script would exit, not the shell you ran it from. Hope this clears things up a bit. |
Hi.
Suppose you have a file like this: Code:
# @(#) s4 Demonstrate command file, omitting shebang line. In this example, the command echo is basically a print statement. The text string on the line will be sent to the terminal output device. There are a number of ways to get commands in a file executed. You can try the illustrations below for yourself. (I did a lot of copying and pasting, but I think the actions and the responses are in the correct order.) The permissions on a file can be important. The permissions of this file can be seen with a command like: Code:
ls -l s4 Code:
-rw-r--r-- 1 makyo makyo 82 Apr 21 17:27 s4 As to how you can execute the commands in this file, you could try (case 1): Code:
./s4 Code:
bash: ./s4: Permission denied Code:
. s4 Code:
Hello, world. Code:
sh s4 Code:
Hello, world. Code:
chmod +x s4 Code:
-rwxr-xr-x 1 makyo makyo 82 Apr 21 17:27 s4* Code:
$ ./s4 I usually make the first line in my file a special line, so that my script files look like: Code:
#!/bin/sh Best wishes ... cheers, makyo (edit 1, 2: typos ) |
It was enough to chmod file and it was working hehe
|
By the way, it's not necessary or advisable to use '*.*' as on Linux files may or may not have file extensions. You want to just use '*' instead.
|
@ druuna:
Quite odd. I distinctly remember doing something like: Code:
echo "mkdir test" >> something && . something |
Quote:
"." or "source" allows you to set up "general variables" into your script Ciao |
Quote:
Quote:
Most 'stuff' that are 'always' used are sourced from /etc/profile (or the other login script), the (extended) PATH is an example of that, but every so often you need access to stuff that isn't needed on a daily base. It that case sourcing a specially created file will do the trick. Hope this clears things up a bit. |
All times are GMT -5. The time now is 05:25 PM. |