Failed to execute bash script on mac - bash

I’ve created the following simple test script
#!/bin/bash
echo "test"
I use chmod +x ./msa.sh to execute it.
Now, when I try to run it
like ./msa
I got the following error,
Failed to execute process ‘./msa.sh'. Reason:
exec: Exec format error
The file './msa.sh' is marked as an executable but could not be run by the operating system.
I'm using mac with fish, what should I do in order to run it successfully?

Are you sure you're saving the script as a plain text file? If you're using TextEdit you may be saving a RTF. Choose Format > Make Plain Text and resave the file if needed.
You can also examine the file from the shell to make sure it starts with the shebang line you're expecting. For example:
nicholas#mary ~> file msa.sh
msa.sh: Rich Text Format data, version 1, ANSI
nicholas#mary ~> head -1 msa.sh
{\rtf1\ansi\ansicpg1252\cocoartf1561\cocoasubrtf400
That is not what you want.
nicholas#mary ~> file msa.sh
msa.sh: Bourne-Again shell script, ASCII text executable
nicholas#mary ~> head -1 msa.sh
#!/bin/bash
And that is what you want.

Related

How to write list of executing program in a text file by bash

I trying write a program with bash file that create a text file then write the List of running programs file in the text file. I created a text file with bash that name is test.txt, how can I write List of running programs in the text file?
I put my code here:
#!/bin/bash
cat test.txt
To get a list of running programs, use ps. Run man ps for details.
To redirect the output of a command to a file, use >.
ps > test.txt
See REDIRECTION in man bash for details.

Gimp batch usage on macOS doesn't read script fu file

I'm trying to make my first steps with Gimp batch processing, but I'm stuck at the very beginning. I'm getting this error:
z80crews-iMac:~ z80crew$ /Applications/GIMP-2.10.app/Contents/MacOS/gimp -i -b '(do-it "france.png" "spain.png")' -b '(gimp-quit 0)'
batch command experienced an execution error:
Error: eval: unbound variable: do-it
do-it is the name of my function to execute. This function is defined in a file that's stored in ~/.gimp-2.10/scripts.
z80crews-iMac:~ z80crew$ cat ~/.gimp-2.10/scripts/gimp-combine-images.scm
(define (do-it fname-one fname-two)
)
z80crews-iMac:~ z80crew$ /Applications/GIMP-2.10.app/Contents/MacOS/gimp -v
GNU Image Manipulation Program Version 2.10.12
Additional info: Defining do-it right on the command line works:
z80crews-iMac:~ z80crew$ /Applications/GIMP-2.10.app/Contents/MacOS/gimp -i -b '((define (do-it fname-one fname-two)) (do-it "france.bigcities.png" "france.cities.png")' -b '(gimp-quit 0)'
batch command executed successfully
Other things I tried:
I tried to pass the script as an argument to gimp.
I opened the Gimp application and stored my script fu script in the directories that are shown in Preferences > Folders > Scripts.
I'm obviously missing something easy, but I can't find out what it is.
It seems this is the way to go:
I opened the Gimp application and stored my script fu script in the directories that are shown in Preferences > Folders > Scripts.
This time I launched and closed GIMP (the application with the GUI) after having put my script in /Users/USERNAME/Application Support/GIMP/2.10/scripts. This was the first path that GIMP lists in Preferences > Folders > Scripts.

Copying .jpg file using shell script gives 'Failed to open input stream for file' error

Very simple script to copy a file
#!/bin/bash
#copy file
mtp-getfile "6" test2.jpg
I set it as executable and run it using
sudo sh ./test.sh
It gives me a file called test2.jpg that has no icon and I cannot open I get a 'Failed to open input stream for file' error
However, if I simply issue the following from the command line
mtp-getfile "6" test2.jpg
It works as expected. What is wrong with my script? I checked and the resulting .jpg file in each case has the same number of bytes. Very strange.
As commented by chepner, your file might have a DOS (Windows) invisible line ending on its name, which would cause an error. To get rid of this unwanted character(s), just create a new blank script on your "nix" system and type the name by hand (not by copying and pasting, to avoid problems), let's say, name it test2.sh.
Then copy all the contents of test.sh to test2.sh (copy and paste) and run test2.sh and see if it works. If it doesn't, try running the following code on the new script, to make sure that there are no unwanted characters on the code itself:
tr -d "\r" < /folder/test2.sh && echo >> /folder/test2.sh
And then try to run script2.sh again to see if it works. Note: the echo >> /folder/test2.sh part of the code above is just to make sure that your new script ends with a newline, which is a Posix standard (and without which some programs may misbehave because they expect the file to end with a newline).
Apparently it was a permissions issue.
I only had to do a sudu chown test2.jpg

shell script behaves differently from Automator

I have a couple scripts to clean up some generated HTML; a python script using BeautifulSoup, and a Ruby script. These remove unneeded tags and attributes, transform symbols to html encodings, get rid of excessive newline characters, and make sure the files are encoded in valid UTF-8.
I can run these two scripts from the command line and they work as expected; and I can run them in this shell script from the command line:
#!/bin/bash
HTMLPath=$1
echo $HTMLPath 2>&1 >> /Users/me/Desktop/log.txt
/usr/bin/python /Users/me/DevProj/script/cleanHTML.py "$HTMLPath" 2>&1 >> /Users/me/DevProj/logs/log.txt
/Users/me/.rvm/rubies/ruby-1.9.3-p374/bin/ruby /Users/me/DevProj/script/cleanupHtml.rb "$HTMLPath" 2>&1 >> /Users/me/DevProj/logs/log.txt
and this also gives the expected result.
However when I run them from Automator, which feeds them a list of html files to process, the python script removes the html encoding − from the file (HTML encoding for minus symbol -) and I end up with white space instead. The problem is, I need those minus signs to remain, preferably as − and I need it to be run from Automator.
I thought it might have something to do with the version of python that the Automator shell was using, so I created the shell script to ensure it would use same bash shell I use on the command line, /bin/bash.
Has anyone else out there run into this issue? Am I doing something wrong from Automator?
In the script I have added python --version >> log.txt but it won't output the python version to the log... I have a sneaking suspicion that Automator is using its own older python library.
Note: I have only encountered this problem when there are − strings in the input file. It otherwise runs fine from Automator. Automator is causing the problem.

Logging terminal while running an install script of sorts

I have written an install script in shell that does some configuration of various things such as xserver, network, etc and then installs a few RPM's which is no problem. But I want to be able to log everything that goes to the terminal screen as well. Is this possible to do from within the script so if the end user runs ./Install.sh it will do everything (including the logging).
I have tried using "script" but doesn't work from within the Install.sh script itself.
Thanks
Just use:
script logfilename install.sh
when the install.sh finished the run, the script end too - so, you get everything logged into logfilename.
Probably you can make it two-stage, so, you should rename your current install.sh into install-stage2.sh and your install.sh will contain:
script ./install.log ./install-stage2.sh
UPDATE from my man script
NAME
script -- make typescript of terminal session
SYNOPSIS
script [-akq] [-t time] [file [command ...]]
DESCRIPTION
The script utility makes a typescript of everything printed on your terminal. It is useful for students who need
a hardcopy record of an interactive session as proof of an assignment, as the typescript file can be printed out
later with lpr(1).
If the argument file is given, script saves all dialogue in file. If no file name is given, the typescript is
saved in the file typescript.
If the argument command is given, script will run the specified command with an optional argument vector instead
of an interactive shell.

Resources