flex cannot open file in sh script - cygwin

I use flex on cygwin package for Windows to build lexical analyzer.
I created file test.lex with rules
When i use this commands in command line
flex -i test.lex
cc lex.yy.c -lfl
./a
It works fine. But when i tried to make sh file (compile_flex.sh) with this commands and run file
./compile_flex.sh
I received error
flex: can't open test.lex
I tried to put different paths to my file
~/test.lex
/home/user/test.lex
c:\\cygwin64\\home\\user\\test.lex
But it doesn't work! I always receive message that file not found.
When I put command
vi test.lex
In my .sh file it works good and file is opened in vim

I figured out with my problem. I use windows editor (notepad++) for create .sh file. When I changed line separator from \n\r to \n in preferences, it was working fine

Related

Unix\AIX Script Error: "0403-015 Cannot access a required executable file. It is in use" & "Cannot Open or Remove a file Containing a Running Program"

Most of the times you create/place a script on/from Windows or WinSCP to a Unix machine without using vi command you will most likely encounter the errors below.
From what I saw it always happens because of special characters that look like simple space but are preformatted from Outlook or other tools.
Errors Encountered:
ksh:
0403-015 Cannot access a required executable file. It is in use.
bash:
/bin/bash^M: bad interpreter: Cannot Open or Remove a file Containing a Running Program
Place your script on a Linux machine and run the following command:
sed -i -e 's/\r$//' Your_Script.sh
Command Explained:
sed
stream editor for filtering and transforming text
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if extension supplied)
-e script, --expression=script
add the script to the commands to be executed
Upload the script in WinSCP using the "Text" mode.
See https://winscp.net/eng/docs/transfer_mode
You better enable the "Automatic" mode, instead of explicit "Text", so that you won't laster inadvertently upload binary files like images using the text mode.

Bro script works on command line but not main run

I can do comparisons with tab delimited files from command line such as :
bro -i eth1 malware_test_ips.bro
But whenever I install the same script to the site area, the comparisons are not made!! In fact it seems it is not reading the very same tab delimited file. Below is the read function. Why would this work on command line not on broctl deploy, despite showing no errors or warnings? Is there a file read setting im missing?
Input::add_table([$source=sinkhole_list_location, $name="sinkhole", $idx=Idx, $val=Val, $destination=sinkhole_list2, $mode=Input::REREAD]);
I have just solved this. I had to use the #DIR to refer to the sinkhole_list_location for the file. Either this or the full file pathway.
It works from command line because the file is local to the command, local pathway.

Run Perl script in Linux environment

I'm trying to run code on Linux environment
Here's the code (saved as hello.pl):
#!/usr/bin/perl
use strict;
use warnings;
print "Hello You\n";
Here's what I tried on my linux environment:
%perl hello.pl
I tried listing out the path starting from C:\Users\... and so on
I keep getting error that says:
Can't open perl script "hello.pl": No such file or directory
You have to be located in the same folder with the hello.pl in the "window" (aka terminal, or console) that you try to execute perl hello.pl.
On linux, you can determine the folder that you're in by issuing pwd.
If you're not in the same folder (the most probable cause of your error), you have 2 options:
Navigate to that folder with cd /path/to/your/script/location you have to replace the /path/to/your/script/location in the example, with your actual path
Execute the file with perl /path/to/hello.pl - of course, you have to replace the /path/to/ in the example, with your ac
Also, you can try and view the file from the console running a less hello.pl
In cygwin you might try: /cygdrive/c/Users/bonan/Desktop/perl/hello.pl.
Alternatively at your prompt try tying in just perl without hitting enter, and then drag the hello.pl file from its file explorer location into the terminal window. That should paste the full file path to the file as text into the command prompt. If you're using cygwin I forget it if properly pastes the path with forward-slashes, like /cygdrive/c/Users/bonan/Desktop/perl/hello.pl, or if it pastes what it would in cmd with backslashes as you've indicated you typed yourself.
The other thing to do that's relatively easy is right click the file and choose to open a terminal or shell here, which for cygwin you can get in your context menu by running chere -i once (it actually says "Bash prompt here" I think). And there's similar context menu options for cmd, powershell, an actual linux bash, or mac os x's terminal ... once you're in the same path as the file, you can just type perl heltab and autocomplete the filename assuming no other files in the same folder start with "hel".

