Cmd file for linux

What is Linux equivalent of CMD /K?

@Celada Thank you, I couldn’t find those when searching, because I was searching for bash , and not for gnome-terminal . Should I delete this question?

2 Answers 2

I found a way. I got it from some stackexchange site but can’t find it back.

Anyway: You can abuse bash’s —init-file option. It is intended to override using some other startup script instead of .bashrc, but you can put anything there. Combined with anonymous pipes, you can do something like this:

After the command ends, you’ll be dropped back into bash — not a new instance of bash like with some other solutions, but the same instance.

Now, this’ll skip .bashrc of course because that’s what —init-file is for. To fix that, you get the tremendously pretty

On the Windows Subsystem for Linux

Finally, I tried doing this from Windows, but got into trouble because when you start bash to invoke the Windows Subsystem for Linux, you’re still in Windows land so anonymous pipes won’t work, $HOME is all wrong, and so on. My workaround for «cmd /k, but for starting the windows subsystem for linux» has become:

bash -c 'exec bash --init-file ^<(echo ". \"$HOME/.bashrc\"; your --command --here")' 

I made a batch file called bash-k.cmd in my PATH so i can invoke it easily, with this inside it:

@bash -c 'exec bash --init-file ^<(echo ". \"$HOME/.bashrc\"; %*")' 

It allows me to do bash-k cowsay hello and things will work

Thank you @skrebbel that is a very helpful command, here is are .reg file codes to add a Bash Console Here right-click context menu if you have Windows Subsystem for Linux

AddContext.reg:

Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\BashConsoleHere] @="Bash Console Here" "Icon"=hex(2):25,00,4c,00,4f,00,43,00,41,00,4c,00,41,00,50,00,50,00,44,00,41,\ 00,54,00,41,00,25,00,5c,00,6c,00,78,00,73,00,73,00,5c,00,62,00,61,00,73,00,\ 68,00,2e,00,69,00,63,00,6f,00,00,00 [HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\BashConsoleHere\command] @="bash -c 'exec bash --init-file <(echo \". \"$HOME/.bashrc\"\")'" [HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\BashConsoleHere] @="Bash Console Here" "Icon"=hex(2):25,00,4c,00,4f,00,43,00,41,00,4c,00,41,00,50,00,50,00,44,00,41,\ 00,54,00,41,00,25,00,5c,00,6c,00,78,00,73,00,73,00,5c,00,62,00,61,00,73,00,\ 68,00,2e,00,69,00,63,00,6f,00,00,00 [HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\BashConsoleHere\command] @="bash -c 'exec bash --init-file <(echo \". \"$HOME/.bashrc\"\")'" [HKEY_CLASSES_ROOT\Drive\shell\BashConsoleHere] @="Bash Console Here" "Extended"="" "NoWorkingDirectory"="" [HKEY_CLASSES_ROOT\Drive\shell\BashConsoleHere\command] @="bash -c 'exec bash --init-file <(echo \". \"$HOME/.bashrc\"\")'" 
Windows Registry Editor Version 5.00 [-HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\BashConsoleHere] @="Bash Console Here" "Icon"=hex(2):25,00,4c,00,4f,00,43,00,41,00,4c,00,41,00,50,00,50,00,44,00,41,\ 00,54,00,41,00,25,00,5c,00,6c,00,78,00,73,00,73,00,5c,00,62,00,61,00,73,00,\ 68,00,2e,00,69,00,63,00,6f,00,00,00 [-HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\BashConsoleHere\command] @="nircmd elevate cmd /c cd \"%V\" && bash.exe" [-HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\BashConsoleHere] @="Bash Console Here" "Icon"=hex(2):25,00,4c,00,4f,00,43,00,41,00,4c,00,41,00,50,00,50,00,44,00,41,\ 00,54,00,41,00,25,00,5c,00,6c,00,78,00,73,00,73,00,5c,00,62,00,61,00,73,00,\ 68,00,2e,00,69,00,63,00,6f,00,00,00 [-HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\BashConsoleHere\command] @="nircmd elevate cmd /c cd \"%1\" && bash.exe" [-HKEY_CLASSES_ROOT\Drive\shell\BashConsoleHere] @="Bash Console Here" "Extended"="" "NoWorkingDirectory"="" [-HKEY_CLASSES_ROOT\Drive\shell\BashConsoleHere\command] @="nircmd elevate cmd /c cd \"%1\" && bash.exe" 

