質問

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top