Bash script: "mkdir -p" doesn't work with var(cat x)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script: "mkdir -p" doesn't work with var(cat x)
# 1  
Old 12-07-2016
Bash script: "mkdir -p" doesn't work with var(cat x)

Hello, Smilie

I've an issue with the creation of a directory, All work without it Smilie So, below, my scripts with the debug output :
Code:
#!/bin/bash

# PATHS
HOME_BACKUP="/home/backup"
HOME_SCRIPT="/home/scripts/test/backup_server"
TARGET="/var/www"

# DATE
DATE_Ymd=$(date +%Y-%m-%d)

# SENDEMAIL
SENDER="MYSERVER@example.com"
RECIPIENT="test@example.com"
SMTP="0.0.0.0"

# DEBBUG
set -x

# SCRIPT
for IP in `egrep -o '([0-9]{1,3}\.){3}[0-9]{1,3}' config_ip.cfg`
do
        ssh "$IP" 'hostname' > hostname.txt
        HOSTNAME=$(cat hostname.txt)

                cd "$HOME_BACKUP"
				
				mkdir -p "$HOSTNAME"
				
				cd "$HOSTNAME"

                mkdir "$DATE_Ymd"

        rsync -azvtP test_user@"$IP":"$TARGET" "$HOME_BACKUP"/"$HOSTNAME"/"$DATE_Ymd"

        tar -czvf "$HOME_BACKUP"/"$HOSTNAME"/"$DATE_Ymd".tar.gz "$DATE_Ymd"

                rm -rf "$DATE_Ymd"

                cd "$HOME_SCRIPT"
         
                rm "$HOME_SCRIPT"/hostname.txt
done

rm dry_run.txt

Config File :
Code:
IP CLIENT: 192.168.1.93
IP CLIENT: 192.168.1.30