Источник

Читайте также:  Jar чем открыть linux

Принцип создание командных файлов для Linux

Вот в Windows все понятно, два варианта: либо пишем команды непосредственно в cmd, либо создаем текстовый файл с расширение bat, и в нем пишем нужные команды которые будут выполнятся при открытии этого файла . А вот в Linux (Ubuntu) не совсем мне все ясно. Знаю что терминал выступает как в windows командная строка, т. е. я могу в терминале писать нужные последовательности команд. Но вот как создать командный файл в Linux? Чтобы он хранился на компьютере и я в любой момент мог его запустить ( ибо каждый раз набирать в терминале одно и тоже не очень улыбается) . В общем вопрос: как в Linux создать командный файл?

Создаешь файл с любым именем (и расширением - в Linux расширений нет, кстати, это просто конец имени). Пишешь в него любой код, но первую строчку делаешь
#!/bin/bash
# означает комментарий, и оболочка по этой строке определит, как запускать командный файл. Кстати, bash - не единственная коммандная среда под Linux. После этого делаешь файл исполняемым - либо в свойствах ставишь галочку, либо выполняешь
chmod +x имяфайла
(подробности в man chmod) и можешь запускать, как и когда угодно.

читай тут
http://linuxforum. ru/viewtopic.php?id=221
более подробно тут
http://rus-linux .net/MyLDP/HOWTO-ru/Bash-Progr-Intro-HOWTO/Bash-Prog-Intro-HOWTO.html#toc14

Сделал исполняемый файл, который устанавливает кучку программ пакетно. Из картинок думаю что все видно и понятно. Не забывать поставить крестик в свойствах файла, чтобы он стал запускаемым. Расширения файла - по барабану. Это не Винда!
Запускается в Терминале и. сидишь только наблюдаешь, как программки устанавливаются. Можно покурить, можно чай попить 🙂

Источник

Linux equivalent for «cmd /C start "Title" "C:\Folder\file.bat"»

I have a Java program that runs a batch file on Windows with Runtime.getRuntime().exec() using the following command:

cmd /C start "Title" "C:\Folder\file.bat" 

we have SO MANY ways to execute a file in linux. but perhaps sharing the contents of your .bat file would be more lucrative. the path, C:\ doesnt exist, youll change that to the path in linux. ~/

@j0h I have edited the question. The content of the script can be anything. However, I am interested in the command to launch it with a similar behavior.

The question effectively is a duplicate of this: askubuntu.com/q/38661/295286 Once bounty expires, it should be voted to be closed

@SergiyKolodyazhnyy - the item you link is about running scripts from the desktop. OP asks about launching processes from within Java. Not a duplicate?

@taifwa OP knows how to launch process from Java program - they already mention Runtime.getRuntime().exec() , and Java being cross-platform language would use same method to launch a program. The post isn't about running scripts form desktop either - it's running scripts in general, and the key to that making script executable with chmod +x and giving path to script or at least having script live in one of the directories that belong to PATH variable. Whether or not it's form Java is irrelevant. These steps are required

Читайте также:  Ноутбук на основе линукс

4 Answers 4

The way to launch a script is to give its path:

To have it continue if the parent process exits, you can just launch it in the background by appending & :

So, if you have, for example, one script calling another like so:

You could run script1.sh which will call script2.sh and, because it is sent to the background, exit immediately.

