Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Here u are the code i want to do , the problem is the special characters i punt like / and then and those things while executing , it dont consider the text between those two parentheses () a string to save it in a variable and that is all so how can i fox this i put the whole code to give u a clear idea about the problem Thanks in advance

#!/bin/bash 

trap "echo 'Caught' >/home/ahmedubuntu/Desktop/fsg.txt" SIGINT SIGTERM SIGHUP


script=( #!/bin/bash 
echo "fdsfds"
while sleep 4
do
i=0
while read line
do
if [ -n "$line" ] && ["$line" != "separator"] 
then
programeNameAndPID[i]=$line
((i++))
elif [["$line" == "separator"]] ; then
getAllPIDRunUnderThisProgram=$( ps -e | grep "$programeNameAndPID[0]" | awk '{print $1;}')
if [${#getAllPIDRunUnderThisProgram[*]} -gt ${#programeNameAndPID[*]}-2  ]
then
unset gnomeTab
newProcessNumber=${#getAllPIDRunUnderThisProgram[*]}-${#programeNameAndPID[*]}-2
index=${#getAllPIDRunUnderThisProgram[*]}-1
for (( c=$newProcessNumber ; c > 0 ; c-- ))
do
gnomeTab+=( --tab -e "cpulimit -p $getAllPIDRunUnderThisProgram[$index] -l $programeNameAndPID[1] ") 
((index--)) 
done
gnome-terminal "${gnomeTab[@]}"
fi
i=0
unset programeNameAndPID
fi
done < $dataFile
done)
echo -e $script > /home/ahmedubuntu/Desktop/script.txt
gnome-terminal -e " /home/ahmedubuntu/Desktop/script.txt"

also i tried this part of code to store the text in file directly and i found taht the stored is the run resut of the code not just considered a string and just write it in file as i want

cat > /home/ahmedubuntu/Desktop/script.sh << EOF 
#!/bin/bash 
echo "fdsfds"
while sleep 4
do
i=0
while read line
do
if [ -n "$line" ] && ["$line" != "separator"] 
then
programeNameAndPID[i]=$line
((i++))
elif [["$line" == "separator"]] ; then
getAllPIDRunUnderThisProgram=$( ps -e | grep "$programeNameAndPID[0]" | awk '{print $1;}')
if [${#getAllPIDRunUnderThisProgram[*]} -gt ${#programeNameAndPID[*]}-2  ]
then
unset gnomeTab
newProcessNumber=${#getAllPIDRunUnderThisProgram[*]}-${#programeNameAndPID[*]}-2
index=${#getAllPIDRunUnderThisProgram[*]}-1
for (( c=$newProcessNumber ; c > 0 ; c-- ))
do
gnomeTab+=( --tab -e "cpulimit -p $getAllPIDRunUnderThisProgram[$index] -l $programeNameAndPID[1] ") 
((index--)) 
done
gnome-terminal "${gnomeTab[@]}"
fi
i=0
unset programeNameAndPID
fi
done < $dataFile
done
EOF
share|improve this question
    
data=`cat <<EOF line1 line2 (...) lineN EOF`? –  Salem May 4 '13 at 12:34
    
Why dont you store the actual contents in /home/ahmedubuntu/Desktop/script.txt itself –  thefourtheye May 4 '13 at 13:20
    
ok i already tried it i will edit the question to see what i get –  Ahmed Zain El Dein May 4 '13 at 13:27
add comment

1 Answer

up vote 2 down vote accepted

probably the main problem in both cases is that the $variables will be expanded before you want. Try

script=$(cat <<'END_OF_SCRIPT'
# your script goes here. 
END_OF_SCRIPT
)

The single quotes on the first END_OF_SCRIPT essentially single-quote the entire here-document.

Next, it is highly important to quote $script whenever you use it:

echo "$script" > some_file

Otherwise all sequences of whitespace, including newlines, will be collapsed to a single space.

share|improve this answer
    
Thank u sir really it works wonderfully :) and yes u are right the problem was that he writes the valus of the $variable instead of just write the text as if it is plain text in the file , but i wonder how just the ' ' single quote of the first 'EOF' solved the problem _ more explanation pls :) –  Ahmed Zain El Dein May 4 '13 at 14:10
1  
See the bash manual: gnu.org/software/bash/manual/bashref.html#Here-Documents –  glenn jackman May 4 '13 at 14:23
    
thank u but in belong to <<- and the leading tabs i tried it and all the tabs in the input lines appeared in the file script.sh and they say over there that it will be stripped so ? any explanation –  Ahmed Zain El Dein May 4 '13 at 15:04
    
make sure they are actual tab characters and not spaces. the manual is specific about that. –  glenn jackman May 4 '13 at 17:12
    
they mean that right "\t"? –  Ahmed Zain El Dein May 4 '13 at 17:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.