.bat to .sh converting for SQL*Loader Start on UNIX PC - batch-file

I have a simple .bat file
#echo; set nls_lang=russian_cis.ru8pc866
#echo off
SET NLS_NUMERIC_CHARACTERS=. '
sqlldr.exe userid=PRB/0611#TSESTDB control=control_file.ctl LOG=fdb_log.log errors=100
cmd
and i need to convert to .sh file for running on the UNIX based pc.
I began to read "BASH Programming - Introduction HOW-TO" (is it suitable for beginners?), but it is a episodical task and dead line comes.
Could anybody help me to convert file? Thanks a lot!!!

rewriting your script.
#!/bin/bash
# #echo;
# set nls_lang=russian_cis.ru8pc866
export NLS_LANG=russian_cis.ru8pc866
# not needed #echo off
# SET NLS_NUMERIC_CHARACTERS=. '
export NLS_NUMERIC_CHARACTERS='.'
PATH="/path/to/sqlDir/install:${PATH}"
# sqlldr.exe userid=PRB/0611#TSESTDB control=control_file.ctl LOG=fdb_log.log errors=100
sqlldr userid=PRB/0611#TSESTDB control=control_file.ctl LOG=fdb_log.log errors=100
# ? cmd
I've left your code in, but commented out (using the shell comment char '#'). The uncommented lines are the 'translation' of .bat syntax into Linux/Unix bash/shell syntax.
There some things above that you may need to fix:
You'll have to include the correct value in the resetting of PATH,
note that the value there is strictly to illustrate the issue.
export is used so that variables set in the current shell (the
shell script) are visible to child processes that run from the shell
script, in this case the important one being sqlldr
I'm not sure what values you really need assigned to
NLS_NUMERIC_CHARACTERS. Note that by quoting with the single-quote
char ' available to the shell, you should get exactly the value
used that you intended. If '*' or other reg-exp chars are used, this
may cause problems.
You may find that sqlldr.exe has a different name altogether. The
linux/unix convention for executable commands does not require the
.exe extension, so I have used sqlldr. Just use the full name of
the program you find in the installed directory.
The line with #!/bin/bash needs to be the first line in the file, with no leading spaces.
You'll also need to inform your OS that the script is intended to be executable. From a bash cmd line, IN the directory that contains this script, do
chmod 755 mySQLLDR_runningScript
Finally, not sure why you have cmd at the end of your .bat file, to open a new window? You'll need to experiment on your system to find the correct cmd to do that. Maybe xterm.
I hope this helps.

Related

Windows BAT script for cp alias produces error

I wrote a file called cp.bat and the content of this file is: DOSKEY cp=COPY $* copy. I saved this file in c:\users\myname\aliases. I also added this path to my environment so cmd can execute it.
When I now type cp in my cmd the output is a strange character for the c in cp.
I need this script to work because I want to install chicken scheme eggs on my machine, but the chicken-install command executes multiple cp commands and those are not recognized in the cmd.
I also tried to alter the build script of the eggs, but it gets newly generated everytime I call the chicken-install command.
It would also help if someone could explain me how to install chicken eggs on a windows machine correctly. I feel this workaround shouldn't be necessary. Thanks in advance.
Like Magoo mentioned:
In all probability, your editor is the root of your problem. Batch files should be created in strict ANSI format using a text-editor, not a word-processor. Notepad is barely adequate; Notepad++ is far better - I use Editplus
The encoding was messed up and it was set to UTF-8-BOM, so I changed it to ANSI and it worked flawlessly.
If you're using CHICKEN 5, it should not be emitting cp commands; it should emit batch files which use the builtin Windows commands only if you're using the mingw platform target. Have you used the PLATFORM=mingw option with every make invocation?
If you're using the mingw-msys (or cygwin) platform, then it will be emitting UNIX style commands.

Windows BAT file SET command works in weird way - Python virtual environment activate script

I'm trying to add some variables into Python virtual environment activate.bat.
I supposed it's a regular BAT file with pretty expected behaviour. But what I see is weird.
set env=abc
set db_url=dfg
If I do echo %env% after in the command prompt I see:
abcset db_url=dfg
Who can explain why is that and how to fix it?
Also, if I try clear it up like
set env=
it just reads all the strings after.
UPD: Okay, I don't know what I've done but what worked:
I recreated the virtual environment
I opened all files in VS Code (not in Notepad++ as before)
I put all the variables sets into double quotes like set "env=dev"

Find multiple files from the command line

Description:
I am searching a very large server for files that is on a different server. right now I open command prompt and type
DIR [FILE NAME] /S/4
This returns the server location of the file with some other stuff that is not really needed.
Question:
I have a lot of files to search and one by one input into the above command could take forever. Is there a way I could input all of the names of all the files and only search once and the search results would only need to show file name and location?
First, I hope you don't mean DOS, but rather Windows cmd or batch.
You can certainly write a script that will run your DIR command once per file being sought.
But what you most likely want instead is to search once and print the path of each file found. For this you can use PowerShell's FindChildItem or the improved one posted here: http://windows-powershell-scripts.blogspot.in/2009/08/unix-linux-find-equivalent-in.html
It will be something like:
Find-ChildItem -Name "firstfile.txt|secondfile.txt|..."
Another approach is to install msys or cygwin or another Linux tools environment for Windows and use the Linux find command.

