You are not logged in.

#1 2011-11-03 08:59:09

muffinss
Member
From: united states
Registered: 2011-02-07
Posts: 54

Need help with my basic script..

Hello all :]

I'm trying to make a script to automatically creates a new, blank scripting file..

Here's my code:

#!/bin/bash
#email@email.com
#Adam

read -p "Please input a name for the script: " nm

if
	[[ "$nm" == "$nm" && "$nm" != "exit" && "$nm" != "" && "$nm" != "quit" && "$nm" != "q" ]]
then
	touch ~/scripts/"$nm"
	echo "#!/bin/bash" >> ~/scripts/"$nm"
	echo "#email@email.com" >> ~/scripts/"$nm"
        echo "#Adam"  >> ~/scripts/"$nm"
	su root -c "chmod a+x ~/scripts/"$nm" && exit"
elif
	[[ "$nm" == "exit" || "$nm" == "" || "$nm" == "quit" || "$nm" == "q" ]]
then
	echo "Exiting"
	sleep 1
	exit
fi

What I want it to do is also have the script to set its permissions to make it executable, though to do this I need to have it access root. I figured that part out, but the problem is that I can't figure out how to have it automatically set the home directory to the user that started the script when the script gets elevated to root. I really want the script to be universal for I can run it as any user without needing to constantly change the script to add in the current users home directory.

Last edited by muffinss (2011-11-03 09:13:14)

Offline

#2 2011-11-03 09:19:56

whitethorn
Member
Registered: 2010-05-02
Posts: 153

Re: Need help with my basic script..

Well why don't you do run a whoami and save it as a variable then use that?

name=`whoami`
...
/home/"$name"/scripts

Are you sure you need root priviledges to set the executable bit? The script is being saved in the users own home folder it should be possible for them to set +x in their own home folder.

Offline

Board footer

Powered by FluxBB