Katacoda doesn't accepts my source code... why? - unix

I was solving a unix problem on Katacoda. I am solving it as instructed but it gives me an error.
The problem is to display number of lines and words in a given input file
Write a command/logic, which will read the content from a given input file and display the number of lines and number of words in the file.
Your default logged in directory: /home/scrapbook/tutorial/
Input file location: /home/scrapbook/tutorial/unix_countproject/
Input file Name: input.txt
Script file location: /home/scrapbook/tutorial/
Script file Name: script.sh
Instructions:
You can view content of input file (input.txt) in the folder at /home/scrapbook/tutorial/unix_countproject/.
Note that this folder and file already exists and are located in the path mentioned above.
You can open the script.sh file using vi editor, Write the logic inside the file to display the number of lines and number of words in the input file (input.txt), After writing the logic , save the file and quit from vi editor .
To test your command/logic, Run the shell script file (script.sh) at the terminal using the below command
sh /home/scrapbook/tutorial/script.sh
If any issues while running the above command, Modify the script.sh file and repeat the point#:3
Don't use any echo statement in the script.sh file, even in the commented line/code
On completion of the task, click on SUMMARY or CONTINUE to proceed to next assignment.
Example : Contents of sample Input file named as input.txt - starting from following line
Hello all
Welcome to all of you
Expected Output:
2 7
As instructed, I store my logic in the script.sh file and it executes successfully and gives me the expected output but it's not accepting my answer.
$ cd unix_countproject
$ cat input.txt
Hi all
Welcome to Unix module
$ vi script.sh
Inside script.sh : wc -l -w input.txt|cut -c-6
(exit vi)
$ chmod +x script.sh
$ ./script.sh
2 6
Then I click on complete but it's not accepting it.

Related

Passing Path as parameters in unix script

My script has two functions running.
1. checking of file
(it has to check file in path /IIS/Data/arrival)
2. archiving of file
(it has to archive files in path /IIS/Data/archive)
how to pass this paths as parameter?
Not sure you are using shell script or not. In shell, we can pass path as parameter, shell treats it like string.
$ cat ./test.sh
#!/bin/sh
function check_file {
file=$1
ls $file
}
check_file $1
the output:
$ ./test.sh Downloads/bleachbit-1.10-1.1.fc20.noarch.rpm
Downloads/bleachbit-1.10-1.1.fc20.noarch.rpm

How to get latest timestamp file from FTP using UNIX Script

I am writing a shell script; I want to download latest uploaded file from FTP. I want to get latest file of specific folder. Below is my code for that. But it is not working as expected.
File names are in specific format like
.../MONTHLY_FILE/
ABC_ECI_12082015.ZIP
ABC_ECI_18092015.ZIP
ABC_ECI_09102015.ZIP
Here my return filename should be "ABC_ECI_09102015.ZIP".
Please help me out in this and let me know what mistake I am making.
#!/bin/ksh
. ospenv
#SRC_DIR=/powerm/Myway/SrcFiles
SRC_DIR=$PMRootDir/SrcFiles
cd $SRC_DIR
ftp -n gate.usc.met.com << FINISH
user ftp_abc.com xyz
##Here xyz is password
cd /MONTHLY_FILE
#mget ls -t -r | tail -n 1`enter code here`
get $1
bye
FINISH
I don't understand exactly where exactly you made a mistake, but this should do the trick:
ls -tr | tail -1
what's mget? why do you have an "enter code here"? What I wrote above should print out the last file... whether you save that into a variable, insert it directly to another action, it's up to you.

how to tail -f on multiple files with a script?

I am trying to tail multiple files in a ksh. I have the following script:
test.sh
#!/bin/ksh
for file in "$#"
do
# show tails of each in background.
tail -f $file>out.txt
echo "\n"
done
It is only reading the first file argument I provide to the script. Not reading the other files as the argument to the script.
When I do this:
./test.sh /var/adm/messages /var/adm/logs
it is only reading the /var/adm/messages not the logs. Any ideas what I might be doing wrong
You should use double ">>" syntax to redirect the stream at the end of your output file.
A simple ">" redirection will write the stream at the beginning of the file and consequently it will remove the previous content.
So try :
#!/bin/ksh
for file in "$#"
do
# show tails of each in background.
tail -f $file >> out.txt & # Don't forget to add the last character
done
EDIT : If you want to use multi tail it's not installed by default. On Debian or Ubuntu you can use apt-get install multi tail.

Is it possible to redirect stdout to a file location rather than a file descriptor?

My problem is that I am redirecting stdout/stderr to log files but logrotate comes and moves files around with output then going a file descriptor without a file.
Is there is any way in shell or at the system level to redirect the output to the new file at the same location?
If you don't mind re-opening the file for each line, instead of:
$ ... > logfile.txt 2>&1
do:
$ ... 2>&1 | while read line; do echo $line >> logfile.txt ; done

Shell script to sort & mv file based on date

Im new to unix,I have search a lot of info but still don not how to make it in a bash
What i know is used this command ls -tr|xargs -i ksh -c "mv {} ../tmp/" to move file by file.
Now I need to make a script that sorts all of these files by system date and moves them into a directory, The first 1000 oldest files being to be moved.
Example files r like these
KPK.AWQ07102011.66.6708.01
KPK.AWQ07102011.68.6708.01
KPK.EER07102011.561.8312.13
KPK.WWS07102011.806.3287.13
-----------This is the script tat i hv been created-------
if [ ! -d /app/RAID/Source_Files/test/testfolder ] then
echo "test directory does not exist!"
mkdir /app/RAID/Source_Files/calvin/testfolder
echo "unused_file directory created!"
fi
echo "Moving xx oldest files to test directory"
ls -tr /app/RAID/Source_Files/test/*.Z|head -1000|xargs -i ksh -c "mv {} /app/RAID/Source_Files/test/testfolder/"
the problem of this script is
1) unix prompt a syntax erro 'if'
2) The move command is working but it create a new filename testfolder instead move to directory testfolder (testfolder alredy been created in this path)
anyone can gv me a hand ? thanks
Could this help?
mv `ls -tr|head -1000` ../tmp/
head -n takes the n first lines of the previous command (here the 1000 oldest files). The backticks allow for the result of ls and head commands to be used as arguments to mv.

Resources