Bash

From Wiki

(Difference between revisions)
Jump to: navigation, search
(functions problem in bash)
 
Line 1: Line 1:
-
=== Change the Shell Prompt ===
+
== Change the Shell Prompt ==
I found that particularly with Centos 5, that the default shell prompt for root was terrible. It did not show the pwd, user or host!
I found that particularly with Centos 5, that the default shell prompt for root was terrible. It did not show the pwd, user or host!
Line 17: Line 17:
Ref.: http://www.builderau.com.au/program/linux/soa/10-shortcuts-to-master-bash/0,339028299,339279043,00.htm
Ref.: http://www.builderau.com.au/program/linux/soa/10-shortcuts-to-master-bash/0,339028299,339279043,00.htm
 +
 +
== Function problem in Bash ==
 +
 +
I recently dist-upgraded a ubuntu server, and one bash script stopped working.
 +
==== Original =====
 +
#!/bin/bash
 +
 +
watch '
 +
function yesno(){
 +
case "$1" in
 +
        0)      echo "yes"
 +
        ;;
 +
        1)      echo "no"
 +
        ;;
 +
        *)      echo "error"
 +
        ;;
 +
esac
 +
}
 +
 +
==== Problem ====
 +
sh: Syntax error: "(" unexpected
 +
 +
==== Solution ====
 +
Remove the word function, and call it directly.
 +
 +
#!/bin/bash
 +
 +
watch '
 +
yesno(){
 +
case "$1" in
 +
        0)      echo "yes"
 +
        ;;
 +
        1)      echo "no"
 +
        ;;
 +
        *)      echo "error"
 +
        ;;
 +
esac
 +
}
 +
 +
Ref: http://stackoverflow.com/questions/6347119/bash-syntax-error

Current revision as of 14:37, 27 April 2012

Contents

Change the Shell Prompt

I found that particularly with Centos 5, that the default shell prompt for root was terrible. It did not show the pwd, user or host!

Before

-bash-3.1# ls

Showing no present working directory, no current username, no hostname


Syntax

PS1='\u@\h:\w\$ '

There are a few other things one can show also. Such as the time, date, FQHN etc. man bash for the full list.


After

root@volcano:/tmp# ls

Ref.: http://www.builderau.com.au/program/linux/soa/10-shortcuts-to-master-bash/0,339028299,339279043,00.htm

Function problem in Bash

I recently dist-upgraded a ubuntu server, and one bash script stopped working.

Original =

#!/bin/bash

watch '
function yesno(){
case "$1" in
       0)      echo "yes"
       ;;
       1)      echo "no"
       ;;
       *)      echo "error"
       ;;
esac
}

Problem

sh: Syntax error: "(" unexpected

Solution

Remove the word function, and call it directly.

#!/bin/bash

watch '
yesno(){
case "$1" in
       0)      echo "yes"
       ;;
       1)      echo "no"
       ;;
       *)      echo "error"
       ;;
esac
}

Ref: http://stackoverflow.com/questions/6347119/bash-syntax-error

Personal tools