How to change a files permissions through a prompt using bash scripting - bash

just started to learn how to script and Im trying to make a loop script which will automatically change the permissions of the requested file and ask the user again if they want to change another files permissions. So far this is what I have although I'm not entirely sure how to fully make the loop to work.
#!/bin/bash
until "$input"=no
do
echo "Enter the name of file to change permissions"
read filename
chmod 777 $filename
echo "$filename permissions has been changed"
echo "Would you like to change the permissions of another file?(yes or no)"
read $input
done
echo "You typed: $input"

Thanks to oguz's anwser I solved my problem, Use [ "$input" = 'no' ] instead of "$input"=no, quote $filename and drop that $ from read $input. Or just go read a tutorial on loops and conditional constructs

Related

How to copy multiple using shell script

I am beginner in shell script i am trying to copy multiple files. I want manually give the input how i change my code
echo "Please Enter File Name"
read b
cp -l ~/Downloads/"{$b,$b,$b,$b,$b}" ~/myProj/image
Not sure of what you mean by "I want manually give the input how i change my code."
But try this :
echo "Please Enter File Name"
read b
for i in $b
do
cp -l ~/Downloads/$i ~/myProj/image
done

Reading a Directory and Verifying if it exists Bash

I have checked everywhere and tried many different "Solutions" on checking to see if the directory exists. Here's my code:
#!/bin/bash
echo -e "Where is the directory/file located?"
read $DIRECTORY
if [ -d "$DIRECTORY" ]; then
echo "Exists!"
else
echo "Does not exist!"
fi
What I am trying to do is have the user input a directory and for the script to check if it exists or not and return a result. This will ultimately tar/untar a directory. Regardless of whether the directory exists or not, it returns the answer "Does not exist!". (The input i'm trying is ~/Desktop, and from what I know that is 100% correct. Any concise answers are much appreciated :).
Your script can be refactored to this:
#!/bin/bash
read -p 'Where is the directory/file located?' dir
[[ -d "$dir" ]] && echo 'Exists!' || echo 'Does not exist!'
Basically use read var instead of read $var
Better not to use all caps variable names in BASH/shell
Use single quotes while using ! in BASH since it denotes a history event

How to separate commndline arguments on bash scripting on Ubuntu?

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.

Shell Script that monitors a folder for new files

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.

bash script mv command not working

I'm trying to write a script that renames a file at login in OSX Lion.
Here is my script so far:
#!/bin/bash
if [ -f /Users/$1/Library/Google/GoogleSoftwareUpdate/GoogleSoftwareUpdate.bundle/Contents/MacOS/ksadmin ]; then
mv ~/Library/Google/GoogleSoftwareUpdate/GoogleSoftwareUpdate.bundle/Contents/MacOS/ksadmin ~/Library/Google/GoogleSoftwareUpdate/GoogleSoftwareUpdate.bundle/Contents/MacOS/ksadmin1
say "Successful"
else
say "Unsuccessful"
fi
I've created a LoginHook which executes the script. I know it executes at login because the computer speaks when it can finds the "ksadmin" file. I know it finds the "ksadmin" file because the computer says "Successful". I have also manually renamed the file, logged out, back in and the computer says "Unsuccessful".
The problem is that the script doesn't rename "ksadmin" to "ksadmin1". Have I written to command properly?
Any ideas would be great.
Morgan
What are the permissions on ksadmin? If it's read only for your login id and a ksadmin1 already exists, then you may need a mv -f.
Also, you may want to expand "~" to the absolute path. Not sure when exactly your script gets executed but perhaps bash can not yet expand it.
Thanks to cdarke, Miquel and mVChr for help. The solution to the problem is as follows:
#!/bin/bash
if [ -f /Users/$1/Library/Google/GoogleSoftwareUpdate/GoogleSoftwareUpdate.bundle/Contents/MacOS/ksadmin ]; then
mv /Users/$1/Library/Google/GoogleSoftwareUpdate/GoogleSoftwareUpdate.bundle/Contents/MacOS/ksadmin /Users/$1/Library/Google/GoogleSoftwareUpdate/GoogleSoftwareUpdate.bundle/Contents/MacOS/ksadmin1
say "Successful"
else
say "Unsuccessful"
fi
Version I use for deployment:
#!/bin/bash
if [ -f /Users/$1/Library/Google/GoogleSoftwareUpdate/GoogleSoftwareUpdate.bundle/Contents/MacOS/ksadmin ]; then
mv /Users/$1/Library/Google/GoogleSoftwareUpdate/GoogleSoftwareUpdate.bundle/Contents/MacOS/ksadmin /Users/$1/Library/Google/GoogleSoftwareUpdate/GoogleSoftwareUpdate.bundle/Contents/MacOS/ksadmin1
fi

Resources