Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
This sounds like homework, so I'm not going to write your script for you. Some pointers might help you though. These statements apply to bash and other borne-like shellsm which seems to me the most likely subject of your question (although you didn't specify a shell script):
If you have a variable v, you can find the length of the string assigned to v using ${#v}
Integer comparison is done using -lt (less than), -gt (greater than), -le (less than or equal), -ge (greater than or equal), -eq (equal to), -ne (not equal)
You can read from a file with the < input re-direction operator
You can append to a file with the >> output re-direction operator
You can read lines from a file with the while read variablename; do ...; done construction
You can print a string using the echo command
You should be able to construct the program from that information and a little searching for examples.
Last edited by matthewg42; 11-24-2007 at 10:45 AM.
Quick hint: you can post your code in [code] tags here to improve readability and preserve formatting.
You can use Pascal on Linux. I can't speak from experience, but I should imagine there would be little in the way of porting of such a program. If you're using Ubuntu, the package name is pascal-compiler.
There is no real "Linux Language" - you can find compilers/interpreters for pretty much anything which will let you build software in almost any language and have it running on Linux.
Unix-likes have a strong history of using C, shell script, awk, Perl, TCL and so on. More modern popular languages include Python, Ruby, C# and so on... Your choice of language will depend on your experience and the type of problem you wish to solve.
Shell script is very useful for cobbling together quick scripts to perform even quick complex tasks, but it is not really very good for line-by-line editing of file contents. You would usually do something like this with a specialist mini-language like sed, or something like awk or Perl.
Now, assuming you want to learn shell scripting (despite the fact that this task is probably more easily accomplished using something else), here's my suggestions about your posted script:
v will probably not be set at the start of the script, and even if it is, don't worry. When you read into it, the original value will be replaced. Don't worry about this affecting the calling shell - a child process cannot modify the environment variables in the parent script.
You while loop can accept the file in question using re-direction:
Code:
while read line; do
...
done < input_file
In this loop, line contains the current line of the file. As I mentioned in the first post, ${#line} will contain the number of characters in the line... You can test this with an if statement and output the original if the length is more than 158 characters, and output the original plus a 0 if it is less.
These outputs can be done with echo, and the result appended to the output file using >>.
[Quote]v will probably not be set at the start of the script, and even if it is, don't worry. When you read into it, the original value will be replaced. Don't worry about this affecting the calling shell - a child process cannot modify the environment variables in the parent script.
You while loop can accept the file in question using re-direction:
Code:
while read line; do
...
done < input_fileIn this loop, line contains the current line of the file. As I mentioned in the first post, ${#line} will contain the number of characters in the line... You can test this with an if statement and output the original if the length is more than 158 characters, and output the original plus a 0 if it is less.
These outputs can be done with echo, and the result appended to the output file using >>.[Quote]
what I understand it's something like this:
while read line ;do
if ${#line} -lt 158 then
echo "line is above 158"
elif ${#line} -eq 159 then
exit
done < readme.txt
If it dosen't could you show me some examples to see what I've been thinking and to understood what you have said before.
Thanks for the help being helded.
I think I missunderstood what I've Quoted.
Quote:
v will probably not be set at the start of the script, and even if it is, don't worry. When you read into it, the original value will be replaced. Don't worry about this affecting the calling shell - a child process cannot modify the environment variables in the parent script.
You while loop can accept the file in question using re-direction:
Code:
while read line; do
...
done < input_fileIn this loop, line contains the current line of the file. As I mentioned in the first post, ${#line} will contain the number of characters in the line... You can test this with an if statement and output the original if the length is more than 158 characters, and output the original plus a 0 if it is less.
These outputs can be done with echo, and the result appended to the output file using >>.
what I understand it's something like this:
while read line ;do
if ${#line} -lt 158 then
echo "line is above 158"
elif ${#line} -eq 159 then
exit
done < readme.txt
If it dosen't could you show me some examples to see what I've been thinking and to understood what you have said before.
The first section doesn't accomplish anything, and is not necessary.
There's a design issue to address... You could hard-code the input file in the script and then make a while loop like I mentioned before. You could also hard-code the output file name and use appending redirects, like this:
Code:
#!/bin/bash
# remove existing output_file - we don't want to just append to existing files.
rm -f output_file
while read line; do
if [ ${#line} -lt 158 ]; then
echo "${line}0" >> output_file
else
echo "${line}" >> output_file
fi
done < input_file
Lets say you saved this to myscript1.sh. To run it, you would make the script executable and then run it by invoking it's name (and path if your PATH environment variable doesn't contain the directory where the script is saved). i.e. this from the command line. the % represents the prompt - do not type it:
Code:
% chmod a+x myscript1.sh
% ./myscript1.sh
The chmod command changes the mode of the file (also known as the file's permissions), adding the execute permission for all users on the system (a+x). Incidentally to remove the execute permission, you would use a-x.
The program would read from the file input_file and output to the file output_file, overwriting any filee which already exists with that name.
Another approach, which is probably better style, is to let the person who runs the program do the re-direction. If you call read from a script and have not specified an input using the < input re-direction, the script will read from whatever input the caller of the script specifies with the input re-direction operator (or just the keyboard if nothing is specified).
Similarly, if you don't explicitly re-direct output to a file in the script, the person who runs the scrip can re-direct the output, deciding where it goes when they run the script.
Here's how. The script is almost the same, except the re-direction is not done:
Code:
#!/bin/bash
while read line; do
if [ ${#line} -lt 158 ]; then
echo "${line}0"
else
echo "${line}"
fi
done
Then, when you run the script, you can re-direct the input and output like this (lets say the script is saved to the file myscript2.sh). Don't forget to change the permissions before you run it for the first time.
Could I have this script making for another files with different number of characters, and make cycles for one of each them.
Read in variable
compare variable
and
choose cycle (atribute cycle)
The great ideia it's something like this.
And by the way could I make the script running in windows plataform.
Thanks for the help being helded.
By the way and I'm sorry for being a boring person.
But could you explain me these I have some doubts
if [ ${#line} -lt 158 ]; then (what this means or when this is used, is this some parameter that could be exchanged ${#line}
echo "${line}0" (here I think that this line do this, put the 0 at the end of the line if it's true the above condition)
else
echo "${line}" ( this prints the line ?)
fi (the end of the cycle Probably )
Thanks
Do you have any book recomendation to begin script on linux ?
Could I have this script making for another files with different number of characters, and make cycles for one of each them.
Read in variable
compare variable
and
choose cycle (atribute cycle)
The great ideia it's something like this.
I don't understand.
Quote:
Originally Posted by LMSSML
And by the way could I make the script running in windows plataform.
Shell scripting is not a Windows technology. However, you can run shell scripts using cygwin. This is a somewhat limited solution though.
Quote:
Originally Posted by LMSSML
Thanks for the help being helded.
By the way and I'm sorry for being a boring person.
But could you explain me these I have some doubts
if [ ${#line} -lt 158 ]; then (what this means or when this is used, is this some parameter that could be exchanged ${#line}
${#line} is the length of the string which is held in the variable named line.
Quote:
Originally Posted by LMSSML
echo "${line}0" (here I think that this line do this, put the 0 at the end of the line if it's true the above condition)
else
echo "${line}" ( this prints the line ?)
The simplest way to take the value of the variable line is to do this:
Code:
$line
However, we want to add a '0' character immediately after this. If we do
Code:
$line0
...the shell program cannot know what we mean "the value of line followed by a 0 character" instead of "the value of the variable line0. So I used the longer variable expansion syntax:
Code:
${line}
Which makes it easy for the shell to know what I mean.
Quote:
Originally Posted by LMSSML
fi (the end of the cycle Probably )
Correct.
Quote:
Originally Posted by LMSSML
Thanks
Do you have any book recomendation to begin script on linux ?
Could I have this script making for another files with different number of characters, and make cycles for one of each them.
Read in variable
compare variable
and
choose cycle (atribute cycle)
The great ideia it's something like this.
What do I do wnat to say it's simple
If I want to increment program and have files to 58 characters I could make a cycle and solve my problem but the question in here it's the following.
The user introduces the name of the file and the name of the file it's associated to the cycle for 158 or 58 characters then he goes to the end of the file and puts a 0.
1.The user is prompted to insert the name of the file.
2. Program keeps the name of the file in a variable and then compares with a list of files
3. Then after comparing it calls the cycle that file belongs to.
4. cycle make the changes in the file.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.