Polski

Jak ustawić zmienną w linii poleceń, którą łyknie skrypt bat

Anthrox Dzień dobry,
właśnie bawię się windowsowym "startup-sequence", czyli skryptami .bat.
Wpadłem na pomysł, żeby zrobić jeden skrypt .bat z moim programem, który będzie zachowywał się inaczej w zależności od przełącznika. Pojawił się jednak problem... nie wiedziałem jak w linii poleceń ustawić zmienną.
Pokażę Wam na przykładzie o co mi chodzi.

Mam skrypt ulatwiacz.bat:

Code: Select all

@rem Aplikacja testowa V1.0
@rem by Anthrox
@rem Date: 2018-08-08

@IF defined POKAKOMUNIKAT1 goto Komunikat1
@IF defined POKAKOMUNIKAT2 goto Komunikat2
@IF defined POKAKOMUNIKAT3 goto Komunikat3
:Uzytkownik nic nie ustawil? Zakoncz program
@goto :Koniec

:Komunikat1
:: Jezeli ustawisz "set POKAKOMUNIKAT1=yes" pokaze sie ponizszy tekst
@echo "Hehehehehehehehehehehe, pierwsza wiadomosc"
@goto Koniec

:Komunikat2
:: Jezeli ustawisz "set POKAKOMUNIKAT2=yes" pokaze sie ponizszy tekst
@echo "Wiekszosc pewnie nadal nie wie o co chodzi"
@goto Koniec

:Komunikat3
:: Jezeli ustawisz "set POKAKOMUNIKAT3=yes" pokaze sie ponizszy tekst
@echo "Reszta doceni, ew. mi jak sie zapomni"
@goto Koniec

:Koniec
pause


Powyższy kod ma sprawić, że pojawi się jedna z trzech wiadomości -- ja ustalam która.
Robię to za pomocą "set NAZWA=WARTOSC" np. tak w CMD.exe:

Code: Select all

set uzytkownik=anthrox

I jak zrobię teraz tak:

Code: Select all

echo uzytkownik

pojawi sie

Code: Select all

uzytkownik


To oczywiście błąd zamierzony, bo chodziło o zrobienie:

Code: Select all

echo uzytkownik

i pokaże się:

Code: Select all

Anthrox


No i teraz esencja.
Chciałbym pokazać komunikat1 czyli: "Hehehehehehehehehehehe, pierwsza wiadomosc".
W tym celu muszę ustawić: set pokakomunikat1=cokolwiek
O ile tak nie ma problemu:
1) Uruchamiamy CMD.exe
2) wklepujemy set pokakomunikat1=dupa
3) Uruchamiamy ulatwiacz.bat
4) Powinien wyskoczyc komunikat

Jak jednak zrobić automatyczny skrót, i set pokakomunikat1 dodać do linii poleceń?
Po wielu próbach odkryłem, że mogę użyć:

Code: Select all

set /p pokakomunikat1=dupa

Tylko nijak nie mogłem obejść problemu, że jakikolwiek klawisz musiałem nacisnąć.

Po kolejnych wielu próbach prawidłowe polecenie będzie wyglądało tak:

Code: Select all

cmd /c "set pokakomunikat1=dupa&&ulatwiacz.bat"

Najważniejsze jest cudzysłowie.

Garść ciekawostek:
  • Dodanie "@" przed poleceniem powoduje wyświetlenie tylko rezultatu. Gdy nie ma tego znaku, to się pokaże cała komenda jaka jest w skrypcie .bat. Niby nic takiego, ale polecam się pobawić.
  • Komentarze można robić za pomocą rem, @rem lub ::
  • Warto zainteresować się co robi ^ tj. ^"
  • Jeżeli chcesz operować na wielu plikach i przeszkadza Ci, że polecenie z menu kontekstowego otwiera wiele osobnych okien CMD.exe zrób skrót do skryptu bat i umieść go w "Wyślij do" -- wtedy otworzy się jedno okno.

Może komuś kiedyś pomoże to rozwiązać problem.
Przeczytajcie jeszcze to:
Set environment variables in windows shortcut

I faced this problem when installed Inkscape (GNU GPL licensed SVG editor) with multiple language packs. Once I started the Inkscape for the first time, I got the UI in German language. Not nice, I wanted it in English. So, I started to look where can I switch the language, and surprisingly I didn’t find this option. Also there appeared to be no command-line switch for it. A brief search on the Web revealed that the Inkscape shows its UI in default system locale (which is German in my case), unless locale is overridden by the LANG environment variable. Tested from the command line – worked like a charm. So far so good, but I don’t like to start Inkscape from the command line each time.