Various ways to execute a script in Linux: my arbitrary script name is foo.bat

ex1: this is is just a regular file at this point, we execute it with bash ex2: we have marked foo.bat as executable. ($chmod +x foo.bat) ex3: Command substitution ex4: execute using "``" ex5: we us the path to the executable.

These are the most common. I would avoid calling things like gnome-terminal, and xterm. These are less common shells compared to things like bash, and sh. For example I use Ubuntu mate. I have neither xterm nor gnome-terminal. But, I do have bash, and sh. as does nearly ever other person running Linux / mac osx.

You may appreciate this tutorial on script execution here

the script foo.bat does this:

$ ./foo.bat Having a bash ./foo.bat 
$ cat foo.bat #!/bin/bash echo -e "Having a bash" $0 

I think the use of $0 can give you the title functionality you may desire. Many more possibilities exist.

gnome-terminal and xterm are not shells themselves, rather they're terminal emulators which run a shell. bash is a shell. zsh is a shell. ksh is a shell.

As I understand, you want to run an external command from within Java, and have the Java process continue (asynchronously) whilst that external command continues to run too?

Here's a little test I whipped up:

[tai@flenux runproc ] $ ls filecreator.sh ProcRunner.class ProcRunner.java [tai@flenux runproc ] $ cat filecreator.sh touch newfile [tai@flenux runproc ] $ cat ProcRunner.java public class ProcRunner < public static void main(String[] args) < try < Runtime.getRuntime().exec(args); Thread.sleep(2000); System.out.println("Done"); >catch(Exception e) < System.err.println(e.toString() ); >> > [tai@flenux runproc ] $ java ProcRunner bash filecreator.sh Done [tai@flenux runproc ] $ ls filecreator.sh newfile ProcRunner.class ProcRunner.java [tai@flenux runproc ] $ 

Java successfully execs the program (which simply creates the "newfile" file) ; it also continues on its own merry way to print the Done message after calling the exec.

Читайте также:  Linux kernel device create

If you wanted a terminal window to open too, prefix the call string

Runtime.getRuntime().exec(["gnome-terminal", "-e"]+args); 

So to answer your question, it looks like you would just call the method you specified. Are you getting a different behaviour?

Источник

Linux equivalent of windows cmd command

I think fc can be replaced by diff or comm , find with grep , but I don't how to do the && part, maybe an if statement.

What do the fc and find commands actually do on Windows, and what's the significance of set at the end? I've never worked with a Windows command line.

fc is also a Unix/Linux command: See unix.com/man-page/opensolaris/1/fc and compare what it does with docs.microsoft.com/en-us/windows-server/administration/… - likewise, set began as a Unix/Linux command unix.com/man-page/opensolaris/1/set and then was replicated in Windows ss64.com/nt/set.html

@K7AAY Note the fc command you link to is a "history manipulation" command, and totally different to the DOS command. The set command in windows hails back to old DOS days, if not further.

Actually DOS started in 2.20 or whatever (can´t remember) implementing some Unix' isms and not the other way around.

@Stephen Harris Duly noted. My comment invites the poster to Read The Fine Manual in response to his comment above.

3 Answers 3

Taking a guess as to what those Windows commands do, I'd say the equivalent in a POSIX sh script would be:

equal=no cmp -s file1 file2 && equal=yes 

which would set the equal variable to yes if the two files can be read and have identical content (byte-to-byte).

As an alternative to cmp -s , on some systems including Linux-based ones, you can use diff -q . diff -q ( q for quiet), contrary to most cmp -s ( s for silent) would report an error message if any of the files could not be read. While the GNU implementations of diff and cmp both first check to see if the two files are paths to the same file (including as hard or symbolic links one of the other) or are of different sizes to save having to read them, the busybox implementation of cmp does not while busybox diff does. So on those systems using busybox , you may prefer diff -q for performance reason.

Источник

Оцените статью
Adblock
detector