Need Help With Shell Script

Hi guys I just started learning Linux, Need some help writing the script for the following

1. An argument of the form-6 sets the script to display the first 6 lines of the succeeding file(s). the default should be 5.

2. Anything else of the form "-x" should produce an error message

3. For a file name, display the first _lines of the file


Similar Content



Shell Scripting

can some1 please tell me ....i am writing script to display type of file....and i m not able to get what "filename##*\." means?....please be brief in your explanation..thank you
the program is like:
for filename in$(ls)
do
ext=${filename##*\.}
case '$ext' in

Script While Loop

Hello,
I'm working on a shell script that needs to read the a file (file1) a batch (read 2000 lines at a time) and then write those lines to seperate file which I'm using to run ldapmodify command. I need to check some other file size and once this file less than 200kb, i would need to run second batch and so on until file1 is empty.
Quote:
#!/bin/bash
set -x
filesize=200
server=10.11.xxx.xx
filename=fileimport.txt
taofile1=687686.txt
taofile=/var/opt/$taofile1

stty -echo
echo -n "Enter password: "
read passwd
stty echo
context="cn=user1,ou=org1,ou=org2,o=org"
if [[ -s "$filename" ]];then
for lines in $filename
do
head -99997 fileimport.txt >> ldapreadd.ldif && sed -i '1,+99996d' fileimport.txt
if [ -s ldapreadd.ldif ];then
ldapmodify -h $server -p 389 -D $context -w $passwd -v -x -f ldapreadd.ldif &> /dev/null
echo "LDIF script is running. Please wait..."
sleep 60
if [ -f "$taofile" ];then
taofilesize=`stat -c %s ${taofile}`
# if [ "$taofilesize" -ge 200 ];then
while [ `stat -c %s ${taofile}` -ge 200 ]
do
echo " Driver is processing LDIF file. Please wait..."
sleep 60
done
fi
rm -rf ldapreadd.ldif
fi
done
else
echo "$filename file is empty. Exiting script..."
exit
fi

I have 2 issues here. first one script is not reading exact lines as specified. Some times its reading more lines some times less lines. Second issues once it run the first batch, script is exiting. Please advise.

Shell Scripting

Please tell me for shell script..
I m writing the script.
In that script i written the 1 function and in that function i have to write the contents in existing file
so i write,
cat > /etc/puppet/manifests/init.pp
{
ensure => present,
}
.
But it caanot write the changes in ssh machine
so what i can do in my script?
Please help me...

Help For Basics Of Shell

I'm super newb and want to start learning by doing simple stuff. So maybe you could give me some support on those tasks -
Creating a procedure that would take file's name as an argument checking current folder AND IF there is no such file (under the name of argument just taken) it would print the message and create a file under mentioned name.

I bet completing this task would "get me on the track" and I could continue from there on my own for a while.

THANKS!

Script To Find The File Creation Time Is More Than Ten Minutes

Hi All,

Im trying to create a script which checks the creation time of a file and if it is more than ten minutes, send out an alert.

For ex : Creation date of file is 10:30 AM
current time is 10:45 AM

Then send an alert/ message.

This is the script i wrote below :

Code:
#!/bin/ksh

filename="/apps/log/file.txt"

if [ -f "${filename}" ]
then
        createTime=`ls -lad "${filename}" | awk '{print $8}'`
        echo "$createTime"
        currentTime=`date '+%M'`
        echo "$currentTime"
        DIFF=$(( $currentTime - $createTime )) 
        echo "$DIFF"

else

        exit 1

fi

I am getting syntax error on the subraction when i try to run this script . I can understand that creation time and current time is in different format thats y this error throws, but i dont know how to rectify it .

I need to find out if the file creation time is more than ten minutes.

Please help me in achieving this output.

Best Way To Run Two Interdependent Scripts

Hi All,

I have two scripts, the aim of these two scripts is, to check whether a particular script is running or not, if it wont runs, then throw a mail.

How i Achieved this output is, I wrote first script.

I created an infinite while loop which performs below steps

1. It creates a touch file
2. Triggers the script which needs to be monitored if its working or not.
3. Removes the touch file.

If the second step fails, then the remove file command will not happen and the script will stuck there itself.

I created an another script which checks the creation time of the touch file and if it is more than ten minutes, it means the second step in the first script is hanged, which also means that particular script is not working.

So if the creation time is more than 2 minutes the second script will throw a mail.

Below are the two scripts.

Code:
#!/bin/ksh



userid="chansd"

filename="/apps/log/check.txt"

while true ;do
touch $filename
pass=`/apps/eDMZ/call_st.ksh $userid`
sleep 20
rm $filename
done

Below script checks the file creation time and throws email if it is older than 2 minutes
Code:
#!/bin/ksh


filename="/apps/log/check.txt"

if [ -f "${filename}" ]
then
if test "`find $filename -mmin +2`"
then
echo "script is not working ! Please act on it" | mail -s "Script  is not working" Example@mail.com
fi


else

        exit 1

fi


What im going to do is

1. I am going to run the first script in background so it runs forever.
2. I am going to run the second script in cron forevry 5 mins to check the file creation time.

3. So if the first script hangs . I will kill the process using process id and after the issue resolves with the inner script, I will run the main script again.

I am new to Linux, Please let me know if this approach will work as expected.

Need Help To Get The Available Hosts Among Many Hosts

I have many hosts as following. But those hosts are dual operating system (Linux and Windows). I always run program background. If someone is using Linux, it is fine. But if someone is using windows or the host is offline. Then, I cannot use ssh. The way I do preiviously is first ssh one by one. And then find the hosts which are offline or windows. And write them down one by one. And then ssh to hosts except them. Let us assume the number of programs is less than the total available hosts. Can someone write a shell script to output the all available hosts to a file like "host_available"?
Here is the host file.
https://www.dropbox.com/s/vbz6w864y3...tlist.txt?dl=0
I am using ssh to connect the computers on campus. If the computer I am trying to connect is offline or using windows, the ssh will take long time, and finally it will failed. I write a shell script to generate the hostlist
Code:
#!/bin/bash
for i in `seq -w 1 28`
do
echo "c15-0330-$i.ad.mtu.edu"
# I would like to add a command here to see whether ssh c15-0330-$i.ad.mtu.edu it succeeds or not. And then output a #file which contains the all available host.
done
for i in `seq -w 1 20`
do
echo "c28-0112-$i.ad.mtu.edu"
done
for i in `seq -w 1 20`
do
echo "c28-112a-$i.ad.mtu.edu"
done

I do not how to set a certain time to see whether connection is successful or not (see the comment in the shell script).

How To Copy File From Remote Host To Local Host Then Delete From Remote Host

I have an expect script to SSH to a remote host and obtain some user inputs and information about the server/network configuration. The responses are saved in a text file that I then need to copy to my local host so that I can read the lines into variables for use in the parent shell script.

Is there a way to do this without needing to enter the username and password for the local host to use function scp? I have tried the following in my expect script to no avail:
Code:
spawn scp $usr@$host:$flnm .
expect {
	-re "(.*)assword:" { 
		send -s "$pswd\r"
	}
}

I have also tried to directly scp the file and enter the username and password to try to debug the issue, and that doesn't work either:
Code:
spawn scp file.txt user@host:file.txt
expect {
	-re "(.*)assword:" {
		send -s "password\r"
	}
	"you sure you want to continue connecting" {
		send -s "yes\r"
		exp_continue
	}
}

In both scenarios I have used exp_internal 1, and there are no errors. But I do not end up with the file on my local host.

Following the copy, I would like to delete the file from the remote host. Any suggestions on how to accomplish this?

How To Run A "sudo" Script Without Password

Hi all,
I have a script where every line needs to be prefixed with sudo.
I was advised to run it as $ sudo /path/to/file.sh

This script needs to be scheduled in crontab to run. If I run it as above, will I be prompted for a password?
Also, one of the lines in the script is;
scp -r root@rem_server:/source/dir /local/dir
Does that mean I will have to provide two passwords: one for sudo and one for root?

Thanks,

Cygwin Rsync Script Copying File Incorrectly

I am trying to write a simple test script that executes a single rsync call. Once I get the syntax right, the script will be expanded to incorporate several rsync instructions to save me time.

I'm using a Cygwin Terminal on Windows 7 to run Rsync.

The script is called "backup" and I call it from the cygwin terminal using "./backup". Have used chmod to make it executable.

When I execute the rsync instruction as a standalone instruction at terminal prompt, the file copies to the target folder perfectly.

However, when I try and drop the same instruction into a script, it copies the file over from it's original file name "mvi_1840.mov" to a file with the name "." -- literally a single period. I can rename the file to a .mov file and it works fine but naturally, I don't want to have to do this.

The one other thing I will mention is that the file is located in the /home/"username" directory in cygwin vs /bin. But I would not think that this would make a difference.

I have pasted the contents of the script file below:
Code:
#!/bin/bash
    rsync.exe -rltDvP --exclude "System Volume Information" --exclude
 "RECYCLER" --exclude=\$RECYCLE.BIN --delete "/cygdrive/c/users/<USERNAME>
/desktop/mvi_1840.mov" "/cygdrive/c/users/<USERNAME>/desktop/test/"

Any help would be greatly appreciated!

Thanks