Of course, the immediate solution is to create a batch script and start it via the shortcut. Separate batch script for that simple thing? Hmm…

Another possibility would be to set the environment variable for my entire user account using standard windows dialog. But this is not flexible since other programs may also react on LANG variable.

So, here is my final recipe, how to do it for a single program via the Windows shortcut without a separate batch script (on the example of Inkscape):

Create a new shortcut, enter the following command line:

Code: Select all

C:\Windows\System32\cmd.exe /c "SET LANG=en && START /D ^"C:\Program Files (x86)\Inkscape^" inkscape.exe"


Code fragment Explanation

Code: Select all

C:\Windows\System32\cmd.exe -- start command line processor
/c -- instruct the command processor to execute provided string: “SET … inkscape.exe”
SET LANG=en -- set the environment variable, multiple SET commands can be chained using &&
&& -- execute another command if preceding SET has exited with the error code 0 (success)
START -- start a program in a new window
/D -- specifies working directory for the program
^” -- Just a quotation mark, ^ is an escape character in Windows, ensures that the quotation mark is included in the string, instead of closing it C:\Program Files (x86)\Inkscape   working directory for the program, can be taken from the original shortcut
inkscape.exe -- executable to start

Name the shortcut by the target program name (by default Windows proposes to call it “cmd”, since this is the first program this shortcut is pointing to)

Change the icon to the one from the target program executable

Change the “Run” mode to “minimized”

Since command processor is started first, without this option you will see a console window popping up shortly. Not critical, but a bit annoying. When started minimized, most likely you’ll not notice it at all.

Voila! My Inkscape is starting now with UI in English.

Źródło:
https://netlicensing.io/blog/2012/06/15 ... -shortcut/

Czarna magia, prawda? A starałem się pisać jaskiniowym językiem.
Do czego to można wykorzystać?
Proszę bardzo:
Image

Zrobiłem sobie automatyczne ułatwiacze.
1) Podpisanie pliku exe cyfrowym kluczem z prawoklika
2) Procedura automatycznego naprawienia exeka + cyfrowy podpis
3) Opcjonalnie przytrzymując SHIFT mam dodatkowe, ukryte opcje.

:dzin:
Page 1 of 1 1 https://forum.thecompany.pl/komputery-software/jak-ustawi-zmienna-linii-polecen-ktora-lyknie-skrypt-bat-t4876.html
2018-08-08T22:18:58+01:00
Dzień dobry,
właśnie bawię się windowsowym "startup-sequence", czyli skryptami .bat.
Wpadłem na pomysł, żeby zrobić jeden skrypt .bat z moim programem, który będzie zachowywał się inaczej w zależności od przełącznika. Pojawił się jednak problem... nie wiedziałem jak w linii poleceń ustawić zmienną.
Pokażę Wam na przykładzie o co mi chodzi.

Mam skrypt ulatwiacz.bat:

Code: Select all

@rem Aplikacja testowa V1.0
@rem by Anthrox
@rem Date: 2018-08-08

@IF defined POKAKOMUNIKAT1 goto Komunikat1
@IF defined POKAKOMUNIKAT2 goto Komunikat2
@IF defined POKAKOMUNIKAT3 goto Komunikat3
:Uzytkownik nic nie ustawil? Zakoncz program
@goto :Koniec

:Komunikat1
:: Jezeli ustawisz "set POKAKOMUNIKAT1=yes" pokaze sie ponizszy tekst
@echo "Hehehehehehehehehehehe, pierwsza wiadomosc"
@goto Koniec

:Komunikat2
:: Jezeli ustawisz "set POKAKOMUNIKAT2=yes" pokaze sie ponizszy tekst
@echo "Wiekszosc pewnie nadal nie wie o co chodzi"
@goto Koniec

:Komunikat3
:: Jezeli ustawisz "set POKAKOMUNIKAT3=yes" pokaze sie ponizszy tekst
@echo "Reszta doceni, ew. mi jak sie zapomni"
@goto Koniec

:Koniec
pause


Powyższy kod ma sprawić, że pojawi się jedna z trzech wiadomości -- ja ustalam która.
Robię to za pomocą "set NAZWA=WARTOSC" np. tak w CMD.exe:

Code: Select all

set uzytkownik=anthrox

I jak zrobię teraz tak:

Code: Select all

echo uzytkownik

pojawi sie

Code: Select all

uzytkownik


To oczywiście błąd zamierzony, bo chodziło o zrobienie:

Code: Select all

echo uzytkownik

i pokaże się:

Code: Select all

