Score:0

Bash script works with one account only

mq flag

i am new here and also new linux/ubuntu user. I have some PCs with xubuntu that are not managed, so all of changes i need to do, must be performed locally. At the moment we have two users in OS - admin and user. User is configured that he is only able to run an internet browser, nothing more.

Now I need to lock some settings, update some things, etc. For that I have prepared a bash script. I have to say, i am not bash expert, all things I have 'written', I found on Internet and changed so they can work as I want, but...

I need to execute it as user, so i have copied it on its desktop and ran with su admin and then sudo sh script.sh and the terminal says, that the code is has an error:

: not found: script.sh
: not found: script.sh
: not found: script.sh
script.sh 42: script.sh Syntax error: word unexcepted (excepting "in")

When I run it as admin (after login as admin) it works without any issue, so I don't think that the problem related to the code itself. Do the user need some extra permissions or is there something that needs to be installed? This is a bit strange for me.

Thank you for any reply.

hr flag
Check your script for Windows CR-LF line endings (use command `file script.sh` for example). Get out of the habit of running bash scripts with `sh` - they are not the same, in particular sh does not support all of the features of bash.
davede avatar
mq flag
@steeldriver thank you for you reply, very appreciated. I am not sure, what do you mean with Windows, but I check the file with `file script.sh` and it returned `start.sh: Bourne-Again shell script, UTF-8 Unicode text executable, with CRLF line terminators`
Score:0
cd flag

I can't comment (yet) so I wanted to write as an answer.

Like @steeldriver suggested, you ran file command and the output says your file has "CRLF" line terminators.

Run dos2unix script.sh to convert line endings.

Do not run your Bash script with sh.

Also, running your script in debug mode may help:

bash -x script.sh

It would be great that if you can share this output.

davede avatar
mq flag
Hey Ali S, thank you for your reply. I had no `dos2unix` on my machine so I installed it and it looks like, it has fixed my issue. Additionally I have also ran `bash -x script.sh` to debug it but is see many echos and `read menu` at the end. Nothing more. The code is probably not perfect but I do not see any errors or something. I will take a look at it once again tomorrow because I have no much time today but on the first glance it works and I am very happy because of it!
Ali S avatar
cd flag
After it hits to "read" command, does your terminal asked for you an input or just returned back to prompt?
davede avatar
mq flag
it asked me to input but still in 'debug mode'.
Ali S avatar
cd flag
Of course it will be, because you ran it in debug mode. However, you can give your input in debug mode and see how it goes. And then, if you ran into any problems, you can try to fix them. When your script works well, run it normally (without debug mode) and that's it.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.