Debug
Code:
test_user@MYSERVER:/home/scripts/test/backup_server$ ./backup_script.sh
++ egrep -o '([0-9]{1,3}\.){3}[0-9]{1,3}' config_ip.cfg
+ for IP in '`egrep -o '\''([0-9]{1,3}\.){3}[0-9]{1,3}'\'' config_ip.cfg`'
+ ssh 192.168.1.93 hostname
++ cat hostname.txt
+ HOSTNAME=vm1
+ cd /home/backup
+ cd vm1
+ mkdir 2016-12-07
+ rsync -azvtP test_user@192.168.1.93:/var/www /home/backup/vm1/2016-12-07
receiving incremental file list
www/
www/html/
www/html/COPYING
         19,419 100%   18.52MB/s    0:00:00 (xfr#1, to-chk=28/31)
+ tar -czvf /home/backup/vm1/2016-12-07.tar.gz 2016-12-07
2016-12-07/
2016-12-07/www/
2016-12-07/www/html/
+ rm -rf 2016-12-07
+ cd /home/scripts/test/backup_server
+ rm /home/scripts/test/backup_server/hostname.txt

For the first IP CLIENT there aren't problem, but for the next (Assuming that the $HOSTNAME directory does not exist) it doesn't work, it even not treat the command :
Code:
+ for IP in '`egrep -o '\''([0-9]{1,3}\.){3}[0-9]{1,3}'\'' config_ip.cfg`'
+ ssh 192.168.1.30 hostname
++ cat hostname.txt
+ HOSTNAME=vm2
+ cd /home/backup
+ cd vm2
./backup_script.sh: line 16: cd: vm2: No such file or directory
+ mkdir 2016-12-07
+ rsync -azvtP test_user@192.168.1.30:/var/www /home/backup/vm2/2016-12-07
receiving incremental file list
rsync: mkdir "/home/backup/vm2/2016-12-07" failed: No such file or directory (2)
rsync error: error in file IO (code 11) at main.c(674) [Receiver=3.1.0]
+ tar -czvf /home/backup/vm2/2016-12-07.tar.gz 2016-12-07
2016-12-07/
tar (child): /home/backup/vm2/2016-12-07.tar.gz: Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now
+ rm -rf 2016-12-07
+ cd /home/scripts/cpp/backup_server
+ rm /home/scripts/cpp/backup_server/hostname.txt

I tried with a simple condition [[ -d $dir ]] I've the same result..
Please who can I help me ? Smilie

---------- Post updated at 02:49 PM ---------- Previous update was at 01:58 PM ----------

The problem was a "rights issue".. I copied my original script for my tests with the root right and when I executed the copied script with the user_test, it did not have the right to create the directory, stupid mistake.. I hope have helped others

Last edited by Arnaudh78; 12-07-2016 at 03:14 PM..
This User Gave Thanks to Arnaudh78 For This Post:
# 2  
Old 12-07-2016
Glad you got it working, thanks for coming back to report the problem. Very strange how it did not report mkdir -p in the log file, or any of its error messages.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Dummies Questions & Answers

"Help with bash script" - "License Server and Patch Updates"

Hi All, I'm completely new to bash scripting and still learning my way through albeit vey slowly. I need to know where to insert my server names', my ip address numbers through out the script alas to no avail. I'm also searching on how to save .sh (bash shell) script properly.... (25 Replies)
Discussion started by: profileuser
25 Replies

3. Shell Programming and Scripting

Why doesn't "grep -w" ALWAYS work?

Working with IP addresses is a pain... Here's my problem: I'm putting some interface information to a file: 3.185.201.2 | Tu1102 152.162.54.130 | Gi0/2.36 3.185.226.2 | Gi0/1 3.185.0.82 | Tu1 ... (12 Replies)
Discussion started by: turk22
12 Replies

4. AIX

AIX 5.3 on p275 - "|" key doesn't work (!)

More issues. The "|" key doesn't work at all, either in the text terminal or in mwm (which starts fine when I do "startx"). How am I supposed to troubleshoot without a "|" key? (2 Replies)
Discussion started by: smithfarm
2 Replies

5. AIX

"/" doesn't work on command prompt for searching commands last typed

When I use "/" to look for a particular command that I typed in the current session it says D02:-/home/user1/temp> /job ksh: /job: not found. D02:-/home/user1/temp> previously it used to fetch all the commands which had job in it.. for example subjob, endjob, joblist etc... may I... (7 Replies)
Discussion started by: meetzap
7 Replies

6. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

7. UNIX for Dummies Questions & Answers

Help! "grep" doesn't work for me!

totally pis*ed off. I have a data set (xxx.txt), as follows: chr1 3821 rs127372 A/C 0.823 chr1 3822 rs127376 A/C/G 0.899 chr1 3722 rs612634 A/C 9.22 chr1 3262 rs7152 A/T 0.22 chr1 3711 rs737 A/C/G 0.2323 ....... I only want to get those lines... (6 Replies)
Discussion started by: kaixinsjtu
6 Replies

8. UNIX for Advanced & Expert Users

sometimes "ps -elf" command doesn't work

when i give "ps -elf" or "ps" system gets hung. if i press "^c" come out from it... pls help..what should i do to get it resolved. thanks CKanth (4 Replies)
Discussion started by: srikanthus2002
4 Replies

9. Linux

By angle-brackets/"pipe" button doesn't work?

How can I configure it? I have a swedish keyboard with swedish keyboard setting. Everything works perfectly (едц) except that button. What can be wrong? /Richard ++ NOTE: It seems like the computer notices the input but that the button isn't assigned to anything (the keyboard-cursor stops).... (1 Reply)
Discussion started by: riwa
1 Replies

10. Shell Programming and Scripting

my "case" doesn't work !

I'm using the case statement in the following script and it always takes the "*" default choice while it should be "3". I wonder why ??? dt_auj=`date +%d` NBLOG=`ls -al /users/notes01/LOG/t*|awk '{print $7}'|grep $dt_auj|wc -l` case $NBLOG in 1) cat ~/LOG/console-notes > $fic_tmp1 ;; 2)... (5 Replies)
Discussion started by: Nicol
5 Replies
Login or Register to Ask a Question