Simple shell question and more if needed

This is a discussion on Simple shell question and more if needed within the C Programming forums, part of the General Programming Boards category; Code: echo "Enter Name of the first file:" read file1 echo $file1 if[ ! -f $file1] echo "Sorry, file does ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    103

    Simple shell question and more if needed

    Code:
    echo "Enter Name of the first file:"
    	read file1
    	echo $file1
    	if[ ! -f $file1]
    		echo "Sorry, file does not exist."
    		exit 1
    I was wondering if someone could tell me why when I pass in a file name (say "txt.txt') it errors out. (txt.txt is located in the same directory from where I execute my script.)


    Sorry if its in the wrong forum section.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    fixed it. But does anyone understand this error?
    "[!: command command not found

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    First thing: bash is whitespace sensitive. That means this is no good:
    Code:
    if[ ! -f $file1]
    You want:
    Code:
    if [ ! -f $file1 ]
    12345678901234567890
    Notice, a space at position 3,5,7, and 17. Those all MUST be there. If you use an editor with syntax highlighting for shell scripts (eg, vim), it will be slightly easier to notice that.

    But also your syntax is missing some things. Try this:
    Code:
    #!/bin/bash
    
    echo "Enter Name of the first file:"
    read file1
    echo $file1
    	if [ ! -f $file1 ]; then
    		echo "Sorry, file does not exist."
    		exit 1
    	fi
    Quote Originally Posted by TaiL View Post
    fixed it. But does anyone understand this error?
    "[!: command command not found
    Because your whitespace was wrong. The ! is right up against [, meaning [! was taken as a single token.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21