What Does "$1/*" Mean in "For File in $1/*"

What does $1/* mean in for file in $1/*

It's the glob of the first argument considered as a directory

In bash scripts the arguments to a file are passed into the script as $0 ( which is the script name ), then $1, $2, $3 ... To access all of them you either use their label or you use one of the group constructs. For group constructs there are $* and $@. ($* considers all of the arguments as one block where as $@ considers them delimited by $IFS)

What is the significance of . $1 in the below script

$1 is the first argument. The script supposes the first argument will be a file (whence the [ -f $1]).

suppose in the file variables and/or functions are declared, running

. $1

will make those variables and functions available for use in your script. You are sort of "including" the file $1 in you script.

What the meaning of $1 before the Filename


What I want to know is what it will yield when they are using like $1/proc-self-mountstats.$2. Let's suppose $1 = 123 and $2 = 100. You mean it will become 123/proc-self-mountstats.100?

Yes, that's right. The expanded values are joined together with the literal parts to form one big string.

Location=$1? what does it mean?

$1 refers to the first argument of the bash file. In this case, you can pass your directory path by issuing the following command:

# ./test2.sh /path/of/your/directory

#!/bin/bash

LOCATION=$1 #first argument of the script
FILECOUNT=0
DIRCOUNT=0

if [ "$#" -lt "1" ] #if the number of argument(s) ($#) is less than 1
then
echo "Usage: ./test2.sh <directory>"
exit 0
fi

You can read this article for more information about parameter passing.
Hope it helps.

What do $#, $1 and $2 mean?

The $# refers to the number of parameters received at run time, not a specific parameter. $1 gets replaced by whatever was in location 1 on the command line when the script was executed.

what is the difference between $1 and $1 in bash script?


$ var="two words"
$ function num_args() { echo "${#}"; }
$ num_args $var
2
$ num_args "$var"
1

The difference between $A and "$A" is how word breaks are treated with respect to passing arguments to programs and functions.

Imagine script that works on files (let's say moves them around):

$ cat my-move
#! /bin/sh
# my-move

src=${1}
dst=${2}

# ... do some fancy business logic here

mv ${src} ${dst}
$ my-move "some file" other/path

As the code stands now (no quotes) this script is broken as it will not handle file paths with spaces in them correctly.

(Following thanks to @CharlesDuffy)

Additionally quoting matters when handling glob patterns:

$ var='*'
$ num_args "$var"
1
$ num_args $var
60

Or:

$ shopt -s failglob
$ var='[name]-with-brackets'
$ echo $var
bash: no match: [name]-with-brackets
$ echo "$var"
[name]-with-brackets

what does -d value in an if expression in shell?

You can use it to determine if a directory exist like

if [ -d "$DIR" ]; then
# code
fi


Related Topics



Leave a reply



Submit