LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   (bash) echo "#!/bin/bash" event not found - trying to generate profiles automatically (https://www.linuxquestions.org/questions/programming-9/bash-echo-bin-bash-event-not-found-trying-to-generate-profiles-automatically-441233/)

jimieee 05-03-2006 08:34 AM

(bash) echo "#!/bin/bash" event not found - trying to generate profiles automatically
 
Hi all,

I'm trying to write a simple bash script to create users on Linux and Solaris systems. Part of the spec is that I have to be able to generate a bash profile on the Solaris systems (this is done automatically on our Linux distros), so I wrote some code a bit like this:

Code:

    echo "#!/bin/bash" >> $USER_HOME.bash_profile
    echo "export PS1=\"\u@\h:\w\n$ \"" >> $USER_HOME.bash_profile
    echo "alias ll=\"ls -altr\"" >> $USER_HOME.bash_profile

However when I run that first line I get the error:

Code:

bash: !/bin/bash": event not found
So I escape the bang:

Code:

$ echo "#\!/bin/bash"
#\!/bin/bash

But as you can see the bang is still escaped in the echo'd text. Is there a better way to do this?

druuna 05-03-2006 08:43 AM

Hi,

Use single quotes instead of double quotes. I.e:

echo '#!/bin/bash' >> $USER_HOME.bash_profile

Or even better: Use an editor to make the script :)

Hope this helps.

perfect_circle 05-03-2006 08:48 AM

Yeah, I don't know what this is. Maybe it has to do with accessing the history commands. Look at this:

Code:

skalkoto@darkstar:~$ echo "#!"
echo "#"#!
##!
skalkoto@darkstar:~$

But I can provide you 2 different solutions
Either put a space between ! and /. With this:
Code:

#! /bin/bash
it's still recognized as a Bourne-Again shell script text executable.

Or even better use ' instead of ":
Code:

skalkoto@darkstar:~$ echo '#!/bin/bash'
#!/bin/bash
skalkoto@darkstar:~$

;)

perfect_circle 05-03-2006 08:50 AM

Quote:

Originally Posted by druuna
Hi,

Use single quotes instead of double quotes. I.e:

echo '#!/bin/bash' >> $USER_HOME.bash_profile

Or even better: Use an editor to make the script :)

Hope this helps.

Hey! you type faster.....:cry:

marozsas 05-03-2006 08:53 AM

Quote:

Originally Posted by druuna
Or even better: Use an editor to make the script :)

This is, for sure, the best advice you can get.

Just to add some more information:
Create a template file, e.g. bash_profile.template and simple copy it to the home directory, changing the name in the copy command.

If you will use the useradd command, the "-m -k" options will create the home dir and copy the files from a template dir to the user's home dir.

druuna 05-03-2006 09:03 AM

Hi all,

I type fast, give good advice and had an excelent yearly review at work today......

Guess I'm having a good day ;)

marozsas 05-03-2006 09:11 AM

Quote:

Originally Posted by druuna
Hi all,
I type fast, give good advice and had an excelent yearly review at work today......

Guess I'm having a good day ;)

Congratulations !

Don't forget to try the Lotto ! ;)

jimieee 05-03-2006 09:12 AM

*slaps forehead* I could have sworn I'd tried that and failed with the same result :( Oh well. Need more coffee I suppose.

BTW I thought of that idea of copying the profile, but you can't guarantee that the template is going to be in any specific location, especially when you're working cross platform. I had considered scp'ing it from somewhere, but this is just supposed to be a simple script and I don't want to mess around with keys or passwords.

druuna 05-03-2006 09:24 AM

Hi,

Why not use /etc/skel to store the file. Both linux and slorais (9 at least) support this. If the content of /etc/skel isn't put in the (new) homedir automatically during user creation you could try to force it with useradd's -k skeleton_dir option.

Hope this clears things up.

jimieee 05-03-2006 10:24 AM

This is more just a way to configure a user on any server, until we get some kind of centralised authentication mechanism going. I don't really want to have to put a default profile on every server on the network just yet.


All times are GMT -5. The time now is 01:48 AM.