i am trying to program a code for file backup using linux CLI bash shell but i keep getting a syntax error near unexpected token then' ,
if[$? -eq 0 ]; then ‘
<code>#!/bin/bash
# Define source and destination directories
sourceDir="/path/to/source"
destinationDir="/path/to/backup"
# Create timestamp for backup folder
backupFolder=$(date +"%Y-%m-%d_%H-%M-%S")
# Create backup directory if it doesn't exist
if [ ! -d "$destinationDir/$backupFolder" ]; then
mkdir -p "$destinationDir/$backupFolder"
fi
# Copy files from source to destination
cp -r "$sourceDir" "$destinationDir/$backupFolder"
# Check if backup was successful
if [ $? -eq 0 ]; then
echo "Backup completed successfully. Files are stored in $destinationDir/$backupFolder"
else
echo "Backup failed. Please check your source and destination directories."
fi
</code>
<code>#!/bin/bash
# Define source and destination directories
sourceDir="/path/to/source"
destinationDir="/path/to/backup"
# Create timestamp for backup folder
backupFolder=$(date +"%Y-%m-%d_%H-%M-%S")
# Create backup directory if it doesn't exist
if [ ! -d "$destinationDir/$backupFolder" ]; then
mkdir -p "$destinationDir/$backupFolder"
fi
# Copy files from source to destination
cp -r "$sourceDir" "$destinationDir/$backupFolder"
# Check if backup was successful
if [ $? -eq 0 ]; then
echo "Backup completed successfully. Files are stored in $destinationDir/$backupFolder"
else
echo "Backup failed. Please check your source and destination directories."
fi
</code>
#!/bin/bash
# Define source and destination directories
sourceDir="/path/to/source"
destinationDir="/path/to/backup"
# Create timestamp for backup folder
backupFolder=$(date +"%Y-%m-%d_%H-%M-%S")
# Create backup directory if it doesn't exist
if [ ! -d "$destinationDir/$backupFolder" ]; then
mkdir -p "$destinationDir/$backupFolder"
fi
# Copy files from source to destination
cp -r "$sourceDir" "$destinationDir/$backupFolder"
# Check if backup was successful
if [ $? -eq 0 ]; then
echo "Backup completed successfully. Files are stored in $destinationDir/$backupFolder"
else
echo "Backup failed. Please check your source and destination directories."
fi
i have tried deleting the space between “]; then”
New contributor
Fola Ola is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.