Makefile running command with whoami - makefile

I am trying to use a makefile to setup my machine. I am trying to setup FZF and have the following code. However, it seems to be replacing that command with empty space instead of the user I am logged as. I have SSH'd into the Pi, so not sure if that is the cause or if it's something else.
linuxfxf:
sudo mkdir -p /usr/local/opt
sudo chown -R $(whoami) /usr/local/opt

$ is a special character for make: it introduces a make variable. If you want to run a recipe and have the shell see the $ you have to escape it:
linuxfxf:
sudo mkdir -p /usr/local/opt
sudo chown -R $$(whoami) /usr/local/opt
Or you could use the old-school syntax `whoami` instead.

Related

MacOS, insecure files/directory (zsh compinit)

My shell tells me the following:
❯ compaudit
There are insecure files:
/usr/local/share/zsh/site-functions/_brew
I tried fixing this using $ sudo chmod -R 755 /usr/local/share/zsh/site-functions/_brew and $ sudo chown -R root:staff /usr/local/share/zsh/site-functions/_brew but that didnt help.
The only other solution I have found was to add ZSH_DISABLE_COMPFIX=true into my ~./zshrc file. But isnt that just a workaround to ignore the problem?
I found the solution:
The file that was making problems (/usr/local/share/zsh/site-functions/_brew) was linking to another file (/usr/local/Homebrew/completions/zsh/_brew).
So I ran these 2 commands and I was fine:
$ sudo chmod -R 755 /usr/local/Homebrew/completions/zsh/_brew
$ sudo chown -R root:staff /usr/local/Homebrew/completions/zsh/_brew
Change directory (cd) into
/usr/local/share/zsh/site-functions/_brew
and then run
sudo chmod -R 755 /usr/local/share/zsh/site-functions/_brew
This worked for me.

Install homebrew using Makefile

I'm trying to install Homebrew using a Makefile, the contents of the Makefile is this:
.PHONY: install
install:
# Install homebrew
/usr/bin/ruby -e $(shell curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)
However this just prints the entire contents of the script, but it does not execute anything. I got as far as googling for this issue and seeing that the characters $, (, ) have a special meaning in a Makefile, however I could not find any solution.
try just piping output from curl to ruby like this:
.PHONY: install
install:
# Install homebrew
curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install | ruby
If you encounter the following warning:
Warning: The Ruby Homebrew install is now deprecated and has been rewritten in Bash.
Please migrate to the following command:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
You can modify #igagis answer as such
.PHONY:install
install:
curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh | bash
I was running into needing sudo, then sudo telling me not to run as root so the command i used was:
sudo true
curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh | sudo -u $$USER bash
Notes:
I use sudo true to run sudo and do nothing. that way the user can input their password. running sudo make from the terminal caused the $USER to be root. if you remove the sudo true line, then if sudo has not been run yet, the sudo access check will fail and not let you insert a password when running the curl line
the two $ makes the makefile write a literal $

Cannot change file ownership from bash script

I am trying to run the command
chown -R "$USER" /mnt/namenode
from a bash script to change the ownership of the namenode folder and its sub-folders. When I type the command manually
sudo chown -R ubuntu /mnt/namenode
it works, but not when I do it from the script. From the script, only the ownership of the namenode is changing, not its subfolders. Does anyone know how I might fix this?
OK, I got it. My script was formatting the namenode after the ownership had been set. Before the format, the subfolders has the correct ownership recursively. After the format, the sub-folders' ownership was changing to root. So I made the following change:
echo 'Y' | /home/$USER/hadoop-2.2.0/bin/hadoop namenode -format
sudo chown -R $USER:$USER /mnt/namenode
sudo chown -R $USER:$USER /mnt/datanode
In other words, I set the ownership after I did the formatting.
Try this:
chown -R $USER:$USER /mnt/namenode
If you run the script with sudo (as you are doing), inside the script $USER will be root not the normal user.
So use sudo inside the script:
sudo chown -R "$USER" /mnt/namenode
and run the script without sudo.
I just have the same problem.
Try to add the slash at the end of folder as:
chown -R "$USER" /mnt/namenode/

File permissions, root bash script, edit by user

