How can I test if files given as an argument exist? - bash

I am making a bash script that you have to give 2 files or more as arguments.
I want to test if the given files exist. I'm using a while loop because I don't know how many files are given. The problem is that the if statement sees the $t as a number and not as the positional parameter $number. Does somebody have a solution?
t=1
max=$#
while [ $t -le $max ]; do
if [ ! -f $t ]; then
echo "findmagic.sh: $t is not a regular file"
echo "Usage: findmagic.sh file file ...."
exit
fi
t=`expr $t + 1`
done

You can do it with the bash Special parameter # in this way:
script_name=${0##*/}
for t in "$#"; do
if [ ! -f "$t" ]; then
echo "$script_name: $t is not a regular file"
echo "Usage: $script_name file file ...."
exit 1
fi
done
With "$#" you are expanding the positional parameters, starting from one as separate words (your arguments).
Besides, remember to provide a meaningful exit status (e.g. exit 1 instead of exit alone). If not provided, the exit status is that of the last command executed (echo in your case, which succes, so you're exiting with 0).
And for last, instead of write the script name (findmagic.sh in your case), you can set a variable at the beginning in your script:
script_name=${0##*/}
and then use $script_name when necessary. In this way you don't need to update your script if it changes its name.

Related

Bash while() loop :: Does this process cmd line arguments?

I'm a Bash newbie, and I'm puzzling through a Bash script where I see this:
while [ $# -ge 2 ]
if [ "x$1" = "x-a" ]; then
echo "STR_A = $2" >>tmpFile.txt
...do more stuff...
elif [ "x$1" = "x-b" ]; then
echo "STR_B = $2" >>tmpFile.txt
...do more stuff...
else
usage
done
The script takes in four (or five?) command line arguments, and that usage function is:
usage() {
echo "usage: myscript -a STR_A | -b STR_B" 1>&2
exit 1
}
So suppose I ran the script like this:
me#ubuntu1:~$./myscript -A apple -B banana
I'm guessing that this code processes the script's command line arguments. I think that the outer while() loop steps through the command line arguments after argument 1, which would be myscript. The inner if() statements check to see an -a or -b flag is used to supply arguments, and then records the text string that follows in tmpFile.txt. Anything outside of those parameters is rejected and the script exits.
A lot of these assumptions rest on the bet that the outer while() loop...
while [ $# -ge 2 ]
...means "parse the BASH argv[] after the first argument", (to put this in C terms.) If that's not a correct assumption, then I have no idea what's going on here. Any feedback is appreciated, thank you!
Some code explanation.
while [ $# -ge 2 ]
There is a missing do for the loop.
This should loop forever if there are two or more arguments, unless shift is used. If there are less than two arguments, the loop does not even start.
if [ "x$1" = "x-a" ]; then
In distant past, it was common to prevent empty strings by adding an extra letter. Nowadays you would if [ "$1" = "-a" ]; then.
else
usage
Note that the usage is called from within the loop. So, if I would call the script as myscript -a, I would not get a usage message. On the other hand, if I would myscript bla bla, I would get an endless stream of error messages, which is probably not what you want.
I would seriously edit the script; determine whether the while is indeed a loop-forever or whether it is used instead of an if, and try the getops for argument parsing.
This doesn't help understand the code you're reading, but I do option parsing like this:
# inititialize vars, not strictly required in this case
a=''
b=''
# process options
while getopts "ha:b:" opt; do
case $opt in
a) a=$OPTARG ;;
b) b=$OPTARG ;;
*) usage ;;
esac
done
# shift after the processing
shift $((OPTIND - 1))
# look for error conditions
if [[ -n $a && -n $b ]]; then
echo "only one of -a or -b should be given" >&2
exit 1
fi
getopts is a bash builtin

Shell script with absolute path and control errors

I was doing this little script in which the first argument must be a path to an existing directory and the second any other thing.
Each object in the path indicated in the first argument must be renamed so that the new
name is the original that was added as a prefix to the character string passed as the second argument. Example, for the string "hello", the object OBJECT1 is renamed hello.OBJECT1 and so on
Additionally, if an object with the new name is already present, a message is shown by a standard error output and the operation is not carried out continuing with the next object.
I have the following done:
#! /bin/bash
if [ "$#" != 2 ]; then
exit 1
else
echo "$2"
if [ -d "$1" ]; then
echo "directory"
for i in $(ls "$1")
do
for j in $(ls "$1")
do
echo "$i"
if [ "$j" = "$2"."$i" ]; then
exit 1
else
mv -v "$i" "$2"."$i"
echo "$2"."$i"
fi
done
done
else
echo "no"
fi
fi
I am having problems if I run the script from another file other than the one I want to do it, for example if I am in /home/pp and I want the changes to be made in /home/pp/rr, since that is the only way It does in the current.
I tried to change the ls to catch the whole route with
ls -R | sed "s;^;pwd;" but the route catches me badly.
Using find you can't because it puts me in front of the path and doesn't leave the file
Then another question, to verify that that object that is going to create new is not inside, when doing it with two for I get bash errors for all files and not just for coincidences
I'm starting with this scripting, so it has to be a very simple solution thing
An obvious answer to your question would be to put a cd "$2 in the script to make it work. However, there are some opportunities in this script for improvement.
#! /bin/bash
if [ "$#" != 2 ]; then
You might put an error message here, for example, echo "Usage: $0 dir prefix" or even a more elaborate help text.
exit 1
else
echo $2
Please quote, as in echo "$2".
if [ -d $1 ]; then
Here, the quotes are important. Suppose that your directory name has a space in it; then this if would fail with bash: [: a: binary operator expected. So, put quotes around the $1: if [ -d "$1" ]; then
echo "directory"
This is where you could insert the cd "$1".
for i in $(ls $1)
do
It is almost always a bad idea to parse the output of ls. Once again, this for-loop will fail if a file name has a space in it. A possible improvement would be for i in "$1"/* ; do.
for j in $(ls $1)
do
echo $i
if [ $j = $2.$i ]; then
exit 1
else
The logic of this section seems to be: if a file with the prefix exists, then exit instead of overwriting. It is always a good idea to tell why the script fails; an echo before the exit 1 will be helpful.
The question is why you use the second loop? a simple if [ -f "$2.$i ] ; then would do the same, but without the second loop. And it will therefore be faster.
mv -v $i $2.$i
echo $2.$i
Once again: use quotes!
fi
done
done
else
echo "no"
fi
fi
So, with all the remarks, you should be able to improve your script. As tripleee said in his comment, running shellcheck would have provided you with most of the comment above. But he also mentioned basename, which would be useful here.
With all that, this is how I would do it. Some changes you will probably only appreciate in a few months time when you need some changes to the script and try to remember what the logic was that you had in the past.
#!/bin/bash
if [ "$#" != 2 ]; then
echo "Usage: $0 directory prefix" >&2
echo "Put a prefix to all the files in a directory." >&2
exit 1
else
directory="$1"
prefix="$2"
if [ -d "$directory" ]; then
for f in "$directory"/* ; do
base=$(basename "$f")
if [ -f "Sdirectory/$prefix.$base" ] ; then
echo "This would overwrite $prefix.$base; exiting" >&2
exit 1
else
mv -v "$directory/$base" "$directory/$prefix.$base"
fi
done
else
echo "$directory is not a directory" >&2
fi
fi

How to use contents of text file as input to shell script?

I'm tasked with writing a shell script that takes in a string of 6 letters and numbers and checks if they meet a certain criteria.
This is that script
FILE=$1
var=${#FILE}
if [ $var -gt 6 ] || [ $var -lt 6 ]
then
#echo $FILE "is not a valid NSID"
exit 1
else if ( echo $1 | egrep -q '^[A-Za-z]{3}\d{3}$' )
then
#echo $1 "is a valid NSID"
exit 0
else
#echo $1 "is not a valid NSID"
exit 1
fi
fi
It works. so that isn't where the problem is.
What I am trying to do is use a "wrapper" script to gather potential valid NSID's from a text file and call that script on the list of inputs. so that if I call that script within my wrapper script it will step through the text file I have given my wrapper and check if each line is valid.
FILE=$1
YES= more $FILE
if ( exec /path/to/file/is_nsid.sh $YES -eq 0 )
then
echo $FILE "is a valid NSID"
else
echo $FILE "is not a valid NSID"
fi
so if I called it with a text file called file1.txt which contained
yes123
yess12
ye1243
it would output whether each was valid or not.
The line
YES= more $FILE
Sets YES in the environment passed to the command more $FILE. That's probably not what you intended.
The line
if ( exec /path/to/file/is_nsid.sh $YES -eq 0 )
starts a subshell to execute exec /path/to/file/is_nsid.sh $YES -eq 0. (That's what the parentheses do.) exec then replaces the subshell with a process which executes
/path/to/file/is_nsid.sh $YES -eq 0
which in turn runs the script at is_nsid.sh, passing it two or three command line arguments:
the value of $YES. This could be several arguments if the value of the shell variable includes whitespace or a glob symbol, but in this case it is more likely to be nothing since $YES has not been defined.
-eq
0
Since your script only examines its first argument, that's probably equivalent to
/path/to/file/is_nsid.sh -eq
That will, presumably, terminate with a failure status code, and since the subshell has been replaced with the script execution, that will also be the return status of the subshell. (Without exec, there would be essentially no difference; the subshell's return status is that of the last command executed in the subshell. Without either the parentheses or the exec, the result would also be the same. So you could have just written if /path/to/file/is_nsid.sh $YES -eq 0 and it would produce the same incorrect result.)
What you presumably wanted to do was to read each line in the file whose name is passed as the first command-line argument to the script. You could do that as follows:
while read -r line; do
if /path/to/file/is_nsid.sh "$line"; then
echo "$line is a valid NSID"
else
echo "$line is not a valid NSID"
fi
done < "$1"
You could simplify your is_nsid script considerably. The following is equivalent:
[ $#1 -eq 6 ] && echo "$1" | egrep -q '^[A-Za-z]{3}\d{3}$'
Note that \d is a Gnu extension to egrep and should not be relied on in portable code (which I assume this is trying to be). You should use [0-9] or [[:digit:]] instead.
The length check is actually unnecessary since the regex can only match six-character lines. Personally, I'd leave it out and just use
echo "$1" | egrep -q '^[[:alpha:]]{3}[[:digit:]]{3}$'
I removed all the unnecessary if statements. If I had left them in, I would have changed else if ... then ... fi to simply elif ... then ... to avoid unnecessary if nesting.

For loop to find out if directory exists in unix

I want to use a script that checks whether a list of directories exists or not and at the same time it should print some custom message that I am sending.
For example:
I have a script that validates if directory exists or not:
**check.sh**
for i in $*
if [ -d "$i" ]; then
echo Found <msg-i> directory.
else
echo <msg-i> directory not found.
Now I want to call this script like this:
./check.sh $DIR1 msg1 $Dir2 msg2 $Dir3 msg3
So if DIR1 doesn't exist then I want to display message as "msg1 directory not found", similarly for DIR2 I want to show "msg2 directory not found". Here msg1 and msg2 are something I want to pass as string. How to achieve this? I am using bash shell.
Try this:
while [ -n "$1" ]
do
dir="$1"
msg="$2"
if [ -d "$dir" ]; then
echo "$msg dir FOUND"
else
echo "$msg dir NOT FOUND"
fi
shift 2
done
shift <n> command simply shifts left positional parameters passed to the script of n positions.
For example if you call a script with:
./myscript 1 2 3 4
$1 is "1" and $2 is "2"
but if you shift 2 then $1 is "3" and $2 is "4".
In this way the loop consumes 2 parameters per cycle until $1 parameter is an empty string ( -n "$1").
while condition can be written more elegantly as:
while (( $# ))
obtaining the same result.
You can also check for the second parameter (while [ -n "$2" ]) but this changes the behavior when user provides an odd number of parameters:
in the first case last directory will be checked but you'll have a strange message because $msg il empty
il the second case you'll not have strange messages, but last directory will silently not be checked
Better test parameters at the beginning:
if (( $# % 2 ))
then
echo "Provide an even number of parameters"
exit 1
fi
Chepner Says:
The while condition can simply be (( $# )) (test if the number of positional parameters is non-zero).
Chaitanya Says:
Hi Chepner, thanks for providing alternate solution, can you please tell me how the while condition should actually look like in order to use $# , I tried different ways but it is not working for me.
Here's a quick sample:
while (( $# ))
do
dir=$1
msg=$2
shift 2
[...]
done
The while (( $# )) will be true as long as there are any command line arguments. Doing the shift twice removes arguments from the list. When no more arguments, the while loop ends.
#Zac has the correct answer.
One tip for the message: use a printf format string:
./check.sh dir1 "can't locate %s directory"
and in the script:
if [[ ! -d "$dir" ]]; then
printf "$msg" "$dir"

Bourne Shell Programming: handling argument errors

I am writing a shell program that takes in three arguments:
an integer to determine the function of the program
a file used by the program
The command is of the form myProgram num file. However, I want the program to output an error if the command only has 0, 1, or more than 2 arguments. That is, if I type "myProgram", "myProgram num", or "myProgram num file anotherWord", an error will be printed to the screen. Does anyone know how I could implement this into my existing code?
In bash, when using integers, the (( )) is more intuitive :
#!/bin/bash
if (($# < 2)); then
printf >&2 "There's less than 2 arguments\n"
exit 1
fi
if (($# > 2)); then
printf >&2 "There's more than 2 arguments\n"
exit 1
fi
if ! (($1)); then
printf >&2 "First argument must be a positive integer\n"
exit 1
fi
if [[ ! -f "$2" ]]; then
printf >&2 "Second argument must be an exited file\n"
exit 1
fi
# -->the rest of the script here<--
Moreover, to respect the best practice & proper coding, when printing an error, it must be so STDERR like I do with printf >&2
The built-in variable $# contains the number of arguments that were passed to the script. You use this to check if there are enough arguments like so:
#!/bin/bash
if [ $# -ne 2 ]; then
echo "Usage: myProgram num file" >&2
exit 1
fi
# The rest of your script.
Use shell built in $# to determine the number of arguments passed into your script. Your program name is not counted.
if you are using bash, then you can approach it like this:
#!/bin/bash
if [ $# -lt 2 ] || [ $# -gt 3 ]
then
echo "You did not provide the correct parameters"
echo "Usage: blah blah blah"
fi
This is a very simple check. You can also check man pages for getopt processing which is much more powerful when evaluating command line parameters.
be well

Resources