Command Separator

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Command Separator
# 1  
Old 11-24-2013
Command Separator

It shows error at 'else', how can I fix it?
if [ -x "$filename" ]; then # Note the space after the semicolon.
#+ ^^
echo "File $filename exists."; cp $filename $filename.bak
else # ^^
echo "File $filename not found."; touch $filename
fi; echo "File test complete."

# 2  
Old 11-24-2013
-x tests to see:
1. if file exists
2. AND the execute permission is set i.e., ls -l "$filename" -rwxr-xr-x

This is what touch will normally do:
$ ls -l foo.lis
ls: cannot access foo.lis: No such file or directory

jim@jim-HP ~
$ ls -l foo.lis
ls: cannot access foo.lis: No such file or directory

]jim@jim-HP ~
$ touch foo.lis

jim@jim-HP ~
$ ls -l foo.lis
-rw-r--r-- 1 jim None 0 Nov 24 06:03 foo.lis

- no "x"

use [ -f "$filename" ] instead to check for file existence.

PS: Your code behaves as you posted it - no error at the "else", you may have an unprintable character on the line.
cat -v

displays all characters.

Last edited by jim mcnamara; 11-24-2013 at 09:11 AM..
# 3  
Old 11-24-2013
From man page

-x ---> FILE exists and execute (or search) permission is granted
-f ---> FILE exists and is a regular file

As jim mcnamara already said
$ filename="/etc/hosts"
$ if [ -f "$filename" ];then echo "yes"; else echo "No";fi

$ if [ -x "$filename" ];then echo "yes"; else echo "No";fi

$ ls -ltr /etc/hosts
-rw-r--r-- 1 root root 218 Nov 11 08:46 /etc/hosts

# 4  
Old 11-24-2013
What error does it show at else?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting


Hello everybody, I'll get one more help I have a cabundle file that I need to separate into 2 parts, the first sequence and the second sequence, I thought of several things but I did not remember anything that could actually accomplish this separation and transform into 2 variables, first... (4 Replies)
Discussion started by: c0i0t3
4 Replies

2. Shell Programming and Scripting

Awk, with separator |

Friends have the following code that is correct. BEGIN { num_reg = 0 suma_iva=0 } { num_reg++ suma_iva=suma_iva+int(substr($0, 103,9)) } END{ printf ("%011d",suma_iva) } I have the following problem, I have to do just that but this time... (4 Replies)
Discussion started by: tricampeon81
4 Replies

3. Shell Programming and Scripting

Row separator

Hello All, I need help with the below, I would appreciate any tip. I have a file as below Input file Apple: Green Banana: Yellow Grapes: Black Apple: Red Banana: Green Grapes: Green Grapes: Brown Apple: Pale Red Banana: Greenish yellow Grapes: Brown Apple: Yellowish... (14 Replies)
Discussion started by: m6248m
14 Replies

4. UNIX for Advanced & Expert Users

File command return wrong filetype while file holds group separator char.

hi, I am trying to get the FileType using the File command. I have one file, which holds Group separator along with ASCII character. It's a Text file. But when I ran the File command the FileType is coming as "data". It should be "ASCII, Text file". Is the latest version of File... (6 Replies)
Discussion started by: Arpitak29
6 Replies

5. Shell Programming and Scripting

Need a separator string between fields in cut -c command

Hi All, I'm trying to view data using cut command for a fixed length file using the below command: cut -c 1-3,4-5 FALCON_PIS_00000000.dat I want to mention a separator say | (pipe) in between 1-3 and 4-5. Please let me know how to achieve this. Thanks in Advance, (3 Replies)
Discussion started by: HemaV
3 Replies

6. Shell Programming and Scripting

Using > as record separator

I have tried to use ">" as record separator, but it doesn't work. I have tried this: awk BEGIN{RS=">"}'{print $0}' input output: awk: BEGIN{RS=>}{print $0} awk: ^ syntax error awk BEGIN{RS="\>"}'{print $0}' input awk: BEGIN{RS=\>}{print $0} awk: ^ backslash not... (2 Replies)
Discussion started by: locoroco
2 Replies

7. Shell Programming and Scripting

Perl: Separator

Hello all, I'm trying to break down a file with the following format: entries dzdf daff enries dfln fnljfd . . .. . I'm reading this file, using "[" as my separator. It's working quite well, but I would like to be able to read the "[" character into my array as a valid... (4 Replies)
Discussion started by: Khoomfire
4 Replies

8. Shell Programming and Scripting

awk field separator or print command

Hello Experts, I am back, with another doubt. I am not sure what it relates to this time - awk or the print command actually. I'll explain the scenario: I have a huge file, and it has some traces(logs). In between those logs, there are statements with some SQL queries. All I want to do is... (4 Replies)
Discussion started by: hkansal
4 Replies

9. Shell Programming and Scripting

record separator

can anyone tell me any way to change record separator (default is new line). RS in nawk as not working. Thanks in advance. Regards Rochit (7 Replies)
Discussion started by: rochitsharma
7 Replies

10. UNIX for Dummies Questions & Answers

Help with unix separator

can some one give me a list of unix separtor(s) if one than just the separator please thank you. (2 Replies)
Discussion started by: Black mage2021
2 Replies
Login or Register to Ask a Question