15

This is a very simple script

#!/usr/local/bin/bash
set -e
if [ "$#" -lt 1 ]
then
echo  "Please insert at least one argument"
exit
else
echo -e "\c"
fi


if [ -h  "$1" ]
then
         echo "$1 is a symbolic link"
else    
         echo "$1 in not a symbolic link"
fi

~
My question is: how to change the script for recognize more than 1 argument? I have 4 files, and I want the script to return

$1 is a symbolic link
$2 is not a symbolic link
$3 is not a symbolic link

etc

How could I do this?

5 Answers 5

12

Using the same structure as your original script, you just need to iterate over the $@ array (that's the list of arguments given in the command line):

#!/usr/local/bin/bash
set -e
if [ "$#" -lt 1 ]
then
echo  "Please insert at least one argument"
exit
else
echo -e "\c"
fi


for file in "$@"
do
    if [ -h  "$file" ]
    then
         echo "$file is a symbolic link"
    else    
         echo "$file is not a symbolic link"
    fi
done

A simplified version of the same thing would be:

#!/usr/bin/env bash
[ "$#" -lt 1 ] && printf "Please give at least one argument\n" && exit 
for file 
do
    [ -h "$file" ] && printf "%s is a symbolic link\n" "$file" || 
        printf "%s is not a symbolic link\n" "$file"
done
8

No one mentioned shift?

if [ x = "x$1" ] ; then
    echo need at least one file
    exit 1
fi

while [ x != "x$1" ] ; do
  if [ -h  "$1" ]; then
    echo "$1 is a symbolic link"
  else    
    echo "$1 is not a symbolic link"
  fi
  shift
done
5

You can use a for loop to process all files passed to script:

for f do
  if [ -h  "$f" ]; then
    printf "%s is a symbolic link\n" "$f"
  else    
    printf "%s is not a symbolic link\n" "$f"
  fi
done
3

Another shift:

: "${1?USAGE: "$0" files...}"
while   [ "$#" -gt 0 ]
do      [ -h "$1" ]
        printf "'%s' is %.$((!$?*4))s%s\n" \
               "$1" "not " "a symbolic link." 
shift;  done
2
  • Remove that !
    – MortezaE
    Commented Oct 3, 2022 at 3:28
  • @MortezaE why should i? the !bang inverts the $? return value from 0 to 1 or non-zero to zero and therefore printf allocates the correct amount of space for the output depending on whether or not the previous command returned correctly. if your bash has a problem with it try setting your shells !bang command line handling to something more portably sane.
    – mikeserv
    Commented Sep 24, 2023 at 2:39
1

The way I read it was how to get my personal script to take multiple arguments the way for example wget can perform "wget url1 url2 url3"

paste your entire script inside the following where it says "lines from my script" and save this file as your new script.:

ARGUMENTS=$(echo "$@"| tr " " "\n") ; while read A; do "lines from my script";done < "$ARGUMENTS"

Arguments to a script are stored in the variable $@. This little script wraps around your script and runs it multiple times, each time passing the next argument to it. If your current script refers to $1 you will have to change that to $A.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .