0
#!/bin/sh


read vstup

if [ -f "$vstup" ]
    then
    cat $vstup
else if [ $vstup = "-"]
        then
         while [ $stadvstup != "q"]
            do
            read $stadvstup >> temp.txt
            done
        cat temp.txt
        rm temp.txt
fi
fi

I would like to make script, which allows user to input name of the file or standard input. If user type name of the file, it will output content of the file, if user type "-", it will allow user to make an input and then it will output. I have used following code, please would someone give me a hint, what is wrong?

2
  • Missing spaces before the ] (to start) Commented May 4, 2016 at 9:38
  • 2
    Shellcheck.net - for syntax Commented May 4, 2016 at 9:52

2 Answers 2

1

You would have to do something like this,

#!/bin/sh

file="temp.txt"
read -r vstup

if [ -f "$vstup" ]
then
     cat "$vstup"
elif [ "$vstup" = "-" ]
then
     while read line
     do
         # break if the line is empty
         [ -z "$line" ] && break
              echo "$line" >> "$file"
     done
   cat $file
   rm $file
fi
0
#!/bin/sh

read vstup

if [ -z "$vstup" ] ; then
    :
elif [ "$vstup" = "-" ] ; then
    vstup=''
elif [ ! -f "$vstup" ] ; then
    printf "%s is not a regular file\n" "$vstup"
    exit 1
fi

cat $vstup

cat defaults to copying stdin to stdout if not given a filename (i.e. if $vstup is empty). It will keep doing so until user types an EOF character (Ctrl-D)

Entering a blank line is treated the same as entering - for vstup.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.