Bash script with ls $ variable

I am trying to get the script all the files in the form of "$ variable". *

the parameter is always a file with the extension .001 I want to find all files with the same name, but a different extension, for example $ file.002, file.003, etc., For example:

first="$1"
others=${first%.001}
ls $others"*"

My problem is in the file: file[success].mpg.001 and what gets from ls file[success].mpg*, which gives me ls: cannot access file[success].mpg*: No such file or directory because I lshave to see:

file\[success\].mpg*

I tried everything, the only thing I noticed ls gave the parameter $ 1, but not if it was uploaded by $ file after I made the file = $ 1. So, I think, how to change a variable in a parameter?

+5
source share
4 answers

bash printf . , *:

#!/bin/bash
first="$1"
others=$(printf %q "${first%.001}")
ls $others*

printf %q , ( bash man-).

:
. ( ) printf, :

#!/bin/bash
first="$1"
others="${first%.001}"
ls -- "$others"*
+6

, :

ls -- "$others"*

, , others .

, ls . , , . :

ls -l -- "$others"*

, ls:

echo "$others"*

, -- echo. , , ,

filenames=("$others"*)

,

for filename in "$others"*
+5

. 3 :

ls -- "${others}".*

$others . * . ls. , ls - :

file1.001 file1.002 file1.003 ...

, , glob ls, :

file1.*

, ls $others"*" , ls .

file1.* ( ), ls .

- basename:

first="$1"
others=`basename -- "$first" .001`
ls -- "${others}".*

basename , ..

, , , , find .

EDIT: * nix, ls basename, cli . , , . .

+2

find ls:

$ ls weg.*
ls: weg.*: No such file or directory
$ find . -name weg.\*
$
+1
source

All Articles