0

how to add output of user input? Use nano editor to create script from the scratch which will output the text provided as an argument, the name of the editor used to create the file and whether or not the text is longer then 10 symbols on the same line divided by semicolons and surrounded by double quotes (e.g. somescript this is text should output "this is text"; "nano"; "more then 10 symbols").

#!/bin/bash

if [ `echo "$1" | awk '{print length}'` -gt 10 ]

then

    echo '"$1";"nano";"more than 10 symbols"'

else

    echo '"$1";"nano";'

fi

here you need to add the output of the text entered by the user (it is necessary to output what you pass to the script as an argument)

Trap29
  • 11
  • 2
  • 1
    Possible duplicate of [How to write a bash script that takes optional input arguments?](https://stackoverflow.com/q/9332802/608639), [How to get the nth positional argument in bash?](https://stackoverflow.com/q/1497811/608639), [Is there a way to avoid positional arguments in bash?](https://stackoverflow.com/q/12128296/608639), [What are the special dollar sign shell variables?](https://stackoverflow.com/q/5163144/608639), etc. – jww Jul 17 '19 at 13:45
  • Please check answer. – Paul Dawson Jul 22 '19 at 12:07

1 Answers1

1

This should do the trick:

#!/bin/bash

if [ `echo "$1" | awk '{print length}'` -gt 10 ]

then

    echo  '"'$1'"'";"'"'nano'"'";"'"'more than 10 symbols'"'


else

echo '"'$1'"'";"'"'nano'"'

fi

./testscript 1234567890
 "1234567890";"nano"

./testscript 12345678910
 "12345678910";"nano";"more than 10 symbols"
Paul Dawson
  • 1,332
  • 14
  • 27
  • That leaves `$1` as an unquoted expansion. You could do something like `printf '"%s";"%s";"%s"\n' "$1" 'nano' 'more than 10 symbols'` instead. – Benjamin W. Jul 17 '19 at 14:30
  • Try calling this with `'*'` as the argument, and you'll see why the unquoted expansion is a problem. – Benjamin W. Jul 17 '19 at 14:38
  • `./testscript 123456*8910 = "123456*8910";"nano";"more than 10 symbols" ` – Paul Dawson Jul 17 '19 at 14:40
  • Suppose if there is an instance where only input `*` then there's a problem. – Paul Dawson Jul 17 '19 at 14:42
  • The more general (and avoidable) problem is `$1` containing any shell metacharacter. There's no reason you can't quote `$1`. If you want to use `echo`, you could to `echo "\"$1\";\"nano\";\"more than 10 symbols\""`, or `echo "\"$1\""';"nano";"more than 10 symbols"'` – Benjamin W. Jul 17 '19 at 15:20