Using $ @ is correct

I am trying to write a tiny script that accepts any number of command line arguments that prints permissions rwxfor a file (not a directory)

I have

file=$@    
if [ -f $file ] ; then    
ls -l $file    
fi

However, it accepts only one command line argument. Thanks for any help.

+3
source share
5 answers

Here is a demonstration of some of the differences between $*and $@, with and without quotation marks:

#/bin/bash
for i in $*; do
    echo "\$*: ..${i}.."
done; echo
for i in "$*"; do
    echo "\"\$*\": ..${i}.."
done; echo
for i in $@; do
    echo "\$@: ..${i}.."
done; echo
for i in "$@"; do
    echo "\"\$@\": ..${i}.."
done; echo

Launch:

user@host$ ./paramtest abc "space here"
$*: ..abc..
$*: ..space..
$*: ..here..

"$*": ..abc space here..

$@: ..abc..
$@: ..space..
$@: ..here..

"$@": ..abc..
"$@": ..space here..
+8
source

How about this:

for file
do
    test -f "$file" && ls -l "$file"
done

for $@, . , "$ file" , . , script 'myll.sh':

$ myll.sh "My Report.txt" file1 file2

"My Report.txt" : "" "Report.txt"

+3

$@ - , ( ). ($* - , , ).

, . , ls.

for file in "$@"; do
    if [ -f "$file" ]; then
        ls -l "$file"
    fi
done

: $@ ! $file - . $@ , file , -f ']'. .

, , , ls ( if), :

ls -l "$@"
+2

, :

for file in "$@"; do
  ls -l "$file"
done

, , :

for file in "$@"; do
  if [ ! -d "$file" ]; then
    ls -l "$file"
  fi
done
+1

bash , script, "$ *". :

for file in $*; do
  if [ -f $file ] ; then
    ls -l $file
  fi
done

( )

0
source

Source: https://habr.com/ru/post/1659458/


All Articles