LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   First steps script (https://www.linuxquestions.org/questions/linux-newbie-8/first-steps-script-820335/)

JoelSmit 07-17-2010 01:24 AM

First steps script
 
Hi guys,

I'm getting an error in line 3:

Quote:

#!/bin/sh
echo Enter File Name
set file=$<
if ( -w $file) then
cat $file
else
chmod ugo+w $file
fi
what am I doing wrong?
I've tried all kinds of combination in that line, none work.

Thanks,
Joel

sag47 07-17-2010 01:40 AM

Code:

if [ -w $file]; then
fixed

google conditional shell script
http://www.dreamsyssoft.com/sp_ifelse.jsp

edit:I misinterpreted what you wanted. I thought you meant command 3...
Anyway google user input shell script. You want the read command.
http://unixhelp.ed.ac.uk/scrpt/scrpt2.3.html

JoelSmit 07-17-2010 02:01 AM

Thanks sag,

it tells that there is something wrong in line 3
Quote:

set file=$<
but I can't figure out what...

thanks,
Joel

colucix 07-17-2010 02:22 AM

Actually you mixed syntax of C-shell and Bourne shell. $< is valid only in C-shells and it's replaced by the next line of standard input. Here are two versions of the same script, one in tcsh, the other in bash. Please, check the differences:
Code:

#!/bin/tcsh
echo "Enter File Name: "
set file=$<
if ( -w $file ) then
  cat $file
else
  chmod ugo+w $file
endif

Code:

#!/bin/bash
read -p "Enter File Name: " file
if [ -w $file ] ; then
  cat $file
else
  chmod ugo+w $file
fi

Hope this helps.

grail 07-17-2010 02:30 AM

Hi Joel

What are you expecting that line to do?

Edit: Beaten by the master again ... lol

JoelSmit 07-17-2010 03:17 AM

It works!

Thank you all!

I just had the wrong shell (like you said):hattip:

It doesn't work with /bin/sh but does with /bin/csh :doh:


All times are GMT -5. The time now is 10:48 AM.