0
#!/bin/sh
BackupPath="/root/upfOps/te"
if [ ! -d "$BackupPath" ]; then
    mkdir -p "$BackupPath"
fi

MYSC_JSON_YAML_DB="home/mysc/cmin02sms/etc/{*.json,*.yaml,*.db}"
tar vczf /root/upfOps/te/t1.tar.gz $MYSC_JSON_YAML_DB

echo $MYSC_JSON_YAML_DB
tar vczf /root/upfOps/te/t2.tar.gz /home/mysc/cmin02sms/etc/{*.json,*.yaml,*.db}

Output:

[root@upf-100.novalocal upfOps]# ./test
tar: home/mysc/cmin02sms/etc/{*.json,*.yaml,*.db}:无法 stat: 没有那个文件或目录
tar: 由于前次错误,将以上次的错误状态退出
home/mysc/cmin02sms/etc/{*.json,*.yaml,*.db}
tar: 从成员名中删除开头的“/”
/home/mysc/cmin02sms/etc/conf.ismpbus.json
/home/mysc/cmin02sms/etc/conf.simu.json
/home/mysc/cmin02sms/etc/conf.slrun.json
/home/mysc/cmin02sms/etc/conf.smp.json
/home/mysc/cmin02sms/etc/chassis.yaml
/home/mysc/cmin02sms/etc/lager.yaml
/home/mysc/cmin02sms/etc/microservice.yaml
/home/mysc/cmin02sms/etc/conf.crontab.db
[root@upf-100.novalocal upfOps]# 

The first tar command results in "cannot stat: no such file or directory". But the second tar can compress those files successfully.

How to compress a file with multiple file suffixes in a variable? can someone give a solution in any ways just use the sh or bash scripts?

I want use only one variable such as

tempPath=home/mysc/cmin02sms/etc/{*.json,*.yaml,*.db}
tar cvzf /root/etcFiles.tar.gz $tempPath

what is the right ways to write the tempPath variable?

sleepy2con
  • 51
  • 6
  • 2
    The wildcards work, the brace expansion doesn't (because brace expansion is done before variable expansion). It's better to do the expansion early, and store the result (i.e. the list of filenames) in an array. – Gordon Davisson Jan 12 '23 at 04:57
  • 2
    my `#!/bin/sh` doesn't even know about brace expansion (-; . Good luck to all. – shellter Jan 12 '23 at 05:18
  • Please [don’t post images of code, error messages, or other textual data.](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors) – tripleee Jan 12 '23 at 07:28
  • Please remove the _bash_ tag, since the question does not relate to bash. – user1934428 Jan 12 '23 at 08:03

1 Answers1

1

Your options are limited with #!/bin/sh. If you can change that to bash (#!/usr/bin/env bash being the recommended way), you can use extended file match patterns via the extglob shell option:

shopt -s extglob
tar vczf /root/upfOps/te/t1.tar.gz home/mysc/cmin02sms/etc/@(*.json|*.yaml|*.db)

I did without the intermediate variable. You could use one if you want, but storing multiple values in a single var is bad practice; it really should be an array (which, like extglob, is not a POSIX feature so requires bash). You assign the array like so:

prefix=home/msyc/cmin02sms/etc
files=("$prefix"/@(*.json|*.yaml|*.db_))

Then using the array in the tar looks like this:

tar vczf /root/upfOps/te/t1.tar.gz "${files[@]}"

Notice I didn't use an all-caps variable name; all-caps should be reserved for variables that are special, usually ones that are exported into the environment for the benefit of some external program you're launching from the shell you set them in.

Mark Reed
  • 91,912
  • 16
  • 138
  • 175