Running a shell script through Cygwin on Windows

I have a bunch of shell scripts that used to run on a Linux machine. Now, we've switched over to Windows, and I need to run these scripts there. I have Cygwin installed, but is there a way to make the script run using Cygwin, but the call is made from Windows batch?
Sure. On my (pretty vanilla) Cygwin setup, bash is in c:\cygwin\bin so I can run a bash script (say testit.sh) from a Windows batch file using a command like:
C:\cygwin\bin\bash testit.sh
... which can be included in a .bat file as easily as it can be typed at the command line, and with the same effect.
One more thing - if You edited the shell script in some Windows text editor, which produces the \r\n line-endings, cygwin's bash wouldn't accept those \r. Just run dos2unix testit.sh before executing the script:
C:\cygwin\bin\dos2unix testit.sh
C:\cygwin\bin\bash testit.sh
If you have access to the Notepad++ editor on Windows there is a feature that allows you to easily get around this problem:
Open the file that's giving the error in Notepad++.
Go under the "Edit" Menu and choose "EOL Conversion"
There is an option there for "UNIX/OSX Format." Choose that option.
Re-save the file.
I did this and it solved my problems.
Hope this helps!
Read more at http://danieladeniji.wordpress.com/2013/03/07/microsoft-windows-cygwin-error-r-command-not-found/
Just wanted to add that you can do this to apply dos2unix fix for all files under a directory, as it saved me heaps of time when we had to 'fix' a bunch of our scripts.
find . -type f -exec dos2unix.exe {} \;
I'd do it as a comment to Roman's answer, but I don't have access to commenting yet.
The existing answers all seem to run this script in a DOS console window.
This may be acceptable, but for example means that colour codes (changing text colour) don't work but instead get printed out as they are:
there is no item "[032mGroovy[0m"
I found this solution some time ago, so I'm not sure whether mintty.exe is a standard Cygwin utility or whether you have to run the setup program to get it, but I run like this:
D:\apps\cygwin64\bin\mintty.exe -i /Cygwin-Terminal.ico bash.exe .\myShellScript.sh
... this causes the script to run in a Cygwin BASH console instead of a Windows DOS console.
If you don't mind always including .sh on the script file name, then you can keep the same script for Cygwin and Unix (Macbook).
To illustrate:
1. Always include .sh to your script file name, e.g., test1.sh
2. test1.sh looks like the following as an example:
#!/bin/bash
echo '$0 = ' $0
echo '$1 = ' $1
filepath=$1
3. On Windows with Cygwin, you type "test1.sh" to run
4. On a Unix, you also type "test1.sh" to run
Note: On Windows, you need to use the file explorer to do following once:
1. Open the file explorer
2. Right-click on a file with .sh extension, like test1.sh
3. Open with... -> Select sh.exe
After this, your Windows 10 remembers to execute all .sh files with sh.exe.
Note: Using this method, you do not need to prepend your script file name with bash to run

Stupid Batch File Behavior. Tries to execute comments

I have tried prefixing lines with semicolons, 'REM', etc.. but no matter what when I run my batch file I keep getting "unknown command REM whatever"
"REM test" It is not recognized, and it is windows vista. I simply get "rem" output back to my console.
That's entirely normal behavior. Batch files are simply sequences of commands that are run one after another. So every line will get output to the console as if it were typed there.
H:\>echo rem test > test.cmd
H:\>test
yields the output
H:\>rem test
as if I typed rem test directly to the console.
You can suppress this by either prefixing the line with #:
#rem test
or by including echo off in the batch file:
#echo off
rem test
If I put ":: test" and execute it I get back "Test".
Can't reproduce here.
If I put "; test" it recursively executes itself
A semicolon at the start of the line seemingly gets ignored.
If you're talking about cmd.exe batch files under Windows, you can use:
rem this method or
:: this method.
For bash and a lot of other UNIX-type shells, you use:
# this method.
I'm pretty certain you're not using cmd.exe since that would give you an error like:
'rem' is not recognized as an internal or external command,
operable program or batch file.
rather then:
Unknown command ...
If you are using a UNIX-type shell, the # character is almost certainly what you're after. If you let us know exactly the shell you're using, we can probably help out further.
you probably created an UNICODE file. These files contain 2 bytes header named BOM
which is not shown by any editor but cmd attempts to execute them and fails.
To make sure this is indeed an issue: type any other command at the very beginning
of your file and see it throws the same error - for example #echo test
To fix it, just create a new plain text file and copy content of the original file there.
then remove the original file and replace it by the newly created one.
In my case the problems are line endings. Somehow Maven or the Jenkins pipeline running on a Linux machine changed the line endings from Windows style (CR LF) to Unix style (LF). Changing them back solves the issue for me.

Resources