Shell nested ifs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell nested ifs
# 1  
Old 10-01-2008
Question Shell nested ifs

Hi can someone tell me whats wrong with the following:

Code:
#!/bin/sh
file1=$1
file2=$2
if [ $# = 2 && -e $file1]
then
    if [ -e $file1 ]
    then
        echo "File 1 is" $file1
        echo "File 2 is" $file2
        cp $file1 $file2
        echo "Copy complete!"
    else
        echo "ERROR: File does not exist!"
    fi
else
    echo "ERROR: Not enough command line arguments!"
fi

I get the following output:
Code:
philip@philip-laptop:~/Desktop$ sh copy.sh test.java test2.java
[: 17: missing ]
ERROR: Not enough command line arguments!

# 2  
Old 10-01-2008
your code
Code:
if [ $# = 2 && -e $file1]

try this
Code:
if [ $# -eq "2" && -e $file1 ]

space was missing after file name
# 3  
Old 10-01-2008
I want to use this command:
ls -l some_file

in my shell script

But I want to then determine if the file has execute permission for the file owner.

How could i do that?
Is there a way to then after it did the command above, to select a certain value from it eg:
-rwSr-Sr-- 1 philip philip 126 2008-10-01 17:15 test.java

Could i select the 4th character?

Last edited by philmetz; 10-01-2008 at 08:42 AM..
# 4  
Old 10-01-2008
if [ `ls -l som_file | cut -c4` = 'x' ]
then
echo "Owner has executable permission"
else
echo "Owner doesn't hace executable permission"
fi
justsam
# 5  
Old 10-01-2008
So for what you said the cut -c4 means select character 4 from the line abobe?
# 6  
Old 10-01-2008
Hi Philmetz,

Yes. cut -c4 will get you the 4th character.Smilie
justsam
# 7  
Old 10-01-2008
But the proper way to test that is with test -x some_file

Actually in your original code you want

Code:
if [ $# = 2 -a -e $file1 ]


Last edited by era; 10-01-2008 at 09:11 AM.. Reason: Note -a pro &&
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

The Shell lost the inverted comma in a nested ssh command

Hi, i want use this Comand for my psql request sh ssh -o StrictHostKeyChecking=no rootatemailaddress.de sudo psql -U postgres -c "select pg_terminate_backend(pid) from pg_stat_activity where datnam=\'$DB\';"'" but the shell lost the inverted comma for datnam=\'$DB\'. The request deliver... (2 Replies)
Discussion started by: peterpane007
2 Replies

2. Homework & Coursework Questions

Alternative solution to nested loops in shell programming

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Hi, The problem statement is: I am trying to read line by line from a flat file by using a while loop. The... (9 Replies)
Discussion started by: Sandeep Pattnai
9 Replies

3. Shell Programming and Scripting

Using shell command need to parse multiple nested tag value of a XML file

I have this XML file - <gp> <mms>1110012</mms> <tg>988</tg> <mm>LongTime</mm> <lv> <lkid>StartEle=ONE, Desti = Motion</lkid> <kk>12</kk> </lv> <lv> <lkid>StartEle=ONE, Source = Velocity</lkid> <kk>2</kk> </lv> <lv> ... (3 Replies)
Discussion started by: NeedASolution
3 Replies

4. Shell Programming and Scripting

Nested ifs

hi I keep getting an error with this nested if statement and am getting the error unexpected end of file, can anyone help me as to why this wont execute? #!/bin/bash #script to check wether the -i -v statements run correctly removeFile () { mv $1 $HOME/deleted }... (3 Replies)
Discussion started by: somersetdan
3 Replies

5. Shell Programming and Scripting

nested logical expression in bash shell

Please tell me how to nest logical expressions in bash. I would like to nest logical expressions for arguments of the "test" command on bash. The following pseudo-code shows my intention. // pseudo code if (exp1 AND (exp2 OR exp3)) { Output true; } else { Output false; } ... (11 Replies)
Discussion started by: LessNux
11 Replies

6. UNIX for Dummies Questions & Answers

How to use nested ifs in unix?

how to use nested ifs in unix (1 Reply)
Discussion started by: pratima.kumari
1 Replies

7. Shell Programming and Scripting

Nested SQL queries within Shell script

Hi, Would someone know if I can fire nested sql queries in a shell script? Basically what I am trying to do is as follows: my_sql=$(sqlplus -s /nolog<<EOF|sed -e "s/Connected. *//g" connect... (2 Replies)
Discussion started by: shrutihardas
2 Replies

8. Shell Programming and Scripting

Shell Integer with nested foreach

I am scripting in tcsh and here is what I currently have: foreach group (g1 g2 g3 g4) set ppl = `cat $group.file.with.list.of.ppl.in.row.format` set label = 1 @ label += 1 foreach ppls ($ppl) echo $label >> file end end ... (0 Replies)
Discussion started by: bonesy
0 Replies

9. Shell Programming and Scripting

Enviornment Variable in B shell (I call it nested variable)

#!/bin/sh APP_ROOT_MODE1=/opt/app1.0 APP_ROOT_MODE2=/opt/app2.0 APP_ROOT=${APP_ROOT_${APP_MODE}} # enviornment variable APP_MODE will be exported in the terminal where # we run the applciation, its value is string - MODE1 or MODE2 # My intension is: # when export APP_MODE=MODE1... (4 Replies)
Discussion started by: princelinux
4 Replies

10. Shell Programming and Scripting

Trouble with Nested Ifs

What I thought was going to be very simple has turned out to be really lame, and so I come to you for help. mountedon=`df -k /synctest | awk 'NR == 2 {print $1}'` if then # mount /pupcl06 mountedon=`df -k /synctest | awk 'NR == 2 {print $1}'` if then mailx -s... (2 Replies)
Discussion started by: ProFiction
2 Replies
Login or Register to Ask a Question