I have a script that needs to be ran as root. In this script I create directories and files. The files and directories cannot be modified by the user who ran the script (unless there root of course).
I have tried several solutions found here and other sites, first I tried to mkdir -m 777 the directories as so:
#!/bin/bash
...
#Check execution location
CDIR=$(pwd)
#File setup
DATE=$(date +"%m-%d_%H:%M:%S")
LFIL="$CDIR/android-tools/logcat/logcat_$DATE.txt"
BFIL="$CDIR/android-tools/backup/backup_$DATE"
mkdir -m 777 -p "$CDIR/android-tools/logcat/"
mkdir -m 777 -p "$CDIR/android-tools/backup/"
...
I have also tried touching every created file and directory with the $USER as root, like so:
#!/bin/bash
...
#Check execution location
CDIR=$(pwd)
#File setup
DATE=$(date +"%m-%d_%H:%M:%S")
LFIL="$CDIR/android-tools/logcat/logcat_$DATE.txt"
BFIL="$CDIR/android-tools/backup/backup_$DATE"
mkdir -p "$CDIR/android-tools/logcat/"
mkdir -p "$CDIR/android-tools/backup/"
sudo -u $USER touch "$CDIR/"
sudo -u $USER touch "$CDIR/android-tools/"
sudo -u $USER touch "$CDIR/android-tools/logcat/"
sudo -u $USER touch "$CDIR/android-tools/backup/"
sudo -u $USER touch "$CDIR/android-tools/logcat/logcat_*.txt"
sudo -u $USER touch "$CDIR/android-tools/logcat/Backup_*"
...
I have also tried manually running sudo chmod 777 /android-tools/*, and sudo chmod 777 /* from the script directory, gave no errors, but I still cannot delete the files without root permission.
Heres the full script, It's not done yet. Don't run it with an android device connected to your computer.
http://pastebin.com/F20rLJQ4
touch doesn't change ownership. I think you want chown.
If you're using sudo to run your script, $USER is root, but $SUDO_USER is the user who ran sudo, so you can use that.
If you're not using sudo, you can't trust $USER to be anything in particular. The caller can set it to anything (like "root cat /etc/shadow", which would make your above script do surprising things you didn't want it to do because you said $USER instead of "$USER").
If you're running this script using setuid, you need something safer, like id -u, to get the calling process's legitimate UID regardless of what arbitrary string happens to be in $USER.
If you cover both possibilities by making makestuff.sh like this:
# $SUDO_USER if set, otherwise the current user
caller="${SUDO_USER:-$(id -u)}"
mkdir -p foo/bar/baz
chown -R "$caller" foo
Then you can use it this way:
sudo chown root makestuff.sh
sudo chmod 755 makestuff.sh
# User runs it with sudo
sudo ./makestuff.sh
# User can remove the files
rm -r foo
Or this way (if you want to use setuid so regular users can run the script without having sudo access -- which you probably don't, because you're not being careful enough for that):
sudo chown root makestuff.sh
sudo chmod 4755 makestuff.sh # Danger! I told you not to do this.
# User runs it without sudo
./makestuff.sh
# User can remove the files
rm -r foo

how to create AppleScript app to run a set of terminal commands

How would I go about creating an AppleScript command that when I just run the script (or double click it in Finder?), it would run a set of terminal commands? The set of commands completely remove MySQL, and it has become a pain to constantly write them out. The commands are:
sudo rm /usr/local/mysql
sudo rm -rf /usr/local/mysql*
sudo rm -rf /Library/StartupItems/MySQLCOM
sudo rm -rf /Library/PreferencePanes/My*
sudo rm -rf /Library/Receipts/mysql*
sudo rm -rf /Library/Receipts/MySQL*
sudo rm /etc/my.cnf
There is also another command sudo nano /etc/hostconfig that opens a file and I need to delete a line from the file, but that seems like it'd be too hard to code, so I guess I can do that by hand. But it would be a huge help to do this automatically with a single script.
Would it just be a bunch of these commands?
do shell script (...)
Thanks,
Hristo
Yes, you would use do shell script.
However, for the commands where you execute as super user (sudo) you would instead use with administrator privileges. So, for sudo rm /usr/local/mysql you'd do:
do shell script "rm /usr/local/mysql" with administrator privileges
If your list of commands is long, then it might just be easier to put all your commands into a single shell script file, and then execute that shell script using do shell script:
do shell script "/path/to/shell/script" with administrator privileges
You don't actually need to use AppleScript for this - just put all the shell commands in a text file and give it a .command suffix and make sure it's executable (e.g. chmod +x my_script.command) - this will make it double-clickable in the Finder.
I find that .scpt files work best but this is just a preference thing.
Open "Script Editor" and add the following command:
sudo rm -rf /usr/local/mysql; sudo rm -rf /usr/local/mysql*; sudo rm -rf /Library/StartupItems/MySQLCOM; sudo rm -rf /Library/PreferencePanes/My*; sudo rm -rf /Library/Receipts/mysql*; sudo rm -rf /Library/Receipts/MySQL*; sudo rm /etc/my.cnf; say job completed successfully" with administrator privileges

Resources