Why won't this script work?  [Solved]

Help with the version of MX KDE officially released by the Development Team.
When asking for help, use Quick System Info from MX Tools. It will be properly formatted using the following steps.
1. Click on Quick System Info in MX Tools
2. Right click in your post and paste.
Message
Author
User avatar
paul1149
Posts: 531
Joined: Fri Dec 29, 2017 11:39 am

Why won't this script work?

#1 Post 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.
MX-19-KDE x64, on Ryzen 5, 16GB RAM, SSD. Nvidia graphics.

User avatar
towwire
Posts: 688
Joined: Fri Oct 15, 2010 12:15 pm

Re: Why won't this script work?

#2 Post by towwire »

I do not have the time right now but maybe this refresher will help you.

https://phoenixnap.com/kb/write-bash-script
It is easier to fight for one's principles then to live up to them.

User avatar
paul1149
Posts: 531
Joined: Fri Dec 29, 2017 11:39 am

Re: Why won't this script work?

#3 Post 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.
MX-19-KDE x64, on Ryzen 5, 16GB RAM, SSD. Nvidia graphics.

User avatar
CharlesV
Administrator
Posts: 8101
Joined: Sun Jul 07, 2019 5:11 pm

Re: Why won't this script work?

#4 Post 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
 
*QSI = Quick System Info from menu (Copy for Forum)
*MXPI = MX Package Installer
*Please check the solved checkbox on the post that solved it.
*Linux -This is the way!

User avatar
paul1149
Posts: 531
Joined: Fri Dec 29, 2017 11:39 am

Re: Why won't this script work?

#5 Post 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?
MX-19-KDE x64, on Ryzen 5, 16GB RAM, SSD. Nvidia graphics.

User avatar
fehlix
Developer
Posts: 12932
Joined: Wed Apr 11, 2018 5:09 pm

Re: Why won't this script work?

#6 Post 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.

User avatar
paul1149
Posts: 531
Joined: Fri Dec 29, 2017 11:39 am

Re: Why won't this script work?

#7 Post 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:~
MX-19-KDE x64, on Ryzen 5, 16GB RAM, SSD. Nvidia graphics.

User avatar
fehlix
Developer
Posts: 12932
Joined: Wed Apr 11, 2018 5:09 pm

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

#8 Post 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.

User avatar
paul1149
Posts: 531
Joined: Fri Dec 29, 2017 11:39 am

Re: Why won't this script work?

#9 Post 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!
MX-19-KDE x64, on Ryzen 5, 16GB RAM, SSD. Nvidia graphics.

User avatar
DukeComposed
Posts: 1507
Joined: Thu Mar 16, 2023 1:57 pm

Re: Why won't this script work?

#10 Post 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.

Post Reply

Return to “MX KDE Official Release”