Loop through list of names and copy file explained? - bash

I've written a script to copy a new user account file to the new user location. It works by reading a list of usernames and copying the file to that location. I can't understand why I needed the done < $USER at the end. Can someone please explain this?
Thanks
USER=/home/example/new.txt
NEWUSER=$USER
LOC=/var/account/
cd /home/example
while read NEWUSER
do
cp _newuser.txt $LOC/$NEWUSER
done < $USER

To iterate over each line in the file /home/example/new.txt, which is the value of variable USER
Pls look at http://en.kioskea.net/faq/1757-how-to-read-a-file-line-by-line
< is input redirection operator (http://www.tldp.org/LDP/abs/html/io-redirection.html)
You can also delete NEWUSER=$USER, since I do not see any use of NEWUSER except the while loop. Due to the while, NEWUSER will be assigned a new value each iteration.

Because read reads the input from standard input (stdin). In order to read from file you need to redirect it to the read command.

Related

Bash: Why are single quotes in my output files replaced with ~#~X and ~#~Y

function GetFilespace() {
echo "Group ${1}"
# run du -hs on every user in the /data/ directory. Append to a text file
du -hs /home/group${1}/data/* >> /home/group1/data/user/stats/filespace_used_individual_${1}.txt
echo "Done with group ${1}"
}
# Loop through the 4 user groups we have
for GROUP in group1 group2 group3 backup1
do
# Launch GetFilespace function and append stderr output to a text file
GetFilespace $GROUP 2>> /home/group1/data/user/stats/permission_denied_${GROUP}.txt &
done
I use a code similar to what I have posted above to get stats about individual file space usage and file size for several storage systems where I work. The function GetFilespace() gathers the size of our users individual directories, and I collect stderr output with 2>> to track which specific files are missing their group read/execution permissions.
Normally when I run this code my stderr output files have lines that look like fig A below (censored for privacy). From this I collect the users name and send them a full path to their offending files so they can resolve the issue.
Figure A
However, when I ran my script this week my script that handles user permission issues stopped working. Checking one of the stderr files I found that all single quotes were replaced by ~#~X and ~#~Y at the beginning and end of the paths respectively (fig B).
Figure B
I checked back through my archived stats and I have no history of this happening before, nor have I been able to find information online. Could anyone explain what is happening to me, and how to resolve the issue?
Thank you!

Is it possible to use scp and change a filename to $date

So I have this script that creates a file called "myfile.txt" this name will never change.
I have 2 servers srv01 and srv02
srv01:/home/user/scriptdata/myfile.txt is the file I want to move to srv02
I want this file to arrive at srv02 in the following format
srv02:/home/user/scriptoutput/$date.txt
Is there an easy way to do this?
So far I have been using scp /home/user/scriptdata/myfile.txt 12.34.56.78:/home/user/scriptoutput/day-month-year.txt manually
scp /home/user/scriptdata/myfile.txt 12.34.56.78:/home/user/scriptoutput/data_$(date +%d-%m-%Y_%H:%M).txt
This worked! thanks jhnc for ur answer

How to get the full name of a folder if I only know the beginning

I am receiving an input from the user which looks like follows:
echo +++Your input:+++
read USER_INPUT
The way I should use it is to retrieve the full name of a folder which starts with that input, but that contains other stuffs right after. All I know is that the folder is unique.
For example:
User input
123456
Target folder
/somepath/someotherpath/123456-111-222
What I need
MYNEED=123456-111-222
I was thinking to retrieve this with an MYNEED=$(ls /somepath/someotherpath/$USER_INPUT*), but if I do this I will get instead all the content of /somepath/someotherpath/123456-111-222 because that's the only folder existing with that name so the ls command directly goes to the next step.
May I have your idea to retrieve the value 123456-111-222 into a variable that I will need to use after?
basename extracts the filename from the whole path so this will do it:
MYNEED=$(basename /somepath/someotherpath/123456*)

Batch command to edit filestream

'echo 0 > Q:\FactoryRecovery\RECOVERY.INI:Done'
Can anyone please explain to me how this command works? I'm curious after reading this Superuser post: https://superuser.com/questions/384658/why-can-i-only-create-one-factory-backup-from-my-lenovo-thinkpad
It allows you to create more than one recovery disk.
Ok, heres a brief expanation:
Echo 0
Simply outputs 0 to the screen. Thats not to complicated. adding a >> and a file path after this will redirect the output to the end of the file. Adding a > will replace the contents of the file with what is being redirected.
Hence:
Echo 0 > Q:\FactoryRecovery\RECOVERY.INI
Will replace the contents of Recovary.INI with 0(while it used to be 1).
It prevents the file needed to create additional recovary disks from exiting, essentially allowin you to create more than one.
Mona.

What does this bash script function does

I am new to shell scripting and i found this function in a given script file.
##############################
# rotate_daily(filename)
rotate_daily() {
_code=0
_file_src=$1
_today=`date '+%Y-%m-%d'`
_file_dest=${_file_src}.${_today}
if [ -f ${_file_dest} ]; then
printk "rotate_daily(): ${_file_dest} already exist"
_code=1
else
if [ -f ${_file_src} ]; then
printk "rotate_daily(): ${_file_src} => ${_file_dest}"
cp -p ${_file_src} ${_file_dest}
_code=$?
>${_file_src}
fi
fi
}
I understand this is kind of coping file from one location to another location. But, it is not rotating right?. could somebody explain me what it is really does.
thanks in advance for any help
It copies _file_src to the location file_dest unless _file_dest already exists. An informative message will be printed that tells you if the file already exists or file_src_ will be copied, It also moves _file_src only if it is a file.
EDIT: forgot to mention what the command >{_file_src} does - it simply wipes out the contents of the source file. So you will have the contents of _file_src moved to file_dest in the end and _file_src will be empty. I can't figure why not simply do a move(with mv) and then create an empty file, but that's your question.
If the time stamped file already exists, this code snippet does nothing but print a message via printk indicating that. If it does not exist, it copies the source file to it and truncates the source file. I would guess that the line you are not quite understanding is:
>${_file_src}
That line truncates the original file after it has been copied. Note that there is a race condition, and any data written to the file between the copy and the truncation will be lost.

Resources