Bash Script: modify bash

Hey guys, i'm having trouble complete one of my bash scripts
I'm hoping to ---

  1. Modify bash so that then the user types "ls" the command that is executed is "ls -al"
  2. Modify the point of entry in bash when the user accesses it, moving the initial location to /var

I've somewhat done #2, but I can only do it to myself. I'm trying to create a script that creates a new user and than modifies the point of entry. But for some reason I cannot access the file. If I just make a script that does

 echo "cd /var" >> /home/eric/.bashrc 

Then it will work.. however if i'm not doing it to my own account it will not work

And i'm totally lost on #1, what commands change commands? And what files store the commands?

Here is my partial code:

#!/bin/bash

#adds a user
echo "What is the name of the new user? "
read newUser

#creates the new user
sudo useradd -m -s /bin/bash $newUser

#modifies point of entry in bash
echo "cd /var/" >> /home/$newUser/.bashrc  
echo "Entry of bash has been changed to /var/"

#code of modify the 'ls' command to 'ls -al'
#code



 

I'd really appreciate some help. Thank you guys!
-eric

For item 1, take a look at the Shell "alias" command.

For item 2

#modifies point of entry in bash
sudo echo "cd /var/" >> /home/$newUser/.bashrc && echo "Entry of bash has been changed to /var/"
  • The sudo because you must be root to write to someone else's file.
  • The && to echo the message only if the preceeding command succeeded.

Hope this helps.

That doesn't work. If there's a way to do it, I hope someone posts it because it's stumped me for some time.

For example, this fails too:

sudo touch /tmp/a.txt        # Creates a file owned by root
sudo echo test >> /tmp/a.txt

bash: a.txt: Permission denied

sudo sh -c "echo test >> /tmp/a.txt"
1 Like

Thank you Scrutinizer.
I think I've seen that before.
But I never thought of it when I needed it.

Thank you for your responses guys! I appreciate them all!

However, I still cannot find a way to append things to a different file to the new user I created for the .bashrc file.

After I create a new user I'd like to edit the new users .bashrc file but I cannot find a way to edit that file.

 sudo sh -c "echo alias ls='ls -al  >> /home/$newUser/.bashrc 

Does not work

I mean, all of this will work perfectly if I was editing my own currently logged in user .bashrc file
But what about after I create a user?

In theory the script would

  1. create a $newUser of your choise
  2. create an alias in the .bashrc for the $newUser
  3. modify the point of entry in bash in the .bashrc file for the $newUser

Again; it would work perfectly if the user used the script for himself. But that is not the way i'm hoping to do it.

So far this is my full script but it will only work for myself not for the $newUser

  #!/bin/bash


#Adds new user
echo "What is the name of the new user?"
read newUser

#creates a user
sudo useradd -m -s /bin/bash $newUser

#modifies point of entry in bash
echo "cd /var/"  >> /home/$newUser/.bashrc
echo "Entry of bash has been changed to /var/"

#adds an alias
echo "alias ls='ls -al' " >> /home/$newUser/.bashrc

 

Try:

sudo sh -c "echo \"alias ls='ls -al'\"  >> /home/$newUser/.bashrc"
1 Like

Ahh!
Thank you VERY much Scrutinizer, and also to everyone else that helped me!

@Scrutinizer - If you don't mind explaining why that does work? is it because of a regular expression?

Hi LibRid.

The sh -c will nibble away at the outer two double quotes so that what remains is and what gets executed as root becomes:

echo "alias ls='ls -al'" >> /home/$newUser/.bashrc

In turn the echo munches those two double quotes so that what end up at the last line of /home/$newUser/.bashrc is:

alias ls='ls -al'