Here's my script. It takes only 4 positional parameters. The output should be all the positional parameters, the number of characters that each one has and the first character of each one.
#!/bin/bash
rm -r param.out
declare -i cont
cont=$#
if [ $cont -eq 4 ]
then
echo "Positional parameters" >> param.out
echo $* >> param.out
echo "Number of characters of each positional parameter" >> param.out
echo -n $1 | wc -c >> param.out
echo -n $2 | wc -c >> param.out
echo -n $3 | wc -c >> param.out
echo -n $4 | wc -c >> param.out
echo "First character of each parameter" >> param.out
echo $1 | head -c 1 >> param.out
echo $2 | head -c 1 >> param.out
echo $3 | head -c 1 >> param.out
echo $4 | head -c 1 >> param.out
else
echo "Error"
exit
fi
With an input of ./file 12 23 34 456
The obtained file is the following one:
Positional parameters
12 23 34 456
Number of characters of each positional parameter
2
2
2
3
First character of each parameter
1234
The ideal would be obtaining the outputs as the first one (12 23 34 456)
PD. I know this could be done using a for
/while
in order to avoid repetitions of echo but I'm learning bash :/