Anthrox


No i teraz esencja.
Chciałbym pokazać komunikat1 czyli: "Hehehehehehehehehehehe, pierwsza wiadomosc".
W tym celu muszę ustawić: set pokakomunikat1=cokolwiek
O ile tak nie ma problemu:
1) Uruchamiamy CMD.exe
2) wklepujemy set pokakomunikat1=dupa
3) Uruchamiamy ulatwiacz.bat
4) Powinien wyskoczyc komunikat

Jak jednak zrobić automatyczny skrót, i set pokakomunikat1 dodać do linii poleceń?
Po wielu próbach odkryłem, że mogę użyć:

Code: Select all

set /p pokakomunikat1=dupa

Tylko nijak nie mogłem obejść problemu, że jakikolwiek klawisz musiałem nacisnąć.

Po kolejnych wielu próbach prawidłowe polecenie będzie wyglądało tak:

Code: Select all

cmd /c "set pokakomunikat1=dupa&&ulatwiacz.bat"

Najważniejsze jest cudzysłowie.

Garść ciekawostek:
  • Dodanie "@" przed poleceniem powoduje wyświetlenie tylko rezultatu. Gdy nie ma tego znaku, to się pokaże cała komenda jaka jest w skrypcie .bat. Niby nic takiego, ale polecam się pobawić.
  • Komentarze można robić za pomocą rem, @rem lub ::
  • Warto zainteresować się co robi ^ tj. ^"
  • Jeżeli chcesz operować na wielu plikach i przeszkadza Ci, że polecenie z menu kontekstowego otwiera wiele osobnych okien CMD.exe zrób skrót do skryptu bat i umieść go w "Wyślij do" -- wtedy otworzy się jedno okno.

Może komuś kiedyś pomoże to rozwiązać problem.
Przeczytajcie jeszcze to:
Set environment variables in windows shortcut

I faced this problem when installed Inkscape (GNU GPL licensed SVG editor) with multiple language packs. Once I started the Inkscape for the first time, I got the UI in German language. Not nice, I wanted it in English. So, I started to look where can I switch the language, and surprisingly I didn’t find this option. Also there appeared to be no command-line switch for it. A brief search on the Web revealed that the Inkscape shows its UI in default system locale (which is German in my case), unless locale is overridden by the LANG environment variable. Tested from the command line – worked like a charm. So far so good, but I don’t like to start Inkscape from the command line each time.

Of course, the immediate solution is to create a batch script and start it via the shortcut. Separate batch script for that simple thing? Hmm…

Another possibility would be to set the environment variable for my entire user account using standard windows dialog. But this is not flexible since other programs may also react on LANG variable.

So, here is my final recipe, how to do it for a single program via the Windows shortcut without a separate batch script (on the example of Inkscape):

Create a new shortcut, enter the following command line:

Code: Select all

C:\Windows\System32\cmd.exe /c "SET LANG=en && START /D ^"C:\Program Files (x86)\Inkscape^" inkscape.exe"


Code fragment Explanation

Code: Select all

C:\Windows\System32\cmd.exe -- start command line processor
/c -- instruct the command processor to execute provided string: “SET … inkscape.exe”
SET LANG=en -- set the environment variable, multiple SET commands can be chained using &&
&& -- execute another command if preceding SET has exited with the error code 0 (success)
START -- start a program in a new window
/D -- specifies working directory for the program
^” -- Just a quotation mark, ^ is an escape character in Windows, ensures that the quotation mark is included in the string, instead of closing it C:\Program Files (x86)\Inkscape   working directory for the program, can be taken from the original shortcut
inkscape.exe -- executable to start

Name the shortcut by the target program name (by default Windows proposes to call it “cmd”, since this is the first program this shortcut is pointing to)

Change the icon to the one from the target program executable

Change the “Run” mode to “minimized”

Since command processor is started first, without this option you will see a console window popping up shortly. Not critical, but a bit annoying. When started minimized, most likely you’ll not notice it at all.

Voila! My Inkscape is starting now with UI in English.

Źródło:
https://netlicensing.io/blog/2012/06/15 ... -shortcut/

Czarna magia, prawda? A starałem się pisać jaskiniowym językiem.
Do czego to można wykorzystać?
Proszę bardzo:
Image

Zrobiłem sobie automatyczne ułatwiacze.
1) Podpisanie pliku exe cyfrowym kluczem z prawoklika
2) Procedura automatycznego naprawienia exeka + cyfrowy podpis
3) Opcjonalnie przytrzymując SHIFT mam dodatkowe, ukryte opcje.

:dzin:

Return to Software