Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash script with ls $variable

Tags:

bash

I'm trying to get my script to ls all the files in the form "$variable".*

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

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

My problem is the file is named file[success].mpg.001 and what gets fed to ls is file[success].mpg* which gives me ls: cannot access file[success].mpg*: No such file or directory because ls needs to see:

file\[success\].mpg*

I've been trying everything, the one thing I did notice is ls fed the parameter $1 works, but not if fed $file after I've done file=$1 . So I guess how do I change the variable into a parameter?

like image 696
geekmagii Avatar asked May 02 '11 15:05

geekmagii


People also ask

What does [- Z $1 mean in bash?

$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.

What is $1 and $2 in bash?

$0 is the name of the script itself (script.sh) $1 is the first argument (filename1) $2 is the second argument (dir1)

What is $@ in bash script?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.

Does ls work in bash?

First Command: ls The ls command lists the files in your current directory (ls is short for "list"). Try it now by typing ls, then hitting <enter>. Since there is nothing to show, ls shows nothing, and bash simply gives you the next prompt ($), indicating that it is ready for a new command.


2 Answers

You could use bash's printf command to reformat the string. Furthermore you don't need to quote *:

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

printf %q reformats the string in a format that can be used as shell input (regarding to bash's man page).

edit regarding to a comment:
The above solution does not work with white spaces in file names. For those cases (as some other answers already mentioned) it's better not to use printf but to properly quote all variables:

#!/bin/bash
first="$1"
others="${first%.001}"
ls -- "$others"*
like image 63
bmk Avatar answered Nov 22 '22 13:11

bmk


You should quote the variable, not the wildcard:

ls -- "$others"*

The double dash stops search for options so that this code should work even if others begins with a dash.

Note that ls is more often than not the wrong solution in scripts. Use it only if you really want to print the list, e.g. in long format:

ls -l -- "$others"*

If you want to print only the names, you don't need ls at all:

echo "$others"*

Unfortunately you cannot use -- with echo. If, however, you want an array of the file names, use

filenames=("$others"*)

And should you want to iterate over them, use

for filename in "$others"*
like image 29
Philipp Avatar answered Nov 22 '22 12:11

Philipp