-1

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 :/

Srini V
  • 11,045
  • 14
  • 66
  • 89
Pol
  • 1

1 Answers1

0

echo -n $1 | wc -c >> param.out

wc appends a new line to it's output, which has to be removed or replaced by a space in order to the get number of characters of each parameter on a single line. You could do that with sed:

echo -n $1 | wc -c | sed 's/\n/ /' >> param.out

echo $1 | head -c 1 >> param.out

Use echo to append a space to the first character of a parameter:

echo "$(echo $1 | head -c 1) " >> param.out
C0DY
  • 191
  • 1
  • 7