Creating a Directory Through shell script and retrieve the path - linux

I am trying to create a directory through shell script. Through some commands I am able to create a directory with permission string 777. Now I want to fetch the path of the created directory so that I can move a file into that.
Below is the code though which I am trying.
It will store datetime
NOW=$(date +"%Y-%m-%d")
It will store hostname
HOST=$(hostname -s)
Create directory with permission
LOG_DIRECTORY=$(mkdir -m 777 DIP_${HOST}_${NOW}_50users)
To fetch path
path="$(dirname /home/e250842/${LOG_DIRECTORY})";
And display path
echo "$path"
But the problem is that LOG_DIRECTORY is not a path.So please suggest some command to fetch the path like /home/e250842/CreatedDirectoryName/.
An example is also helpful.
Thanks in Advance.

You can change your code
LOG_DIRECTORY=$(mkdir -m 777 DIP_${HOST}_${NOW}_50users)
path="$(dirname /home/e250842/${LOG_DIRECTORY})"
to
LOG_DIRECTORY="DIP_${HOST}_${NOW}_50users"
mkdir -m 777 "${LOG_DIRECTORY}"
path="/home/e250842/${LOG_DIRECTORY}"

now=$(date +"%Y-%m-%d")
host=$(hostname -s)
path=$(pwd)/DIP_${host}_${now}_50users
mkdir -m 777 "$path"
echo "$path" -- display path

Related

Bash script, redirect output to another directory

I have an environment variable containing the name of a directory. I am trying to redirect output from an echo command to a text file in a different directory.
For example
DIR="NewDirectory"
mkdir $DIR
echo "Testing" >> "$DIR\file.txt"
Results in a file named NewDirectory\file.txt in the working directory of the script...what exactly am I missing here? The directory is created without issue, so I am not sure what is going on here.
You have to change \ into /:
DIR="NewDirectory"
mkdir -p $DIR
echo "Testing" >> "$DIR/file.txt"
Changed mkdir -p as suggested by #Jord, because -p means: no error if existing, make parent directories as needed
In linux (or unix for that matter), the directory separator is a slash (/), not a backslash (\):
DIR="NewDirectory"
mkdir $DIR
echo "Testing" >> "$DIR/file.txt"
Your line
echo "Testing" >> "$DIR\file.txt"
should read
echo "Testing" >> "$DIR/file.txt"
as / is the separator in paths in Linux.

Copying syslog file to a new directory in linux

I'm currently having an assignment to write a bash script that can perform backup log (syslog, dmesg and message) files to a new directory. I wrote my script like this:
cd /var/log
sudo cp syslog Assignment
The file "Assignment" is in my home directory. When I used the "ls" command in my Assignment folder, I don't find a copy of syslog in there. Can someone tell me where did I go wrong? Thanks in advance.
I think you mean Assignment folder, not Assignment file. Anyways if you cd to /var/log, then when you do a cp in /var/log it will think Assignment is local to /var/log. If you do an ls in /var/log now you will see a copy of syslog called Assignment in /var/log. To get syslog copied to the assignment folder in your home directory you need to specify the absolute path not the relative path. Use the tilde, ~, to specify the home directory. So your script should say
cd /var/log
sudo cp syslog ~/Assignment/
You can try this:
#!/bin/sh
if ! [ $1 ] ; then
echo "Usage:";
echo $0 "<directory_where_to_save_logs>";
return;
fi
if [ ! -d "$1" ]; then
echo "Creating directory $1";
mkdir $1;
fi
cp /var/log/syslog* $1
cp /var/log/dmesg* $1
Thanks

How do you refer to an error in Bash/Shell Script?

Is it possible to refer to an error? Here is my code:
read dir
mkdir /Users/Dillon/$dir
And if the directory is already there, it tells me mkdir: /Users/Dillon/(dir): File exists
. Is there a way to state that if it already exists to not not show error?
You can test for directory existence before running the command:
[ -d /Users/Dillon/$dir ] || mkdir /Users/Dillon/$dir
Alternately, you can use the -p flag:
mkdir -p /Users/Dillon/$dir
This will make the directory if it does not yet exist, as well as any missing directories in the path. It does not complain if the directory already exists. It will complain if any segment of the path exists, but isn't a directory or a symlink to a directory.
To suppress error output for any command, redirect the stderr stream to /dev/null
mkdir /Users/Dillion/$dir 2> /dev/null
Or for this one specific case, you could first check for the existence of the directory and bypass the mkdir call if the directory exists:
if [ ! -d /Users/Dillion/$dir ]; then
mkdir /Users/Dillion/$dir
fi

Linux - Creating multiple folders from a text file

#!/bin/bash
IFS='
'
for _dir in $(cat "$1 usernames.lnk"); do
mkdir "$_dir Windows"
done
I am having trouble with this script, It has to create a folder within the Windows folder of each name that is listed in the usernames.lnk text file, but everytime I try and run the script it states "usernames.lnk" directory could not be found. The script and the username file and the Windows folder are all in the same folder. Thanks in advance.
This will read each line of the file usernames.lnk and create a directory under the directory Windows (relative path).
#!/bin/bash
while read -r line; do
mkdir "Windows/$line"
done < usernames.lnk
This way should work:
#!/bin/bash
for i in `cat usernames.lnk`;
do mkdir "Windows/$i";
done

Linux script for copying files from multiple windows machines

Having a issue trying to make a bash script that will read ip address and usernames from a file that mount a connection to that share on windows and then copy ano file types into a new folder called the users name.
At the moment it doesn't quite work, it makes hundreds of folders called *.ano if it can not find the windows share.
Help please
Text file:
192.168.0.2 user1
192.168.0.3 user2
bash script:
USER='/home/user/user.ip'
IPADDY=$(grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' $USER)
USERNAME=$(awk '{ print $NF }' $USER)
for i in $IPADDY $USERNAME
do
mkdir /home/user/Documents/$USERNAME
mount -t smbfs //$IPADDY/$USERNAME /home/user/$USERNAME
rsync -va /home/user/$USERNAME/*.ano /home/user/Documents/$USERNAME/*.ano
done
Hi all thanks for such a quick reply, I have change the code as follow but still get multiple files have I done something wrong here
USER='/home/user/user.ip'
IPADDY=$(grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' $USER)
USERNAME=$(awk '{ print $NF }' $USER)
while read IPADDY USERNAME; do
mkdir /home/user/Documents/$USERNAME
mount -t smbfs //$IPADDY/$USERNAME /home/user/$USERNAME
rsync -va /home/user/$USERNAME/*.ano /home/user/Documents/$USERNAME/
done < $USER
The problem is in the for command. In your script, i iterates over the contents of $IPADDY, then it iterates over the contents of $USERNAME. Meanwhile, $USERNAME inside the loop gets expanded to user1 user2, resulting in:
mkdir /home/user/Documents/user1 user2
The mount line becomes:
mount -t smbfs //192.168.0.2 192.168.0.3/user1 user2 /home/user/user1 user2
And so on.
Rather, loop over the file itself:
while read IPADDY USERNAME; do
#awesome things here based on $IPADDY and $USERNAME
done < $USER
You might want to add [[ -z $IPADDY ]] && continue to skip over any possible blank lines in the file.
One problem is that you use a wildcard (*) for the destination files. But those files don't exist - therefore /home/user/Documents/$USERNAME/*.ano cannot match and rsync will create a file *.log.
Better do:
rsync -va /home/user/$USERNAME/*.ano /home/user/Documents/$USERNAME/

Resources