how can i detect user input is coming from pipe line or with parameters? (as using "if else")
example:
with pipeline
$ cat input_file | ./example.sh
hello
world
with parameters
$ ./example.sh "hello" "world"
hello
world
my false code:
I write url slug shell script. I have a one function for url parsing in script. I use that function for pipe cat a | ./example.sh
or user input ./example.sh "hello" "world"
. My code is correct but I don't understand how detect and check user input is pipe or parameters.
sorry my english
#!/bin/bash
# define replacements
declare -a repls=(
"Ğg"
"ğg"
"Çc"
"çc"
"Şs"
"şs"
"Üu"
"üu"
"Öo"
"öo"
"İi"
"ıi"
" -"
"--"
)
function slug() {
slug=""
for (( i=0; i<${#arg}; i++ ))
do
char="${arg:$i:1}"
ascii=$(printf "%d" "'$char")
# if alphanumeric
# locale encoding should be UTF-8 for this values to work
if [[ ( $ascii -ge 48 && $ascii -le 57 ) || # numbers
( $ascii -ge 65 && $ascii -le 90 ) || # uppercase
( $ascii -ge 97 && $ascii -le 122 ) ]]; then # lowercase
slug="$slug$char"
else
for (( j=0; j < ${#repls[@]}; j++ ))
do
from=${repls[$j]:0:1}
to=${repls[$j]:1:1}
if [[ $char == $from ]]; then
slug="$slug$to"
break
fi
done
fi
done
if [[ $slug == "" ]]; then
echo "words should contain at least one valid character"
exit 1
fi
echo $slug | awk '{print tolower($0)}'
}
#FOR PARAMETERS
for arg in "$@"
do
slug;
done
##FOR READ PIPE
[[ -z "$@" ]] && while read arg;
do
slug;
done
[[ -z "$@" ]]
for this test.[ -z "$@" ]
(insh
) would fail if there are multiple command line arguments. Better to use[ "$#" -eq 0 ]
.