how to convert a text/plain to text/x.shellscript

I am sending a .sh file created from a windows machine to a linux so that I could run it there. The problem is that I keep on getting an error called bad interpreter.But when I program the shell script in the linux machine it runs with no problems even though it has the same code with the one sent from the windows machine. After my ivestigation, I found out that the windows machine .sh script is a text/plain file(using file -bi) and the other one from the linux machine is a text/x.shellscript. Is there a way to convert the text/plain to a text/x.shellscript? thank you
this is the script:
#!/bin/bash
date
sudo apt-get update
I tried a solution by doing another .sh file in a linux box containing only
#!/bin/bash
Then the windows machine only sent a file containing test commands like :
date
hostname
Then I append the file from the windows box to the linux one with
cat windows.sh >> linux.sh
It did not work if I run linux.sh. It says errors like:
./linuxh.sh: line 2 $'date\r':command not found
./linuxh.sh: line 2 $'hostname\r':command not found
However, if I open Linux.sh then save it again without doing anything. It works
I'm summarising below the steps you need to take so other users can see easily what needs doing:
Firstly, you need to check your script has the correct path to your interpreter after the "#!" in the very first line. This is should probably be:
#!/bin/bash
or
#!/usr/bin/bash
and you can find which is correct by typing:
which bash
on your Linux box.
Secondly, you need to make sure that any Windows carriage returns (or "^M") at the ends of the lines are removed before expecting your Linux box to run the script. You can do this with:
dos2unix yourscript
Just for reference, you can easily see weird characters such as TABs or linefeeds or carriage returns in Linux by using:
cat -vet yourfile
or
sed -n l yourfile
Thirdly, you need to make sure your script is executable on Linux, using chmod like this:
chmod +x yourscript
Finally, when you have done all that, you need to either add the directory where the script is located to your PATH variable (and export it) or give the full path to your script like this if your script is in the current directory:
./yourscript
or like this if it is located somewhere else
/some/directory/some/where/yourscript

Linux bash script doesn't run correctly, but when run at the command line everything is fine

Originally I thought this was a db administration issue, but I think the lines have become a little blurred. I'm going to keep this strictly a scripting problem here. If you want more background info I posted this on ServerFault here:
https://serverfault.com/questions/511780/postgresql-pgdump-script-on-rhel5-failed-fe-sendauth-no-password-supplieddat
My Script
I distilled my original script down to this to keep things simple. This is what I have in a .sh named pg_dump_script:
/home/myusername/lappstack-1.2-5/postgresql/bin/pg_dump --host=127.0.0.1 --port=5433 --username=myusername mydbname
When I run this I get the following error:
$ bash pg_dump_script.sh
Password:
" does not exist database "corenection to database "mydbname
I can copy and paste the line in the script file and run it from the command line and everything works fine.
Here are the permission on the script file:
-rwxr-xr-x
I used chmod 755
I'm not sure what I'm doing wrong here? I'm not an expert on scripting, but I thought if I could run it from the console it would of course run fine from a script. Let me know if any information is needed. I'm trying to keep this simple.
Updates
I removed all parameters. This is all the text in the .sh
/home/myusername/lappstack-1.2-5/postgresql/bin/pg_dump
I receive this error:
$ ./pg_dump_script.sh
: No such file or directoryine 1: /home/myusername/lappstack-1.2-5/postgresql/bin/pg_dump
: command not foundst.sh: line 2:
Run dos2unix on your .sh file to convert to proper UN*X line endings and it should work:
dos2unix pg_dump_script.sh
When editing with vi check to make sure it's not in dos edit mode. If you start with an original text file with CR/LF in it, vi will automatically go into dos mode unless you tell it otherwise.

Resources