Page 1 of 2

Why won't this script work?

Posted: Sat Dec 02, 2023 11:32 pm
by paul1149
I'm on MX23/kde, trying to get a script to work with no luck.

System Info:
Processes: 357 Uptime: 14h 13m wakeups: 1 Memory: 15.52 GiB used: 8.07 GiB (52.0%) Init: SysVinit
v: 3.06 runlevel: 5 default: graphical tool: systemctl Compilers: gcc: 12.2.0 alt: 12
Client: shell wrapper v: 5.2.15-release inxi: 3.3.26

Bash location:
$ whereis bash
bash: /usr/bin/bash /usr/share/man/man1/bash.1.gz

Attempt to run script:

paul@mx:~
$ls r*
remove.sh

$ ./remove.sh
bash: ./remove.sh: cannot execute: required file not found

If I click on remove.sh in Dolphin, I get:
execvp: No such file or directory

The script is marked executable.


The script itself:
_______________
#!/usr/bin/bash
#
echo "Hello World"
ls
_______________

I've also tried it with the shebang:
#!/bin/bash

I was originally trying to use the script to delete a file. I was then going to schedule it as a job. But when it wouldn't work I kept simplifying the script until I was sure it was some kind of system/script problem, not a command within the script.

Thanks for any guidance.

Re: Why won't this script work?

Posted: Sun Dec 03, 2023 12:28 am
by towwire
I do not have the time right now but maybe this refresher will help you.

https://phoenixnap.com/kb/write-bash-script

Re: Why won't this script work?

Posted: Sun Dec 03, 2023 11:06 am
by paul1149
Thanks, I think I have those bases covered.

Also, when I use the shebang,

#!/usr/bin/env bash

I get the result:

$ ./remove.sh
/usr/bin/env: ‘bash\r’: No such file or directory
/usr/bin/env: use -[v]S to pass options in shebang lines

which I have no clue about.

Re: Why won't this script work?

Posted: Sun Dec 03, 2023 11:25 am
by CharlesV
Not sure what is going on with that script... this works perfectly in my KDE tester (marked executable )

called from terminal using
sh test.sh

test.sh is

Code: Select all

#!/bin/bash

echo "hello world"
 ls
 

Re: Why won't this script work?

Posted: Sun Dec 03, 2023 11:45 am
by paul1149
Thanks.

When I entered

Code: Select all

sh remove.sh
from the directory that script lies in, it did execute. I did not even have to precede it with "./", as in

Code: Select all

sh ./remove.sh
which I thought was necessary. So maybe I'd misunderstood how to call scripts from the command line?

I'm confused at what's going on. Clicking on the script in Dolphin still brings an error. I thought scripts could be invoked through the file manager?

Re: Why won't this script work?

Posted: Sun Dec 03, 2023 11:55 am
by fehlix
paul1149 wrote: Sat Dec 02, 2023 11:32 pm I'm on MX23/kde, trying to get a script to work with no luck.

System Info:
Processes: 357 Uptime: 14h 13m wakeups: 1 Memory: 15.52 GiB used: 8.07 GiB (52.0%) Init: SysVinit
v: 3.06 runlevel: 5 default: graphical tool: systemctl Compilers: gcc: 12.2.0 alt: 12
Client: shell wrapper v: 5.2.15-release inxi: 3.3.26

Bash location:
$ whereis bash
bash: /usr/bin/bash /usr/share/man/man1/bash.1.gz

Attempt to run script:

paul@mx:~
$ls r*
remove.sh

$ ./remove.sh
bash: ./remove.sh: cannot execute: required file not found

If I click on remove.sh in Dolphin, I get:
execvp: No such file or directory

The script is marked executable.


The script itself:
_______________
#!/usr/bin/bash
#
echo "Hello World"
ls
_______________

I've also tried it with the shebang:
#!/bin/bash

I was originally trying to use the script to delete a file. I was then going to schedule it as a job. But when it wouldn't work I kept simplifying the script until I was sure it was some kind of system/script problem, not a command within the script.

Thanks for any guidance.
You have no posted the script-file itself, but rather a textual copy/paste,
where the "failure" within the script file is not visible anymore.
May be this: Open terminal (konsole) with the folder the script is located and post the text shown
from this command:

Code: Select all

cat -v remove.sh
This may reveal the real problem with the puzzling behavior of the bash script you created.

Re: Why won't this script work?

Posted: Sun Dec 03, 2023 12:05 pm
by paul1149
I use the xfce terminal, just to be clear, but here is the output.

Code: Select all

paul@mx:~
$ cat -v remove.sh 
#! /usr/bin/bash^M
#^M
echo "Hello World"^M
ls^M
paul@mx:~

Re: Why won't this script work?  [Solved]

Posted: Sun Dec 03, 2023 12:58 pm
by fehlix
paul1149 wrote: Sun Dec 03, 2023 12:05 pm I use the xfce terminal, just to be clear, but here is the output.

Code: Select all

paul@mx:~
$ cat -v remove.sh 
#! /usr/bin/bash^M
#^M
echo "Hello World"^M
ls^M
paul@mx:~
Which tells: It tries to find the executable "bash^M", which does not exist,
b/c in linux text-files,and script files are line terminated with linefeed.
The "^M" as shown represents a carriage return character,
b/c other OS like Dos/Windows do use two characters for line endings carriage return+line feed.
To make the script created by a windows supporting text-editor,
you may consider to remove the carriage return,
e.g. this way:

Code: Select all

sed -i 's/\r//' remove.sh 
Or consider to adjust the used text-editor to not write windows type of text files when saving.

Re: Why won't this script work?

Posted: Sun Dec 03, 2023 1:41 pm
by paul1149
Wow, that's all it was. I switched the file to UNIX endings, and it worked not only in the command line, but through Dolphin. I suspected it had to be something basic, but this never occurred to me.

Thanks much, mystery solved!

Re: Why won't this script work?

Posted: Sun Dec 03, 2023 1:51 pm
by DukeComposed
paul1149 wrote: Sun Dec 03, 2023 11:06 am Also, when I use the shebang,

#!/usr/bin/env bash

I get the result:

$ ./remove.sh
/usr/bin/env: ‘bash\r’: No such file or directory
/usr/bin/env: use -[v]S to pass options in shebang lines

which I have no clue about.
As fehlix points out, "\r" in the output at the end of a line is a dead giveaway that this script was written on a Windows machine and copied to a Linux machine. Another way to clean a text file or script to ensure it has Linux-friendly line endings is to run "col < filename > new_filename". The col program was written explicitly to fix line endings, and can also convert between tabs and spaces when prompted.