im just try to figure out who call root or sudo,cuz i dont want to set some things in root's home folder, when i try this in terminal its work perfect, but doesnt work for spec, what im doing wrong?
terminal:
[Mcfly#Mcfly ~]$ whoami=$(who am i | awk '{print $1}')
[Mcfly#Mcfly ~]$ echo $whoami
Mcfly
SPEC
%define whoami %(who am i | awk '{print $1}')
echo "The user that built this is %{whoami}"
the user that built this is '%{whoami}'
can you help me ?
or there is a easy way to know the user path in rpm-spec, i mean /home/mcfly/ no /root?
regards
What you want is $SUDO_USER, a variable conveniently set by sudo for you.
If you want to ensure that root doesn't do the install, put this in your %pre:
if [ -z "$SUDO_USER" ]; then echo "Please use sudo to ensure configuration files are installed in proper location."; exit 99; fi
if [ z"$SUDO_USER" == "zroot" ]; then echo "Please use sudo from a non-root account to ensure configuration files are installed in proper location."; exit 99; fi
I'm not a Linux guru and you don't explain how your current approach fails to work but the id command looks a simpler alternative.
Related
I have no experience in bash, and I must create one of the type filewatcher that when a file like "FILE_RECEIVED.CSV" arrives in a local directory "C:/directory/", it executes a curl (GET method ) containing the name of that received file.
Something like this:
curl http://localhos:3030/load/process-file?filename=FILE_RECEIVED.CSV&filePathFile=C:/directory/
#!/bin/bash
if [ -z $1 ];
then
echo "especificar un directorio como argumento"
echo "$0 <dir>"
exit 1
fi
while true;
do
for a in $(ls -1 $1/* 2>/dev/null);
do
curl "[http://localhost:3000/load/process-file?filenam=art_2021_inv.csv&filePathFile=C:/Users/mgarc/repo/"
done
sleep 2s
done
executar:
./filewatcher_curl.sh <directorio>
You can use inotifywait to watch changes in a directory. First, if not installed you should install inotifywait. In ubuntu:
sudo apt install inotify-tools
Then, wait for a CREATE event, and get the result with:
NEW_FILE_NAME=$(inotifywait -e CREATE ${DIRECTORY_TO_WATCH} | grep CREATE | awk '{print $3}')
# what ever you want to do with ${NEW_FILE_NAME}
Note that this is just an example and it is not perfect, because for example if two files created simultaneously, you may miss one.
I have set up a http server at localhost, with several sites. I would like to connect to each site root folder, at the same way I used to at a remote server via ssh. So, I tried to create a bash script, intended to log as user "http", giving the site root folder as argument and change the $HOME to the site root folder:
#!/bin/bash
echo "Connecting to $1 as http...";
read -p "ContraseƱa: " -s pass;
su - http << EOSU >/dev/null 2>&1
$pass
export HOME="/srv/http/$1";
echo $HOME;
source .bash_profile;
exec $SHELL;
EOFSU
It does not work, basically because of:
echo $HOME keeps giving out the home folder of the user launching the script.
when the script reaches the end, it ends (obvious), but I would like that it stays open, so I could have a terminal session for user "http" and go on typing commands.
In other words, I am looking for a script that saves me 3 commands:
# su - http
# cd <site_root_folder>
# export HOME=<site_root_folder>
Edit:
Someone suggested the following:
#!/bin/bash
init_commands=(
"export HOME=/srv/http/$(printf '%q' "$1")"
'cd $HOME'
'. .bash_profile'
)
su http -- --init-file <(printf '%s\n' "${init_commands[#]}")
I am sorry but their post is gone... In any case, this give me out bash: /dev/fd/63: permission denied. I am not so skillful to understand the commands above and try to sort it out. Can someone help me?
Thanks.
Possible solution:
I have been playing around, based on what was posted and some googling, and finally I got it :-)
trap 'rm -f "$TMP"' EXIT
TMP=$(mktemp) || exit 1
chmod a+r $TMP
cat >$TMP <<EOF
export HOME=/srv/http/$(printf '%q' "$1")
cd \$HOME
. .bash_profile
EOF
su http -- --init-file $TMP
I admit it is not a nice code, because of:
the temporary file is created by the user executing the script and later I have to chmod a+r so user "http" can access... not so good.
I am sure this can be done on the fly, without creating a tmp file.
If some can improve it, it will be welcome; although in any case, it works!
Your main problem is that the $HOME is evaluated as when the user run the script, meaning that it will get his evaluation of $HOME instead of evaluating it as the given user.
You can evaluate the $HOME as the given user (using the eval command) but I wont recommend it, it is generally bad practice to use this kind of evaluation, especially when talking about security.
I can recommend you to get the specific user home directory with standard linux tools like passwd
Example of the behavior you are seeing:
# expected output is /home/eliott
$ sudo -u eliott echo $HOME
/root
Working it around with passwd:
$ sudo -u eliott echo $(getent passwd eliott | cut -d: -f6)
/home/eliott
In my bash script I want to change file permissions on a particular file called "test.txt" which is located at :
"/var/www/tomcat7/dir1/test.txt"
My question is if I'm giving this full path to the file, I want to make change the permission on all the directories like, "var", "www", tomcat7", "dir1", and finally "test.txt".
File path is given via a separate text file as command-line arguments, and here is my code,
setFilePErmission(){
ssh ppuser#10.101.5.91 "sudo chmod 777 $1"
}
setFilePErmission $1
Can anyone help me? Thank You.... :)
#!/bin/bash
setFilePErmission(){
i=$(echo "$1" | awk -F '/' '{print NF}')
y=$1
while [[ $i -gt 1 ]]
do
ssh ppuser#10.101.5.91 "sudo chmod 777 $y"
y=${y%/*}
(( i-- ))
done
}
setFilePErmission "your path goes here"
Check if this works for you.
I am still doubtful, why would one need such permissions..
Please be sure while running such thing because once you change permission it will be very difficult to get them to previous values unless you dont remember each and every file permissions.
I'm not a pro in shell scripting, thats why I ask here :).
Let's say I got a folder. I need a script that monitors that folder for new files (no prefix name of files is given). When a new file gets copied into that folder, another script should start. Has the second script processed the file successfully the file should be deleted.
I hope you can give me some ideas on how to achieve such script :)
Thank you very much in advance.
Thomas
Try this:
watcher.sh:
#!/bin/bash
if [ -z $1 ];
then
echo "You need to specify a dir as argument."
echo "Usage:"
echo "$0 <dir>"
exit 1
fi
while true;
do
for a in $(ls -1 $1/* 2>/dev/null);
do
otherscript $a && rm $a #calls otherscript with the file a as argument and removes it if otherscript returned something non-zero
done
sleep 2s
done
Don't forget to make it executable
chmod +x ./watcher.sh
call it with:
./watcher.sh <dirname>
try inotify(http://man7.org/linux/man-pages/man7/inotify.7.html)
or you may need to install inotify-tools (http://www.ibm.com/developerworks/linux/library/l-ubuntu-inotify/) to use it by shell.
I have a problem with this one.
It is constantly returning me, not a directory, but is certainly is
#!/usr/local/bin/bash
DIR=$1
if [ -d "$DIR" ]; then
ls -1Apl /home/$DIR | grep -v /\$
else
echo "not a directory"
fi
One more thing, I need a little hint. I have to list files from a given user in a given directory, where I get both the user and directory as parameters.
Just suggestions, please.
Are you in the /home directory when you run this? If not, you may want to change it to:
if [ -d "/home/$DIR" ]; then
to match the ls command. This is assuming you're running it with something like myscript pax to examine the /home/pax directory, which seems to be the case.
And if you want to only list those files in there owned by a specific user, you can use awk to only print those with column 3 set to the desired value ($usrnm), something like:
ls -1Apl /home/$DIR | grep -v /\$ | awk -v user=${usrnm} '$3==user{print}{}'
You're not testing for the existence of the same directory as you're trying to list - maybe you mean -d "/home/$DIR"? Or from your requirement, do you have two parameters?
user="$1"
dir="$2"
# and then examine "/home/$user/$dir"