translate a short csh script to bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting translate a short csh script to bash
# 1  
Old 09-12-2010
BASH help: translate a short csh script to bash

Hi,

I have a csh:

Code:
  set NODES = `cat $HOST_FILE`
  set NODELIST = $TMPDIR/namd2.nodelist
  echo group main >! $NODELIST
  foreach node ( $NODES )
    echo host $node >> $NODELIST
  end
  @ NUMPROCS = 2 * $#NODES

I am very frustrated to translate it to bash:

Code:
NODES = `cat $HOST_FILE`
NODELIST = $TMPDIR/namd2.nodelist
echo group main > NODELIST1
for  node in $NODES
do
    echo host $node >> $NODELIST
done
NUMPROCS = 2 * $#NODES

The script basically capture each line of file $NODELIST and add 'host' to the beginning of each line.
Can some please help to fix my translation?
Your time is greatly appreciated!
Todd

Last edited by Scott; 09-12-2010 at 08:16 AM.. Reason: Added code tags
# 2  
Old 09-12-2010
PLease, put code tags for your next post.
Code:
# NODES = `cat $HOST_FILE`
NODES=$(cat $HOST_FILE) # no spaces around '=' for a value affectation
# NODELIST = $TMPDIR/namd2.nodelist
NODELIST=$TMPDIR/namd2.nodelist # same as above
echo group main > NODELIST1
for node in $NODES
do
   echo host $node >> $NODELIST
done
# NUMPROCS = 2 * $#NODES
NUMPROCS=$((2 * ${#NODES})) # $(( )) is the construction for arithmetic evaluation

# 3  
Old 09-12-2010
If you're nodes are separated by lines, a safer method would be:
Code:
HOST_FILE=<your path to host file>

NODES=()

while read; do
	NODES[${#NODES[@]}]=$REPLY
done < "$HOST_FILE"

NODESLIST=$TMPDIR/namd2.nodelist

echo "group main" > "$NODESLIST"

for NODE in "${NODES[@]}"; done
	echo "host $NODE" >> "$NODESLIST"
done

(( NUMPROCS = 2 * ${#NODES[@]} ))

And a simpler version could also be like this:
Code:
HOST_FILE=<your path to host file>
NODESLIST=$TMPDIR/namd2.nodelist
NODESCOUNT=0

{
	echo "group main"

	while read; do
		echo "nodes $REPLY"

		(( NODESCOUNT++ ))
	done < "$HOST_FILE"
} > "$NODESLIST"

(( NUMPROCS = 2 * NODESCOUNT ))

It can also be further simplified if you can use readarray or mapfile from bash 4.0+.
# 4  
Old 09-12-2010
frans and konsolebox,
They worked.

Thanks a lot! Appreciate your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Translate bash mathematical calculation to awk

this code below is very useful in calculating mean and quartiles. however, i would really like to translate it to awk without having to write to any external file: #!/bin/sh filename="tmp.txt" sort -n $1 >$filename rows=`wc -l $filename|cut -d' ' -f1` q2=`echo "($rows+1)/2" |bc` ... (3 Replies)
Discussion started by: SkySmart
3 Replies

2. Shell Programming and Scripting

Csh , how to set var value into new var, in short string concatenation

i try to find way to make string concatenation in csh ( sorry this is what i have ) so i found out i can't do : set string_buff = "" foreach line("`cat $source_dir/$f`") $string_buff = string_buff $line end how can i do string concatenation? (1 Reply)
Discussion started by: umen
1 Replies

3. Shell Programming and Scripting

Converting awk script from bash to csh

I have the following script set up and working properly in bash. It basically copies a set of lines which match "AS1100002" from one file and replaces the same lines in another file. awk -vN=AS1100002* 'NR==FNR { if($1 ~ N)K=$0; next } { if($1 in K) $0=K; print }' $datadir/file1... (7 Replies)
Discussion started by: ncwxpanther
7 Replies

4. Shell Programming and Scripting

Changing script from csh to bash

Hello Guys I have a script working fine on csh, but I would like to change it to bash, how I should change this command to be able to work as bash script. :wall: if ( $fsw > "0" ) then foreach swath ( `awk 'BEGIN {for (i='$fsw';i<='$lsw';i++) printf ("%s\n", i) }'` ) ## work to be done... (2 Replies)
Discussion started by: jiam912
2 Replies

5. Shell Programming and Scripting

Translate csh to ksh

Hi! I need to translate those line in csh (to initialise variable) into ksh construct. Any help would be appreciated! I don't know how to replace them :( Thanks Hulu setenv TestHul "$0 $*" setenv JG `setenvp "JG" "" "$*"` setenv A_1 `setenvp "A_1" "NA" "$*"` Please use next time... (2 Replies)
Discussion started by: patator67
2 Replies

6. Shell Programming and Scripting

Need a script to convert csh to bash

Hi, Can anyone give me a script to convert csh to bash? or any key points which can help me to do so as i am new to it. (3 Replies)
Discussion started by: vineet.dhingra
3 Replies

7. Shell Programming and Scripting

How to run a bash script in csh shell?

Hi how to execute a bash script in csh shell? Thanks (3 Replies)
Discussion started by: rubinovito
3 Replies

8. Shell Programming and Scripting

translate ksh code to csh code

hi all, Can any 1 help me translate this korn shell code to C shell code : email=$(grep "^$1" $folder/config_2.txt | awk '{print $2'}) In config_2.txt the content is : which mean in korn shell , $1=groupname and $2=email address. Now i need to write in C shell script,when i set the... (2 Replies)
Discussion started by: proghack
2 Replies

9. Shell Programming and Scripting

how to make your bash script run on a machine with csh and bash

hi, i have a script that runs on bash and would like to run it on a machine that has csh and bash. the default setting on that machine is csh. i dont want to change my code to run it with a csh shell. is there any way i can run the script (written in bash) on this machine? in other words is there... (3 Replies)
Discussion started by: npatwardhan
3 Replies

10. Shell Programming and Scripting

Converting bash script to csh

Hi, I'm a beginner in scripting and I recently wrote a bash script that would've worked fine until I realized it needed to be written in csh. Could someone please show me how to correctly change the syntax from bash to csh in this script? Any help will be greatly appreciated. I can provide more... (4 Replies)
Discussion started by: Kweekwom
4 Replies
Login or Register to Ask a Question