解释我2行此shell脚本 [英] Explain me 2 lines of this shell script

查看:221
本文介绍了解释我2行此shell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是什么意思?

 如果[-f $ 2 / $ 1]

和这一行:

  CP $ 1 $ 2 / $ 1

确实 $ 2 / $ 1 重新present一个文件,因为它与 -f 相关?

 #!/斌/庆典
如果测试$#-ne 2
然后
  回声的说法无效号码
其他
  如果[-f $ 2 / $ 1]
  然后
    回声文件已经存在,更换?
    回读
    如果测试$收益率='Y'
    然后
      CP $ 1 $ 2 / $ 1
    其他
      出口
    科幻  其他
    CP $ 1 $ 2 / $ 1
  科幻
科幻


解决方案

  ./ script.shtmp.txt/项目
回音1 $ = tmp.txt
回声$ 2 = /项目
$ 1 - 文件名
$ 2 - 目录
如果[-f $ 2 / $ 1] - 检查文件是否存在,-f的文件,为-d目录
CP $ 1 $ 2 / $ 1 - 将文件复制到目录/ FILE_NAME
如果[-f /project/tmp.txt]
CP tmp.txt /project/tmp.txt

What does this mean?

if [ -f $2/$1 ]

and this line:

cp $1 $2/$1

Does $2/$1 represent a file, because it's associated with -f?

#!/bin/bash
if test $# -ne 2 
then
  echo "Numbers of argument invalid"
else
  if [ -f $2/$1 ]
  then
    echo "file already exist , replace ?"
    read return
    if test $return = 'y'
    then
      cp $1 $2/$1
    else
      exit
    fi

  else
    cp $1 $2/$1
  fi
fi

解决方案

./script.sh "tmp.txt" "/project"
echo $1 = tmp.txt
echo $2 = /project
$1 - file name
$2 - directory
if [ -f $2/$1 ] - checks if file exists, -f for files, -d for directory
cp $1 $2/$1 - copy file to directory/file_name
if [ -f /project/tmp.txt ]
cp tmp.txt /project/tmp.txt

这篇关于解释我2行此shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